r20 Java Lab Manual
r20 Java Lab Manual
[Link]
{
public static void main(String[] args)
{
float s1,s2,s3,s4,s5,average;
Scanner s = new Scanner([Link]);
[Link]("Enter speed of first racer:");
s1 = [Link]();
[Link]("Enter speed of second racer:");
s2 = [Link]();
[Link]("Enter speed of third racer:");
s3 = [Link]();
[Link]("Enter speed of fourth racer:");
s4 = [Link]();
[Link]("Enter speed of fifth racer:");
s5 = [Link]();
average=(s1+s2+s3+s4+s5)/5;
if(s1>average)
[Link]("First racer is qualify racer:");
else if(s2>average)
[Link]("Second racer is qualify racer:");
else if(s3>average)
[Link]("Third racer is qualify racer:");
else if(s4>average)
[Link]("Fourth racer is qualify racer:");
else if(s5>average)
[Link]("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
Second racer is qualify racer:
d) A case study
AIM: A case study on public static void main(250 words)
Case study:
The SOURCE-CODE structure of a simple java SOURCE-CODE is given below with
different steps
Step-1: Click start+run and then type notepad in run dialog box and click OK. It displays
Notepad.
[Link]
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-CODE
as “[Link]” in a current working directory.
class example
{
public static void main(String args[])
{
[Link](“Welcome”);
}
}
Step-4 (Compilation): To compile the program type the following in current working
directory and then click enter.
c:\xxxx >javac [Link]
Step-5 (Execution): To run the program type the following in current working directory and
then 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 file
can be compiled but it cannot be executed. That is when execution it gives the
following error
[Link]
EXPERIMENT - 2
a) Implementation of Binary search mechanism
AIM: To write a JAVA program to search for an element in a given list of elements using
binary search mechanism
SOURCE-CODE:
import [Link];
class binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last, middle;
int a[ ]=new int[20];
Scanner s = new Scanner([Link]);
[Link]("Enter total number of elements:");
n = [Link]();
[Link]("Enter elements in sorted order:");
for (i = 0; i < n; i++)
a[i] = [Link]();
[Link]("Enter the search value:");
num = [Link]();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( a[middle] < num )
first = middle + 1;
else if ( a[middle] == num )
{
[Link]("number found");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
[Link]( " Number is not found");
}
}
OUT-PUT:
Enter total number of elements:
5
Enter elements:
24689
Enter the search value:
8
[Link]
number found
b) Bubble sort
AIM: To write a JAVA program to sort for an element in a given list of elements using
bubble sort
SOURCE-CODE:
import [Link];
class bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner([Link]);
[Link]("Enter total number of elements:");
n = [Link]();
[Link]("Enter elements:");
for (i = 0; i < n; i++)
a[i] = [Link]();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
[Link]("The sorted elements are:");
for(i=0;i<n;i++)
[Link]("\t"+a[i]);
}
}
OUT-PUT:
Enter total number of elements:
10
Enter elements:
3257689140
The sorted elements are:
0123456789
c) Merge sort:
AIM: To write a JAVA program to sort for an element in a given list of elements using
merge sort
SOURCE-CODE:
import [Link].*;
class mergedemo
[Link]
{
public static void main(String args[])
{
int n1,n2,i,j,k;
int a[ ]=new int[20];
int b[ ]=new int[20];
int c[ ]=new int[20];
Scanner s = new Scanner([Link]);
[Link]("Enter number of elements in first array:");
n1 = [Link]();
[Link]("Enter sorted elements of first array:");
for (i = 0; i < n1; i++)
a[i] = [Link]();
[Link]("Enter number of elements in second array:");
n2 = [Link]();
[Link]("Enter sorted elements of second array:");
for (j = 0; j < n2; j++)
b[j] = [Link]();
i = 0;
j = 0;
k = 0;
while((i < n1) && (j <n2))
{
if(a[i] > b[j])
c[k++] = b[j++];
else
c[k++] = a[i++];
}
while(i < n1)
c[k++] = a[i++];
while(j < n2)
c[k++] = b[j++];
[Link]("After merging the elements are:\n");
for(i = 0; i < (n1 + n2); i++)
[Link]("\t"+c[i]);
}
}
OUT-PUT:
Enter number of elements in first array:
6
Enter elements of first array:
8 9 12 13 15 18
Enter number of elements in second array:
5
Enter elements of second array:
6 7 10 11 20
After merging the elements are:
6 7 8 9 10 11 12 13 15 18 20
[Link]
d) Implementing StringBuffer
AIM: To write a JAVA program using StringBuffer to delete, remove character
SOURCE-CODE:
class stringbufferdemo
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello World");
[Link](0,6);
[Link](sb1);
StringBuffer sb2 = new StringBuffer("Some Content");
[Link](sb2);
[Link](0, [Link]());
[Link](sb2);
StringBuffer sb3 = new StringBuffer("Hello World");
[Link](0);
[Link](sb3);
}
}
OUT-PUT:
World
Some Content
ello World
[Link]
EXPERIMENT - 3
a) Implementing Class & Objects
AIM: To write a JAVA program to implement class mechanism. – Create a class, methods
and invoke them inside main method
SOURCE-CODE:
[Link] return type and without parameter-list:
class A
{
int l=10,b=20;
void display()
{
[Link](l);
[Link](b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
[Link]();
}
}
OUT-PUT:
10
20
[Link] return type and with parameter-list:
class A
{
void display(int l,int b)
{
[Link](l);
[Link](b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
[Link](10,20);
}
}
OUT-PUT:
10
20
3. return type and without parameter-list
class A
{
[Link]
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=[Link]();
[Link]("The area is: "+r);
}
}
OUT-PUT:
The area is:200
[Link] type and with parameter-list:
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=[Link](10,20);
[Link](“The area is:”+r);
}
}
OUT-PUT:
The area is:200
b) Implementing Constructor
AIM: To write a JAVAto implement constructor
SOURCE-CODEs:
(i)A constructor with no parameters:
class A
{
int l,b;
A()
{
l=10;
b=20;
}
[Link]
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A();
int r=[Link]();
[Link]("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=[Link]();
[Link]("The area is: "+r);
}
}
OUT-PUT:
The area is:200
[Link]
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=[Link]();
[Link]("The area is: "+r1);
A a2=new A(30,40);
int r2=[Link]();
[Link]("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;
}
[Link]
int area(int l,int b)
{
return l*b;
}
}
class overmethoddemo
{
public static void main(String args[])
{
A a1=new A();
int r1=[Link]();
[Link]("The area is: "+r1);
int r2=[Link](5,20);
[Link]("The area is: "+r2);
}
}
OUT-PUT:
The area is: 200
The area is: 100
[Link]
EXPERIMENT - 5
a)Implementing Single Inheritance
AIM: To write a JAVA program to implement Single Inheritance
SOURCE-CODE:
class A
{
A()
{
[Link]("Inside A's Constructor");
}
}
class B extends A
{
B()
{
[Link]("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()
{
[Link]("Inside A's Constructor");
}
}
class B extends A
{
B()
{
[Link]("Inside B's Constructor");
}
}
class C extends B
{
C()
[Link]
{
[Link]("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
{
[Link]
public static void main(String[] args)
{
rectangle r1=new rectangle();
triangle t1=new triangle();
square s1=new square();
[Link]("The area of rectangle is: "+[Link]());
[Link]("The area of triangle is: "+[Link]());
[Link]("The area of square is: "+[Link]());
}
}
OUT-PUT:
The area of rectangle is: 31.25
The area of triangle is: 13.65
The area of square is: 26.0
[Link]
EXPERIMENT - 6
a)super keyword implementation
AIM: Write a JAVA program give example for “super” keyword
SOURCE-CODEs:
(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=[Link]();
[Link]("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;
}
}
[Link]
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 superdemo
{
public static void main(String args[])
{
B b1=new B(30,20,30);
int r=[Link]();
[Link]("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()
{
[Link]("B's method");
}
}
class C extends B
{
public void callme()
{
[Link]("C's method");
}
}
class interfacedemo
{
[Link]
public static void main(String args[])
{
C c1=new C();
[Link]();
[Link]();
}
}
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()
{
[Link]("This is in callme method");
}
}
class B extends A implements E
{
public void display()
{
[Link]("This is in display method");
}
public void show()
{
[Link]("This is in show method");
}
}
class C extends B
{
void call()
{
[Link]("This is in call method");
}
}
class interfacedemo
{
public static void main(String args[])
{
[Link]
C c1=new C();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
This is in display method
This is in show method
This is in callme method
This is in call method
(iii) Third form of interface implementation
interface A
{
void display();
}
class B implements A
{
public void display()
{
[Link]("This is in B's method");
}
}
class C implements A
{
public void display()
{
[Link]("This is C's method");
}
}
class interfacedemo
{
public static void main(String args[])
{
B b1=new B();
C c1=new C();
[Link]();
[Link]();
}
}
OUT-PUT:
This is in B's method
This is C's method
(iv) Fourth form of interface implementation
interface A
{
void display();
[Link]
}
interface B
{
void callme();
}
interface C extends A,B
{
void call();
}
class D implements C
{
public void display()
{
[Link]("interface A");
}
public void callme()
{
[Link]("interface B");
}
public void call()
{
[Link]("interface C");
}
}
class interfacedemo
{
public static void main(String args[])
{
D d1=new D();
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
interface A
interface B
interface C
[Link]
EXPERIMENT - 7
a) Exception handling mechanism
AIM: To write a JAVA program that describes exception handling mechanism
SOURCE-CODE:
Usage of Exception Handling:
class trydemo
{
public static void main(String args[])
{
try
{
int a=10,b=0;
int c=a/b;
[Link](c);
}
catch(ArithmeticException e)
{
[Link](e);
}
[Link]("After the catch statement");
}
}
OUT-PUT:
[Link]: / by zero
After the catch statement
b) Illustrating multiple catch classes
SOURCE-CODE:
AIM: To write a JAVA program Illustrating Multiple catch clauses
class multitrydemo
{
public static void main(String args[])
{
try
{
int a=10,b=5;
int c=a/b;
int d[]={0,1};
[Link](d[10]);
[Link](c);
}
catch(ArithmeticException e)
{
[Link](e);
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link](e);
}
[Link]
[Link]("After the catch statement");
}
}
OUT-PUT:
[Link]: 10
After the catch statement
[Link]
EXPERIMENT – 8
a)Runtime Polymorphism
SOURCE-CODE:
AIM: To write a JAVA program that implements Runtime polymorphism
class A
{
void display()
{
[Link]("Inside A class");
}
}
class B extends A
{
void display()
{
[Link]("Inside B class");
}
}
class C extends A
{
void display()
{
[Link]("Inside C class");
}
}
class runtimedemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
A ref;
ref=c1;
[Link]();
ref=b1;
[Link]();
ref=a1;
[Link]();
}
}
OUT-PUT:
Inside C class
Inside B class
Inside A class
b)Case study on Runtime Polymorphism
AIM: To write a Case study on run time polymorphism, inheritance that implements in
above problem
[Link]
Dynamic method dispatch is the mechanism by which a call to an overridden method is
resolved at run time, rather than compile time.
When an overridden method is called through a superclass reference, Java determines
which version(superclass/subclasses) of that method is to be executed based upon the
type of the object being referred to at the time the call occurs. Thus, this
determination is made at run time.
At run-time, it depends on the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be
executed
A superclass reference variable can refer to a subclass object. This is also known as
upcasting. Java uses this fact to resolve calls to overridden methods at run time.
Upcasting
[Link]
EXPERIMENT – 9
a)creation of illustrating throw
SOURCE-CODE:
AIM: To write a JAVA program for creation of Illustrating throw
class throwdemo
{
public static void main(String args[])
{
try
{
throw new NullPointerException("demo");
}
catch(NullPointerException e)
{
[Link](e);
}
}
}
OUT-PUT:
[Link]: demo
b)creation of illustrating finally
AIM: To write a JAVA program for creation of Illustrating finally
SOURCE-CODE(i):
class finallydemo
{
public static void main(String args[])
{
try
{
int a=10,b=0;
int c=a/b;
[Link](c);
}
catch(ArithmeticException e)
{
[Link](e);
}
finally
{
[Link]("This is inside finally block");
}
}
}
OUT-PUT:
[Link]: / by zero
This is inside finally block
SOURCE-CODE(ii):
[Link]
class finallydemo
{
public static void main(String args[])
{
try
{
int a=10,b=5;
int c=a/b;
[Link](c);
}
catch(ArithmeticException e)
{
[Link](e);
}
finally
{
[Link]("This is inside finally block");
}
}
}
OUT-PUT:
2
This is inside finally block
c)creation of Java Built-in-Exceptions
AIM: To write a JAVA program for creation of Java Built-in Exceptions
SOURCE-CODEs:
(i) Arithmetic exception
class arithmeticdemo
{
public static void main(String args[])
{
try
{
int a = 10, b = 0;
int c = a/b;
[Link] (c);
}
catch(ArithmeticException e)
{
[Link] (e);
}
}
}
OUT-PUT:
[Link]: / by zero
(ii)NullPointer Exception
class nullpointerdemo
[Link]
{
public static void main(String args[])
{
try
{
String a = null;
[Link]([Link](0));
}
catch(NullPointerException e)
{
[Link](e);
}
}
}
OUT-PUT:
[Link]
(iii)StringIndexOutOfBound Exception
class stringbounddemo
{
public static void main(String args[])
{
try
{
String a = "This is like chipping ";
char c = [Link](24);
[Link](c);
}
catch(StringIndexOutOfBoundsException e)
{
[Link](e);
}
}
}
OUT-PUT:
[Link]:
String index out of range: 24
(iv)FileNotFound Exception
import [Link].*;
class filenotfounddemo
{
public static void main(String args[])
{
try
{
File file = new File("E://[Link]");
FileReader fr = new FileReader(file);
}
[Link]
catch (FileNotFoundException e)
{
[Link](e);
}
}
}
OUT-PUT:
[Link]: E:\[Link] (The system cannot find the file specified)
(v)NumberFormat Exception
class numberformatdemo
{
public static void main(String args[])
{
try
{
int num = [Link] ("akki") ;
[Link](num);
}
catch(NumberFormatException e)
{
[Link](e);
}
}
}
OUT-PUT:
[Link]: For input string: "akki"
(vi)ArrayIndexOutOfBounds Exception
class arraybounddemo
{
public static void main(String args[])
{
try
{
int a[] = new int[5];
a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link] (e);
}
}
}
OUT-PUT:
[Link]: 6
d)creation of User Defined Exception
AIM: To write a JAVA program for creation of User Defined Exception
SOURCE-CODE:
class A extends Exception
[Link]
{
A(String s1)
{
super(s1);
}
}
class owndemo
{
public static void main(String args[])
{
try
{
throw new A("demo ");
}
catch(Exception e)
{
[Link](e);
}
}
}
OUT-PUT:
A: demo
[Link]
EXPERIMENT – 10
a)Extending Thread class
AIM: To write a JAVA program that creates threads by extending Thread class .First
thread display “Good Morning “every 1 sec, the second thread displays “Hello “every 2
seconds and the third display “Welcome” every 3 seconds ,(Repeat the same by
implementing Runnable)
SOURCE-CODEs:
(i)Creating multiple threads using Thread class
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000);
[Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000);
[Link]("hello");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class C extends Thread
{
public void run()
{
try
[Link]
{
for(int k=1;k<=10;k++)
{
sleep(3000);
[Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class threaddemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
good morning
hello
good morning
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
hello
good morning
good morning
welcome
hello
good morning
hello
welcome
hello
welcome
[Link]
hello
hello
welcome
hello
welcome
welcome
welcome
welcome
(ii)Creating multiple threads using Runnable interface
class A implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
[Link](1000);
[Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class B implements Runnable
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
[Link](2000);
[Link]("hello");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class C implements Runnable
{
public void run()
[Link]
{
try
{
for(int k=1;k<=10;k++)
{
[Link](3000);
[Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class runnabledemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
Thread t1=new Thread(a1);
Thread t2=new Thread(b1);
Thread t3=new Thread(c1);
[Link]();
[Link]();
[Link]();
}
}
OUT-PUT:
good morning
good morning
hello
good morning
welcome
good morning
hello
good morning
good morning
welcome
hello
good morning
good morning
hello
good morning
welcome
good morning
[Link]
hello
welcome
hello
hello
welcome
hello
welcome
hello
hello
welcome
welcome
welcome
welcome
(b)Implementing isAlive() and join()
AIM: To write a program illustrating isAlive and join ()
SOURCE-CODE:
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000);
[Link]("good morning");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000);
[Link]("hello");
}
}
catch(Exception e)
{
[Link]
[Link](e);
}
}
}
class C extends Thread
{
public void run()
{
try
{
for(int k=1;k<=10;k++)
{
sleep(3000);
[Link]("welcome");
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class isalivedemo
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
[Link]();
[Link]();
[Link]();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
try
{
[Link]();
[Link]();
[Link]();
}
catch(InterruptedException e)
{
[Link](e);
}
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]
}
}
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
[Link]
EXPERIMENT - 11
a)Producer-Consumer problem
AIM: Write a JAVA program Producer Consumer Problem
SOURCE-CODE:
class A
{
int n;
boolean b=false;
synchronized int get()
{
if(!b)
try
{
wait();
}
catch(Exception e)
{
[Link](e);
}
[Link]("Got:"+n);
b=false;
notify();
return n;
}
synchronized void put(int n)
{
if(b)
try
{
wait();
}
catch(Exception e)
{
[Link](e);
}
this.n=n;
b=true;
[Link]("Put:"+n);
notify();
}
}
class producer implements Runnable
{
A a1;
Thread t1;
producer(A a1)
{
[Link]
this.a1=a1;
t1=new Thread(this);
[Link]();
}
public void run()
{
for(int i=1;i<=10;i++)
{
[Link](i);
}
}
}
class consumer implements Runnable
{
A a1;
Thread t1;
consumer(A a1)
{
this.a1=a1;
t1=new Thread(this);
[Link]();
}
public void run()
{
for(int j=1;j<=10;j++)
{
[Link]();
}
}
}
class interdemo
{
public static void main(String args[])
{
A a1=new A();
producer p1=new producer(a1);
consumer c1=new consumer(a1);
}
}
OUT-PUT:
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
[Link]
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10
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 or
synchronized block otherwise JVM will throw IllegalMonitorStateException.
3. Always call wait and notify method from a loop and never from if() block, because loop
test 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
[Link]
EXPERIMENT – 12
a) Illustration of class path
AIM: To write a JAVA program, illustrate class path
import [Link];
import [Link];
public class App
{
public static void main(String[] args)
{
ClassLoader sysClassLoader = [Link]();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< [Link]; i++)
{
[Link](urls[i].getFile());
}
}
}
OUT-PUT:
E:/java%20work/
[Link]
Step-2:
In System Properties click Advanced and then click Environment Variables
It displays the following “Environment Variables” dialog.
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;
[Link]
public class box
{
public int l=10,b=20;
public void display()
{
[Link](l);
[Link](b);
}
}
3. Create sub directory with a name same that of package name under the current working
directory by as follows. d:\>md mypack
4. Under this subdirectory store the above SOURCE-CODE with a file name “[Link]”.
import [Link];
class packagedemo
{
public static void main(String args[])
{
box b1=new box();
[Link]();
}
}
3. Now compile the above SOURCE-CODE in the current working directory d:\
javac [Link]
4. Execute the above SOURCE-CODE in current working directory
java packagedemo
OUT-PUT:
10
20
[Link]
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 [Link].*;
import [Link].*;
import [Link].*;
//<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 = [Link]();
[Link]( [Link] ); [Link]( 0, 0, w, h ); [Link]( [Link] );
i = createImage( w, h );
g1 = [Link]();
[Link]( [Link] ); [Link]( 0, 0, w, h ); [Link]( [Link] );
addMouseMotionListener( this );
}
public void mouseMoved( MouseEvent e ) { }
public void mouseDragged( MouseEvent me )
{
int x = [Link](); int y = [Link]();
[Link](x-10,y-10,20,20);
repaint();
[Link]();
}
public void update( Graphics g )
{
[Link]( i, 0, 0, this );
}
public void paint( Graphics g )
{
update(g);
}
}
OUT-PUT:
[Link]
b) Display Analog Clock using Applet
AIM: To write a JAVA program to display analog clock using Applet.
SOURCE-CODE:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
//<applet code="clockdemo" width="550" height="250"></applet
public 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;
[Link]();
}
else
{
if(b)
{
b=false;
synchronized(this)
{
notify();
}
}
}
}
public void stop()
{
b=true;
}
public void run()
{
try
{
while(true)
{
Calendar clndr=[Link]();
h=[Link](Calendar.HOUR_OF_DAY);
[Link]
if(h>12)h-=12;
m=[Link]([Link]); s=[Link]([Link]);
SimpleDateFormat frmatter=new SimpleDateFormat("hh:mm:ss",
[Link]());
Date d=[Link](); str=[Link](d);
if(b)
{
synchronized (this)
{
while(b)
{
wait();
}
}
}
repaint();
[Link](1000);
}
}
catch(Exception e)
{
[Link](e);
}
}
void drawHand(double angle, int radius, Graphics grp)
{
angle-=0.5*[Link];
int a=(int)(radius*[Link](angle)); int b=(int)(radius*[Link](angle));
[Link](wt/2,ht/2,wt/2+a,ht/2+b);
}
void drawWedge(double angle,int radius, Graphics grp)
{
angle-=0.5*[Link];
int a=(int)(radius*[Link](angle)); int b=(int)(radius*[Link](angle));
angle+=2*[Link]/3;
int a2=(int)(5*[Link](angle)); int b2=(int)(5*[Link](angle));
angle+=2*[Link]/3;
int a3=(int)(5*[Link](angle)); int b3=(int)(5*[Link](angle));
[Link](wt/2+a2, ht/2+b2,wt/2+a,ht/2+b);
[Link](wt/2+a3, ht/2+b3,wt/2+a,ht/2+b);
[Link](wt/2+a2, ht/2+b2,wt/2+a3,ht/2+b3);
}
public void paint(Graphics grp)
{
[Link]([Link]);
drawWedge(2*[Link]*h/12,wt/5,grp); drawWedge(2*[Link]*m/60,wt/3,grp);
drawHand(2*[Link]*s/60,wt/2,grp);
}
}
[Link]
OUT-PUT:
[Link]
[Link]
EXPERIMENT - 14
a) Cursor movement using mouse
AIM: To write a JAVA program that display the x and y position of the cursor movement
using
Mouse.
SOURCE-CODE:
import [Link].*;
import [Link].*;
import [Link].*;
//<applet code="mouseevent" width=450 height=300></applet>
public class mouseevent extends Applet
implements MouseListener, MouseMotionListener
{
String s1=" ";
int x,y;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
x=100;
y=100;
s1="Mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
x=100;
y=200;
s1="Mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
x=100;
y=300;
s1="Mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
x=[Link]();
y=[Link]();
s1="Mouse Pressed";
repaint();
}
[Link]
public void mouseReleased(MouseEvent me)
{
x=[Link]();
y=[Link]();
s1="Mouse Realeased";
repaint();
}
public void mouseDragged(MouseEvent me)
{
x=[Link]();
y=[Link]();
s1="Mouse Dragged";
repaint();
}
public void mouseMoved(MouseEvent me)
{
x=[Link]();
y=[Link]();
s1="Mouse Moved";
repaint();
}
public void paint(Graphics g)
{
[Link](s1,x,y);
}
}
OUT-PUT:
[Link]
b) Key-up and Key-down event
AIM: To write a JAVA program that identifies key-up key-down event user entering text
in a Applet.
SOURCE-CODE:
import [Link].*;
import [Link].*;
import [Link].*;
//<applet code="keyevent" width=450 height=300></applet>
public class keyevent extends Applet implements KeyListener
{
String s1=" ";
int x,y;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
x=100;
y=200;
s1= "key pressed ";
repaint();
}
public void keyReleased(KeyEvent ke)
{
x=100;
y=400;
s1= "key Released ";
repaint();
}
public void keyTyped(KeyEvent ke)
{
s1=s1+[Link]();
repaint();
}
public void paint(Graphics g)
{
[Link](s1,x,y);
}
}
OUT-PUT:
[Link]
[Link]