Java Lab Manual-1
Java Lab Manual-1
.
b)Roots of a quadratic equation
AIM: To write a java program that display the roots of a quadratic equation ax2+bx=0.
Calculate the discriminate D and basing on value of D, describe the nature of root.
SOURCE-CODE:
import java.util.Scanner;
public class QuadraticEquationExample1
{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d= b * b - 4.0 * a * c;
if (d> 0.0)
{
double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
}
else
{
System.out.println("Roots are not real.");
}
}
}
Output 1:
Output 2:
b) Bike Race:
AIM: Five Bikers Compete in a race such that they drive at a constant speed which may or may not be the
same as the other. To qualify the race, the speed of a racer must be more than the average speed of
all 5 racers. Take as input the speed of each racer andprint back the speed of qualifying racers.
SOURCE-CODE:
import java.util.*;
class RaceDemo
{
public static void main(String[] args)
{
float s1,s2,s3,s4,s5,average;
Scanner s = new Scanner(System.in);
System.out.println("Enter speed of first racer:");s1 =
s.nextFloat();
System.out.println("Enter speed of second racer:");s2 =
s.nextFloat();
System.out.println("Enter speed of third racer:");s3 =
s.nextFloat();
System.out.println("Enter speed of fourth racer:");s4 =
s.nextFloat();
System.out.println("Enter speed of fifth racer:");s5 =
s.nextFloat(); average=(s1+s2+s3+s4+s5)/5;
if(s1>average)
System.out.println("First racer is qualify racer:");else
if(s2>average)
System.out.println("Second racer is qualify racer:");else
if(s3>average)
System.out.println("Third racer is qualify racer:");else
if(s4>average)
System.out.println("Fourth racer is qualify racer:");else
if(s5>average)
System.out.println("Fifth racer is qualify racer:");
}
}
OUT-PUT:
Enter speed of first racer:
4.5
Enter speed of second racer:
6.7
Enter speed of third racer:
3.8
Enter speed of fourth racer:
5.3
Enter speed of fifth racer:
4.9
Step-1: Click start+run and then type notepad in run dialog boxand click OK. It displays Notepad.
Step-2: In run dialogbox type cmd and click OK. It displays command prompt.
Step-3: Type the following SOURCE-CODE in the Notepad and save the SOURCE-CODEas
“example.java” in a current working directory.
class example
{
public static void main(String args[])
{
System.out.println(“Welcome”);
}
}
Step-4 (Compilation): To compile the program type the following in current workingdirectory and then
click enter.
c:\xxxx >javac example.java
Step-5 (Execution): To run the program type the following in current working directory andthen click enter.
c:\xxxx>java example
Explanation:
Generally the file name and class name should be same. If it is not same then the java filecan be compiled
but it cannot be executed. That is when execution it gives the following error
int area()
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();int
r=a1.area();
System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
4. return type and with parameters:
class A
{
int area(int l,int b)
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area(10,20);
System.out.println(“The area is:”+r);
}
}
OUT-PUT:
The area is:200
b) Implementing Constructor:
AIM: To write a JAVA Program to implement constructor
SOURCE-CODE:
(i) A constructor with no parameters:
class A
{
int l,b;
A()
{ l=10;
b=20;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A();int
r=a1.area();
System.out.println("The area is: "+r);
}
}
OUT-PUT:
The area is:200
(ii) A constructor with parameters:
class A
{
int l,b;
A(int u,int v)
{
l=u;
b=v;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A(10,20);
int r=a1.area(); System.out.println("The
area is: "+r);
}
}
OUT-PUT:
The area is:200
EXPERIMENT - 4
a) Constructor Overloading:
AIM: To write a JAVA program to implement constructor overloading
SOURCE-CODE:
class A
{
int l,b;
A( )
{ l=10;
b=20;
}
A(int u,int v)
{
l=u;
b=v;
}
int area()
{
return l*b;
}
}
class overconstructdemo
{
public static void main(String args[])
{
A a1=new A(); int
r1=a1.area();
System.out.println("The area is: "+r1);A
a2=new A(30,40);
int r2=a2.area(); System.out.println("The
area is: "+r2);
}
}
OUT-PUT:
The area is: 200
The area is: 1200
b) Method Overloading:
AIM: To write a JAVA program implement method overloading
SOURCE-CODE:
class A
{
int l=10,b=20;
int area()
{
return l*b;
}
int area(int l,int b)
{
return l*b;
}
}
class overmethoddemo
{
public static void main(String args[])
{
A a1=new A(); int
r1=a1.area();
System.out.println("The area is: "+r1);int
r2=a1.area(5,20); System.out.println("The
area is: "+r2);
}
}
OUT-PUT:
The area is: 200
The area is: 100
EXPERIMENT - 5
a) Implementing Single Inheritance:
AIM: To write a JAVA program to implement Single Inheritance
SOURCE-CODE:
class A
{
A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's Constructor");
}
}
class singledemo
{
public static void main(String args[])
{
B b1=new B();
}
}
OUT-PUT:
Inside A's Constructor
Inside B's Constructor
b) Multi level Inheritance:
AIM: To write a JAVA program to implement multi level Inheritance
SOURCE-CODE:
class A
{
A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's Constructor");
}
}
class C extends B
{
C()
{
System.out.println("Inside C's Constructor");
}
}
class multidemo
{
public static void main(String args[])
{
C c1=new C();
}
}
OUT-PUT:
Inside A's Constructor
Inside B's Constructor
Inside C's Constructor
c) Abstract Class:
AIM: To write a java program for abstract class to find areas of different shapes
SOURCE-CODE:
abstract class shape
{
abstract double area();
}
class rectangle extends shape
{
double l=12.5,b=2.5;
double area()
{
return l*b;
}
}
class triangle extends shape
{
double b=4.2,h=6.5;
double area()
{
return 0.5*b*h;
}
}
class square extends shape
{
double s=6.5;
double area()
{
return 4*s;
}
}
class shapedemo
{
public static void main(String[] args)
{
rectangle r1=new rectangle();
triangle t1=new triangle(); square
s1=new square();
System.out.println("The area of rectangle is: "+r1.area());
System.out.println("The area of triangle is: "+t1.area());
System.out.println("The area of square is: "+s1.area());
}
}
OUT-PUT:
The area of rectangle is: 31.25The
area of triangle is: 13.65 The area
of square is: 26.0
EXPERIMENT - 6
a) super keyword implementation
AIM: Write a JAVA program give example for “super” keyword
SOURCE-CODE:
(i) Using super to call super class constructor (Without parameters):
class A
{
int l,b;
A()
{ l=10;
b=20;
}
}
class B extends A
{
int h;
B()
{
super();
h=30;
}
int volume()
{
return l*b*h;
}
}
class superdemo
{
public static void main(String args[])
{
B b1=new B();
int r=b1.volume();
System.out.println("The vol. is: "+r);
}
}
OUT-PUT:
The vol. is:6000
(ii) Using super to call super class constructor (With parameters)
class A
{
int l,b;
A(int u,int v)
{
l=u;
b=v;
}
}
class B extends A
{
int h;
B(int u,int v,int w)
{
super(u,v);
h=w;
}
int volume()
{
return l*b*h;
}
}
class superdemo1
{
public static void main(String args[])
{
B b1=new B(30,20,30);
int r=b1.volume();
System.out.println("The vol. is: "+r);
}
}
OUT-PUT:
The vol. is:18000
b) Implementing interface:
AIM: To write a JAVA program to implement Interface.
SOURCE-CODEs:
(i) First form of interface implementation:
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("B's method");
}
}
class C extends B
{
public void callme()
{
System.out.println("C's method");
}
}
class interfacedemo
{
public static void main(String args[])
{
C c1=new C();
c1.display();
c1.callme();
}
}
OUT-PUT:
B's method
C's method
(ii) Second form of interface implementation:
interface D
{
void display();
}
interface E extends D
{
void show();
}
class A
{
void callme()
{
System.out.println("This is in callme method");
}
}
class B extends A implements E
{
public void display()
{
System.out.println("This is in display method");
}
public void show()
{
System.out.println("This is in show method");
}
}
class C extends B
{
void call()
{
System.out.println("This is in call method");
}
}
class interfacedemo
{
public static void main(String args[])
{
C c1=new C();
c1.display();
c1.show();
c1.callme();
c1.call();
}
}
OUT-PUT:
This is in display method
This is in show method This
is in callme methodThis is in
call method
(iii) Third form of interface implementation:
interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("This is in B's method");
}
}
class C implements A
{
public void display()
{
System.out.println("This is C's method");
}
}
class interfacedemo
{
Upcasting
Therefore, if a superclass contains a method that is overridden by a subclass, then when different types
of objects are referred to through a superclass reference variable,different versions of the method
are executed. Here is an example that illustrates dynamic method dispatch:
for(int k=1;k<=10;k++)
{
sleep(3000); System.out.println("welcome");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class threaddemo
{
public static void main(String args[])
{
A a1=new A(); B
b1=new B(); C
c1=new C();
a1.start();
b1.start();
c1.start();
}
}
OUT-PUT:
good morning
hello
good morning
good morning
welcome hello
good morning
good morning
hello
good morning
welcome good
morninghello
good morning
good morning
welcome hello
good morning
hello welcome
hello welcome
hello hello
welcome
hello
welcome
welcome
welcome
welcome
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class isalivedemo
{
public static void main(String args[])
{
A a1=new A(); B
b1=new B(); C
c1=new C();
a1.start();
b1.start();
c1.start();
System.out.println(a1.isAlive());
System.out.println(b1.isAlive());
System.out.println(c1.isAlive()); try
{
a1.join();
b1.join();
c1.join();
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(a1.isAlive());
System.out.println(b1.isAlive());
System.out.println(c1.isAlive());
}
}
OUT-PUT:
true good morning
true hello
true welcome
good morning hello
good morning hello
hello welcome
good morning hello
welcome welcome
good morning hello
hello hello
good morning welcome
good morning welcome
welcome welcome
hello welcome
good morning false
good morning false
hello false
good morning
welcome
Things to remember:
1. We can use wait() and notify() method to implement inter-thread communication in Java.Not just one
or two threads but multiple threads can communicate to each other by using these methods.
2. Always call wait(), notify() and notifyAll() methods from synchronized method orsynchronized
block otherwise JVM will throw IllegalMonitorStateException.
3. Always call wait and notify method from a loop and never from if() block, because looptest waiting
condition before and after sleeping and handles notification even if waiting for the condition is
not changed.
4. Always call wait in shared object e.g. shared queue in this example.
5. Prefer notifyAll() over notify() method due to reasons given in this article
EXPERIMENT – 12
a) Illustration of class path:
AIM: To write a JAVA program, illustrate class pathimport
java.net.URL;
import java.net.URLClassLoader;
public class App
{
public static void main(String[] args)
{
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();URL[] urls =
((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< urls.length; i++)
{
System.out.println(urls[i].getFile());
}
}
}
OUT-PUT:
E:/java%20work/
Step-3:
In Environment Variables click New in System variables
It displays the following “New System Variable” dialog box.
Step-4:
Now type variable name as a path and then variable value as
c:\SOURCE-CODE Files\java\jdk1.5.0_10\bin;
Step-5:
Click OK
package mypack;
public class box
{
public int l=10,b=20;
public void display()
{
System.out.println(l); System.out.println(b);
}
}
3. Create sub directory with a name same that of package name under the current workingdirectory by
as follows. d:\>md mypack
4. Under this subdirectory store the above SOURCE-CODE with a file name “box.java”.
import mypack.box;
class packagedemo
{
public static void main(String args[])
{
box b1=new box();
b1.display();
}
}
3. Now compile the above SOURCE-CODE in the current working directory d:\
javac packagedemo.java
4. Execute the above SOURCE-CODE in current working directory
java packagedemo
OUT-PUT:
10
20
EXPERIMENT - 13
a) Paint like Paint Brush in Applet:
AIM: To write a JAVA program to paint like paint brush in applet.
SOURCE-CODE:
import java.applet.*; import
java.awt.*; import
java.awt.event.*;
//<applet code="paintdemo" width="800" height="500"></applet>
public class paintdemo extends Applet implements MouseMotionListener
{
int w, h;
Image i;
Graphics g1;
public void init()
{
w = getSize().width; h = getSize().height;i =
createImage( w, h );
g1 = i.getGraphics();
g1.setColor( Color.white ); g1.fillRect( 0, 0, w, h ); g1.setColor( Color.red );i =
createImage( w, h );
g1 = i.getGraphics();
g1.setColor( Color.white ); g1.fillRect( 0, 0, w, h ); g1.setColor( Color.blue );
addMouseMotionListener( this );
}
public void mouseMoved( MouseEvent e ) { }public
void mouseDragged( MouseEvent me )
{
int x = me.getX(); int y = me.getY();
g1.fillOval(x-10,y-10,20,20); repaint();
me.consume();
}
public void update( Graphics g )
{
g.drawImage( i, 0, 0, this );
}
public void paint( Graphics g )
{
update(g);
}
}
OUT-PUT:
b) Display Analog Clock using Applet:
AIM: To write a JAVA program to display analog clock using Applet.
SOURCE-CODE:
import java.util.*;
import java.text.*;
import java.applet.*;
import java.awt.*;
//<applet code="clockdemo" width="550" height="250"></appletpublic class
clockdemo extends Applet implements Runnable
{
int h=0, m=0, s=0;
String str=""; int wt, ht; Thread thr=null; boolean b;public void
init()
{
wt=getSize().width; ht=getSize().height;
}
public void start()
{
if (thr==null)
{
thr=new Thread(this);
b=false;
thr.start();
}
else
{
if(b)
{
b=false;
synchronized(this)
{
notify();
}
}
}
}
public void stop()
{
b=true;
}
public void run()
{
try
{
while(true)
{
Calendar clndr=Calendar.getInstance();
h=clndr.get(Calendar.HOUR_OF_DAY);
if(h>12)h-=12;
m=clndr.get(Calendar.MINUTE); s=clndr.get(Calendar.SECOND);
SimpleDateFormat frmatter=new SimpleDateFormat("hh:mm:ss",
Locale.getDefault());
Date d=clndr.getTime(); str=frmatter.format(d);if(b)
{
synchronized (this)
{
while(b)
{
wait();
}
}
}
repaint();
thr.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
void drawHand(double angle, int radius, Graphics grp)
{
angle-=0.5*Math.PI;
int a=(int)(radius*Math.cos(angle)); int b=(int)(radius*Math.sin(angle));
grp.drawLine(wt/2,ht/2,wt/2+a,ht/2+b);
}
void drawWedge(double angle,int radius, Graphics grp)
{
angle-=0.5*Math.PI;
int a=(int)(radius*Math.cos(angle)); int b=(int)(radius*Math.sin(angle));angle+=2*Math.PI/3;
int a2=(int)(5*Math.cos(angle)); int b2=(int)(5*Math.sin(angle));
angle+=2*Math.PI/3;
int a3=(int)(5*Math.cos(angle)); int b3=(int)(5*Math.sin(angle));
grp.drawLine(wt/2+a2, ht/2+b2,wt/2+a,ht/2+b); grp.drawLine(wt/2+a3,
ht/2+b3,wt/2+a,ht/2+b); grp.drawLine(wt/2+a2, ht/2+b2,wt/2+a3,ht/2+b3);
}
public void paint(Graphics grp)
{
grp.setColor(Color.gray);
drawWedge(2*Math.PI*h/12,wt/5,grp); drawWedge(2*Math.PI*m/60,wt/3,grp);
drawHand(2*Math.PI*s/60,wt/2,grp);
}
}
OUT-PUT: