Java Labb7 PDF
Java Labb7 PDF
Java Labb7 PDF
a)Write a Java program that prints all real solutions to the quadratic
equation ax2 + bx +c= 0. Read in a, b, c and use the quadratic
formula. If the discriminant b2 -4ac is negative, display a message
stating that there are no real solutions.
import java.util.*;
public class Quadratic
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter any three values:");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
int D = b * b - 4 * a * c;
if(D<0)
System.out.println("There is no real solution");
else
System.out.println("There is a real solution");
}
}
Output:
Enter any three values:4 1 1
There is no real solution
}
}Output:
Enter number of names:4
Enter all the names: randy john roman brock
Names in Sorted Order:brock,john,randy john roman brock,roman
c)Write a Java Program that reads a line of integers, and then
displays each integer, and then sum of all the integers (Use
StringTokenizer class of java.util)
import java.util.*;
class StringTokenizerEx
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("\nEnter A Line Of Integers:");
String line = s.nextLine();
void eat()
{
System.out.println("Rodent is eating grass");
}
void tail()
{
System.out.println("Rodent has long tail");
}
}
class Mouse extends Rodent
{
void place()
{
System.out.println("mouse lives in all countries");
}
void eat()
{
System.out.println("mouse is eating plants and meat");
}
void tail()
{
System.out.println("mouse has long tail\n");
}
}
class Gerbil extends Rodent
{
void place()
{
System.out.println("gerbil lives in sandy planes");
}
void eat()
{
System.out.println("gerbil is eating nuts");
}
void tail()
{
System.out.println("gerbil has normal tail\n");
}
}
class Hamster extends Rodent
{
void place()
{
System.out.println("Hamster lives in dry areas");
}
void eat()
{
System.out.println("Hamster is eating seeds");
}
void tail()
{
System.out.println("Hamster is short tail\n");
}
}
class RodentsDemo
{
public static void main(String args[])
{
Rodent r[] = new Rodent[3];
r[0] = new Mouse();
r[1] = new Gerbil();
r[2] = new Hamster();
for(int i=0;i<3;i++)
{
r[i].place();
r[i].eat();
r[i].tail();
}
}
}
Output:
mouse lives in all countries
mouse is eating plants and meat
mouse has long tail
javac -d . Subraction.java
javac -d . Multiplication.java
javac -d . Division.java
javac PackageDemo.java
java PackageDemo
Sum:30
difference:10
product:200
division:2
Exercise 7
a) Write a Java program demonstrating the life cycle of a thread.
class ThreadDemo extends Thread
{
public void run()
{
System.out.println(getState());
}
}
class ThreadCycle
{
public static void main(String[] args)
{
ThreadDemo t1=new ThreadDemo();
ThreadDemo t2=new ThreadDemo();
t1.setName("PVPSIT");
t2.setName("VRSEC");
System.out.println(t1.getName()+":"+t1.getState());
System.out.println(t2.getName()+":"+t2.getState());
System.out.println();
t1.start();
System.out.println(t1.getName()+":"+t1.getState());
System.out.println(t2.getName()+":"+t2.getState());
System.out.println();
t2.start();
System.out.println(t1.getName()+":"+t1.getState());
System.out.println(t2.getName()+":"+t2.getState());
}
}
Output:
VPSIT:NEW
VRSEC:NEW
PVPSIT:RUNNABLE
VRSEC:NEW
RUNNABLE
PVPSIT:RUNNABLE
VRSEC:RUNNABLE
RUNNABLE
b) Develop an applet that displays a simple message
import java.awt.*;
import java.applet.*;
/*
<applet code="HelloWorld.class" width=10000 height =1000>
</applet>
*/
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
g.drawString("PVPSIT",90,90);
}
}
Output:
Exercise 8
a)Develop an applet that receives an integer in one text field, and
computes its factorial Value and returns it in another text field,
when the button named “Compute” is clicked.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Example.class" width=500 height=500>
</applet>
*/
public class Example extends Applet implements ActionListener
{
Label l1,l2;
Button b;
TextField t1,t2;
public void init()
{
l1=new Label("Enter Number");
l2=new Label("Result");
t1=new TextField(10);
t2=new TextField(20);
b=new Button("compute");
add(l1);
add(t1);
add(b);
add(l2);
add(t2);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int a=Integer.parseInt(t1.getText());
int fact=1;
for(int i=1;i<=a;i++)
fact=fact*i;
t2.setText(""+fact);
}
}
Output:
b)Write a Java program that allows user to draw lines, rectangles
and ovals.
import java.awt.*;
import java.applet.*;
/*
<applet code="GraphicsDemo.class" width=500 height=100>
</applet>
*/
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("welcome",100,100);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.blue);
g.fillOval(220,150,30,30);
g.drawArc(90,150,100,100,0,270);
g.fillArc(270,150,30,30,0,180);
}
}
Output:
Exercise 9
a) Write a Java program that works as a simple calculator. Use a
grid layout to arrange buttons for the digits and for the +, -,*, %
operations. Add a text field to display the result.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="Calculator" width=200 height=200>
</applet>
*/
}
public void actionPerformed(ActionEvent ae)
{
int num1=0,num2=0,res=0;
if(ae.getSource()==b1)
{
try
{
num1=Integer.parseInt(t1.getText());
num2=Integer.parseInt(t2.getText());
res=num1/num2;
t3.setText(Integer.toString(res));
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null,"Invalid
Number.Only integers are allowed");
}
catch(ArithmeticException aex)
{
JOptionPane.showMessageDialog(null,"Invalid
Arthmetic Exception Division cannt be are
allowed");
}
}
if(ae.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
Output:
b) Write a Java program that lets users create Pie charts. Design
your own user interface
import java.awt.*;
import java.applet.*;
/*
<applet code="Graphic" width=400 height=400>
</applet>
*/
public class Graphic extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillArc(110,80,200,200,0,120);
g.drawString("IT Dept:33.3%",10,50);
g.setColor(Color.red);
g.fillArc(110,80,200,200,120,120);
g.drawString("CSE Dept:33.3%",10,80);
g.setColor(Color.blue);
g.fillArc(110,80,200,200,240,120);
g.drawString("ECE Dept:33.3%",10,110);
}
} output: