Java Lab Manual JNTUK II-I
Java Lab Manual JNTUK II-I
Prepared by
Professor
Course Objectives:
The aim of this lab is to
Course Outcomes:
By the end of the course student will be able to write java program for
Evaluate default value of all primitive data type, Operations, Expressions, Control-
flow, Strings
Determine Class, Objects, Methods, Inheritance, Exception, Runtime Polymorphism,
User defined Exception handling mechanism
Illustrating simple inheritance, multi-level inheritance, Exception handling
mechanism
Construct Threads, Event Handling, implement packages, developing applets
Exercise - 1 (Basics)
a) Write a JAVA program to display default value of all primitive data type of JAVA
b) 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.
c) 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 and print back the speed
of qualifying racers.
Exercise - 2 (Operations, Expressions, Control-flow, Strings)
a) Write a JAVA program to search for an element in a given list of elements using binary
search mechanism.
b) Write a JAVA program to sort for an element in a given list of elements using bubble sort
c) Write a JAVA program to sort for an element in a given list of elements using merge sort.
d) Write a JAVA program using StringBuffer to delete, remove character.
Exercise - 3 (Class, Objects)
a) Write a JAVA program to implement class mechanism. Create a class, methods and
invoke them inside main method.
b) Write a JAVA program to implement constructor.
Exercise - 4 (Methods)
a) Write a JAVA program to implement constructor overloading.
b) Write a JAVA program implement method overloading.
Exercise - 5 (Inheritance)
a) Write a JAVA program to implement Single Inheritance
b) Write a JAVA program to implement multi level Inheritance
c) Write a java program for abstract class to find areas of different shapes
Exercise - 6 (Inheritance - Continued)
a) Write a JAVA program give example for “super” keyword.
b) Write a JAVA program to implement Interface. What kind of Inheritance can be
achieved?
Exercise - 7 (Exception)
a) Write a JAVA program that describes exception handling mechanism
b) Write a JAVA program Illustrating Multiple catch clauses.
Exercise – 8 (Runtime Polymorphism)
a) Write a JAVA program that implements Runtime polymorphism
b) Write a Case study on run time polymorphism, inheritance that implements in above
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
problem
Exercise – 9 (User defined Exception)
a) Write a JAVA program for creation of Illustrating throw
b) Write a JAVA program for creation of Illustrating finally
c) Write a JAVA program for creation of Java Built-in Exceptions
d) d)Write a JAVA program for creation of User Defined Exception
Exercise – 10 (Threads)
a) 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)
b) Write a program illustrating isAlive and join ()
c) Write a Program illustrating Daemon Threads.
Exercise - 11 (Threads continuity)
a) Write a JAVA program Producer Consumer Problem
b) Write a case study on thread Synchronization after solving the above producer
consumer problem
Exercise – 12 (Packages)
a) Write a JAVA program illustrate class path
b) Write a case study on including in class path in your os environment of your package.
c) Write a JAVA program that import and use the defined your package in the previous
Problem
Exercise - 13 (Applet)
a) Write a JAVA program to paint like paint brush in applet.
b) Write a JAVA program to display analog clock using Applet.
c) Write a JAVA program to create different shapes and fill colors using Applet.
Exercise - 14 (Event Handling)
a) Write a JAVA program that display the x and y position of the cursor movement
using Mouse.
b) Write a JAVA program that identifies key-up key-down event user entering text in a
Applet.
LAB INTRODUCTION
Exercise - 1 (Basics)
1 a) Write a JAVA program to display default value of all primitive data type of JAVA
Aim: A JAVA program to display default values of all primitive data types in JAVA.
Program:
class Exe1a
{
static boolean b;
static byte by;
static short s;
static int i;
static long li;
static char c;
static float f;
static double d;
static String str;
public static void main (String args[])
{
[Link]("Boolean="+b);
[Link]("Byte="+by);
[Link]("Short="+s);
[Link]("Int="+i);
[Link]("Long="+li);
[Link]("Char="+c);
[Link]("Float="+f);
[Link]("Double="+d);
[Link]("String="+str);
}
}
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe1a
Boolean=false
Byte=0
Short=0
Int=0
Long=0
Char=
Float=0.0
Double=0.0
String=null
D:\DNRCET>
1 b) 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.
Aim: 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.
Program:-
import [Link].*;
class Exe1b
{
public static void main(String args[]) throws IOException
{
int a,b,c,d;
double r1,r2;
DataInputStream dis=new DataInputStream([Link]);
[Link]("Enter a,b and c values");
a=[Link]([Link]());
b=[Link]([Link]());
c=[Link]([Link]());
d=b*b-4*a*c;
if(d==0)
{
r1=r2=(double)-b/(2*a);
[Link]("Root1 is "+r1+" Root2 is "+r2);
}
else if(d>0)
{
r1=(-b+[Link](d))/(2*a);
r2=(-[Link](d))/(2*a);
[Link]("Root1 is "+r1+" Root2 is "+r2);
}
else
{
[Link]("Roots are imaginary");
}
}
}
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe1b
Enter a,b and c values
9
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
1
5
Roots are imaginary
D:\DNRCET>
1c) 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 and print back
the speed of qualifying racers.
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 and print back the speed of
qualifying racers.
Program:-
import [Link].*;
class racedemo
{
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:");
}
}
Output:
Enter speed of first racer:
4.5
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
Aim: a).A JAVA program to search for an element in a given list of elements using binary
Search mechanism.
Program:
import [Link];
class A
{
int binarysearch(int L[],int n,int k)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(L[mid]==k)
{
return mid;
}
else if(L[mid]>k)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return -1;
}
}
class Exe2a
{
public static void main(String args[])
{
int L[]=new int[20];
int n,k,pos,i;
A obj=new A();
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe2a
Enter n value
5
Enter 5 elements
1 2 3 4 5
Enter searching element
3
Element found at 3 location
D:\DNRCET>java Exe2a
Enter n value
4
Enter 4 elements
10 9 8 7
Enter searching element
5
Searching element not found in the list
D:\DNRCET>
2. b). Write a JAVA program to sort for an element in a given list of elements using bubble sort
Aim: b). A JAVA program to sort for an element in a given list of elements using bubble sort
Program:
import [Link];
class Exe2b
{
public static void main(String []args)
{
int n,t,i,j;
Scanner s = new Scanner([Link]);
[Link]("How many numbers are to be sorted:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter " + n + " integers");
for (i = 0; i < n; i++)
a[i] = [Link]();
for (i = 0; i < ( n - 1 ); i++) {
}
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe2b
How many numbers are to be sorted:
6
Enter 6 integers
1 9 2 8 5 3
After after sorting:
1 2 3 5 8 9
D:\DNRCET>
2.c) Write a JAVA program to sort for an element in a given list of elements using merge sort.
Aim:- A JAVA program to sort for an element in a given list of elements using merge sort.
Program:-
import [Link].*;
class Exe2c
{
private int[] a;
private int[] tempar;
private int length;
public static void main(String args[])
{
int[] a = {6,1,3,4,2,5};
[Link]("array before sorting is ");
for(int i=0;i < [Link];i++)
[Link]("\t"+a[i]);
Exe2c obj = new Exe2c();
[Link](a);
[Link]("\n array after sorting is ");
for(int i=0;i < [Link];i++)
{
[Link]("\t"+a[i]);
}
}
void sort(int a[])
{
this.a = a;
[Link] = [Link];
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe2c
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
Program:-
class Exe2d
{
public static void main(String arg[])
{
StringBuffer s = new StringBuffer("Kumar");
[Link](s);
[Link](1, 3);
[Link](s);
[Link](0);
[Link](s);
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe2d
Kumar
Kar
ar
D:\DNRCET>
Aim:-A JAVA program to implement class mechanism. – Create a class, methods and invoke
them inside main method.
Program:
public class Exe3a
{
private static void call()
{
[Link]("World");
}
public static void main(String[] args)
{
[Link]("Hello");
call();
}
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe3a
HelloWorld
D:\DNRCET>
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe3b
Constructor called
D:\DNRCET>
Exercise - 4 (Methods)
Program:
class Employee
{
int age;
String name;
Employee()
{
age =20;
name="Harish";
}
Employee(int age,String name)
{
[Link] =age;
[Link]=name;
}
public void disp()
{
[Link]("Name:"+name+"\t"+ "Age:"+age);
}
}
class Exe4a
{
public static void main(String args[])
{
Employee e1 = new Employee();
Employee e2 = new Employee(15,"Sirish");
[Link]();
[Link]();
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe4a
Name:Harish Age:20
Name:Sirish Age:15
D:\DNRCET>
4.b). Write a JAVA program implement method overloading.
Program:
class Exe4b
{
int n1;
int n2;
Exe4b()
{
n1 = 10;
n2 = 20;
}
void square()
{
[Link]("The Square is " + n1 * n2);
}
void square(int p1)
{
n1 = p1;
[Link]("The Square is " + n1 * n2);
}
void square(int p1, int p2)
{
n1 = p1;
n2 = p2;
[Link]("The Square is " + n1 * n2);
}
public static void main(String args[])
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
{
Exe4b obj = new Exe4b();
[Link](); //call non parameterise method
D:\DNRCET>java Exe4b
The Square is 200
The Square is 80
The Square is 56
D:\DNRCET>
Exercise - 5 (Inheritance)
5. a). Write a JAVA program to implement Single Inheritance
{
[Link]("Subtraction is= " +(a-b));
}
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
}
class Exe5a
{
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe5a
Addition is= 15
Subtraction is= 5
D:\DNRCET>
5 b) Write a JAVA program to implement multi level Inheritance
5. c). Write a java program for abstract class to find areas of different shapes
Aim:- a java program for abstract class to find areas of different shapes
Program:-
//Program for abstract class to find areas of different shapes
import [Link];
abstract class Calcarea
{
abstract void findTriangle(double b, double h);
abstract void findRectangle(double l, double b);
abstract void findSquare(double s);
abstract void findCircle(double r);
}
class Exe5c
{
public static void main(String args[])
{
double l, b, h, r, s;
findArea area = new findArea();
Scanner obj = new Scanner([Link]);
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe5c
D:\DNRCET>
Program:
class A
{
int a=10;
A()
{
[Link]("This is default constructor of A");
}
A(int a)
{
[Link]("This is parameterised constructor of A "+a);
}
void display()
{
[Link]("This is display method in A");
}
}
class B extends A
{
int a=20;
B()
{
[Link]("This is default constructor of B");
}
B(int a)
{
super(6);
//this();
[Link]("This is parameterised constructor of B i.e addition
"+(a+this.a+super.a));
[Link]();
[Link]();
}
void display()
{
[Link]("This is display method in B");
}
}
class Exe6a
{
public static void main(String args[])
{
B obj=new B();
B obj2=new B(9);
}
}
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe6a
This is default constructor of A
This is default constructor of B
This is parameterised constructor of A 6
This is parameterised constructor of B i.e addition 39
This is display method in A
D:\DNRCET>
6 b). Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?
Aim:- A JAVA program to implement Interface. What kind of Inheritance can be achieved?
Program:
interface add{
int addition(int i,int j);
}
interface sub{
int substraction(int i,int j);
}
D:\DNRCET>
{
[Link]("Arithmetic exception occoured: ");
}
finally
{
[Link]("This is finally block");
}
result=n*n;
[Link]("Square of given number is "+result);
}
}
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe7a
Arithmetic exception occurred:
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe7b
Arithmetic Exception found
End of the Program
D:\DNRCET>
Program:
class Human{
//Overridden method
public void eat()
{
[Link]("Human is eating");
}
}
class Exe8a extends Human
{
//Overriding method
public void eat()
{
[Link]("Boy is eating");
}
public static void main( String args[])
{
Exe8a obj = new Exe8a();
//This will call the child class version of eat()
[Link]();
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe8a
Boy is eating
D:\DNRCET>
8. b). Write a Case study on run time polymorphism, inheritance that implements in above
problem
Aim:- A Case study on run time polymorphism, inheritance that implements in above problem
Program:
class Exe9a
{
static void validate(int age) throws VoteException
{
if(age < 18)
throw new VoteException("Candidate is not eligible to vote");
else
[Link]("Welcome to Vote");
}
public static void main(String args[])
{
try
{
validate(13);
}
catch(Exception e)
{
[Link]("Exception occured: "+e);
}
[Link]("End of the program");
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe9a
Exception occured: VoteException: Candidate is not eligible to vote
End of the program
D:\DNRCET>java Exe9a
Welcome to Vote
End of the program
D:\DNRCET>
Program:-
class Exe9b
{
public static void main(String args[]){
try{
[Link]("First statement of try block");
int num=45/0;
[Link](num);
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("ArrayIndexOutOfBoundsException");
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Exception");
}
finally
{
[Link]("finally block");
}
[Link]("Out of try-catch-finally block");
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe9b
First statement of try block
Arithmetic Exception
finally block
Out of try-catch-finally block
D:\DNRCET>
Program:-
// Java program to demonstrate String Index Out Of Bounds Exception
class Exe9c
{
public static void main(String args[])
{
try
{
String a = "This is built-in exception"; // length is 26
char c = [Link](26); // accessing 27th element
[Link](c);
}
catch (StringIndexOutOfBoundsException e)
{
[Link]("String Index Out Of Bounds Exception");
}
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe9c
String Index Out Of Bounds Exception
D:\DNRCET>
Program:
//Program for creation of user defined exception
import [Link];
class OddNumberException extends Exception
{
OddNumberException(String s)
{
super(s);
}
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
}
class Exe9d
{
public static void main(String[] args)
{
int num;
Scanner S = new Scanner([Link]);
[Link]("Enter any number : ");
num = [Link]();
//num = [Link]([Link]());
try
{
if(num%2 != 0)
throw new OddNumberException("Odd number exception");
else
[Link](" " + num + " is an even number");
}
catch(OddNumberException Ex)
{
[Link]("Error : " + [Link]());
}
[Link]("End of program");
}
}
Output:
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe9d
Enter any number : 456
456 is an even number
End of program
D:\DNRCET>java Exe9d
Enter any number : 237
Error : Odd number exception
End of program
D:\DNRCET>
Exercise – 10 (Threads)
10 a.i). 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
Aim:- 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
Program:-
//Program for Multithreading by extending Thread Class
class ChildThread extends Thread
{
Thread t;
ChildThread(String name)
{
t = new Thread(this, name);
[Link]();
}
public void run()
{
for(int i=1;i<=5;i++)
{
try
{
if([Link]().equals("First Thread"))
{
[Link](1000);
[Link]([Link]()+": Good Morning");
}
else if([Link]().equals("Second Thread"))
{
[Link](2000);
[Link]([Link]()+": Hello");
}
else
{
[Link](3000);
[Link]([Link]()+": Welcome");
}
}
catch(InterruptedException e)
{
[Link]([Link]()+" is interrupted");
}
}
}
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
}
class Exe10ai
{
public static void main(String args[])
{
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe10ai
First Thread: Good Morning
Second Thread: Hello
First Thread: Good Morning
Third Thread: Welcome
First Thread: Good Morning
Second Thread: Hello
First Thread: Good Morning
First Thread: Good Morning
Third Thread: Welcome
Second Thread: Hello
Second Thread: Hello
Third Thread: Welcome
Second Thread: Hello
Third Thread: Welcome
10 [Link]). Write a JAVA program that creates threads by implementing Runnable [Link]
thread display “Good Morning “every 1 sec, the second thread displays “Hello “every 2 seconds
and the third display “Welcome” every 3 seconds
Aim:-A JAVA program that creates threads by implementing Runnable [Link] thread
display “Good Morning “every 1 sec, the second thread displays “Hello “every 2 seconds and the
third display “Welcome” every 3 seconds
Program:-
//Program for Multithreading by implementing Runnable interface
}
class Exe10aii
{
public static void main(String args[])
{
ChildThread one = new ChildThread("First Thread");
ChildThread two = new ChildThread("Second Thread");
ChildThread three = new ChildThread("Third Thread");
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe10aii
First Thread: Good Morning
Second Thread: Hello
First Thread: Good Morning
Third Thread: Welcome
First Thread: Good Morning
Second Thread: Hello
First Thread: Good Morning
First Thread: Good Morning
Third Thread: Welcome
Second Thread: Hello
Second Thread: Hello
Third Thread: Welcome
Second Thread: Hello
Third Thread: Welcome
Third Thread: Welcome
D:\DNRCET>
Program:-
//import [Link].*;
public class Exe10b implements Runnable {
public void run() {
Thread t = [Link]();
// tests if this thread is alive
[Link]("status = " + [Link]());
}
public static void main(String args[]) throws Exception {
Thread t = new Thread(new Exe10b());
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe10b
status = true
status = false
D:\DNRCET>
Program:-
// Program for illustrating Daemon Threads
class Daemonthread extends Thread
{
public void run()
{
if([Link]().isDaemon())
[Link]("Daemon Thread");
else
[Link]("User Thread");
}
public static void main(String args[])
{
Daemonthread t1= new Daemonthread();
Daemonthread t2= new Daemonthread();
Daemonthread t3= new Daemonthread();
[Link](true);
[Link]();
[Link]();
[Link]();
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Daemonthread
Daemon Thread
User Thread
User Thread
D:\DNRCET>
Program:-
// Java program to implement solution of producer consumer problem
import [Link];
public class Exe11a
{
public static void main(String[] args) throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable()
{
public void run()
{
try
{
[Link]();
}
catch(InterruptedException e)
{
[Link]();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable()
{
public void run()
{
try
{
[Link]();
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
}
catch(InterruptedException e)
{
[Link]();
}
}
});
// Start both threads
[Link]();
[Link]();
// t1 finishes before t2
[Link]();
[Link]();
}
// This class has a list, producer (adds items to list and consumber (removes items).
public static class PC
{
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 1;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// producer thread waits while list is full
while ([Link]()==capacity)
wait();
[Link]("Producer produced-" + value);
// to insert the jobs in the list
[Link](value++);
// notifies the consumer thread that now it can start consuming
notify();
// makes the working of program easier to understand
[Link](1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list is empty
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
while ([Link]()==0)
wait();
//to retrive the first job in the list
int val = [Link]();
[Link]("Consumer consumed-" + val);
// Wake up producer thread
notify();
[Link](1000);
}
}
}
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java Exe11a
Producer produced-0
Consumer consumed-0
Producer produced-1
Consumer consumed-1
Producer produced-2
Consumer consumed-2
Producer produced-3
Consumer consumed-3
Producer produced-4
Consumer consumed-4
Producer produced-5
Consumer consumed-5
Producer produced-6
Consumer consumed-6
Producer produced-7
Consumer consumed-7
Producer produced-8
Consumer consumed-8
Producer produced-9
Consumer consumed-9
Producer produced-10
Consumer consumed-10
D:\DNRCET>
11 b).Write a case study on thread Synchronization after solving the above producer consumer
problem
Aim:- A case study on thread Synchronization after solving the above producer consumer
problem
sleep() at the end of both methods just make the output of program run in step wise
manner and not display everything all at once so that you can see what actually is
happening in the program.
Exercise – 12 (Packages)
12 a). Write a JAVA program illustrate class path
Program:-
package mypackage;
class Shape
{
int d1,d2;
Shape(int x,int y)
{
d1=x;
d2=y;
}
}
public class Rectangle extends Shape
{
public Rectangle(int x,int y)
{
super(x,y);
}
public void area()
{
[Link]("Area:"+ (d1*d2));
}
}
import mypackage.*;
class ShapeDemo1
{
public static void main(String args[])
{
Rectangle r = new Rectangle(20,30);
[Link]();
}
}
Output:-
C:\Users\admin>d:
D:\>cd DNRCET
D:\DNRCET>cd mypackage
D:\DNRCET\mypackage>javac [Link]
D:\DNRCET\mypackage>cd..
D:\DNRCET>javac [Link]
D:\DNRCET>java ShapeDemo1
Area:600
D:\DNRCET>
b). Write a case study on including in class path in your os environment of your package.
Aim:-
Program:-
COPEING THE PATH OF JAVA:-
First of all go to MYCOMPUTER and go to the drive where the java is installed. In that go to
c). Write a JAVA program that import and use the defined your package in the previous
Problem
Aim:- a JAVA program that import and use the defined your package in the previous
Problem
Program:-
import [Link];
class ShapeDemo2
{
public static void main(String args[])
{
Rectangle r = new Rectangle(20,30);
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
[Link]();
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java ShapeDemo2
Area:600
D:\DNRCET>
class ShapeDemo3
{
public static void main(String args[])
{
[Link] r = new [Link](20,30);
[Link]();
}
}
Output:-
D:\DNRCET>javac [Link]
D:\DNRCET>java ShapeDemo3
Area:600
Exercise - 13 (Applet)
13 a).Write a JAVA program to paint like paint brush in applet.
Program:-
/* JAVA program to paint like paint brush in applet*/
import [Link].*;
import [Link].*;
import [Link].*;
public class Exe13a extends Applet implements MouseMotionListener
{
public void init()
{
addMouseMotionListener(this);
setBackground([Link]);
}
public void mouseDragged(MouseEvent me){
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
Graphics g=getGraphics();
[Link]([Link]);
[Link]([Link](),[Link](),5,5);
}
public void mouseMoved(MouseEvent me){}
}
/*
<applet code="[Link]" width="300" height="300">
</applet>
*/
Output:-
Program:-
Thread t = null;
boolean threadSuspended;
int hours=0, minutes=0, seconds=0;
String timeString = "";
SimpleDateFormat formatter
= new SimpleDateFormat( "hh:mm:ss", [Link]() );
Date date = [Link]();
timeString = [Link]( date );
while ( threadSuspended ) {
wait();
}
}
}
repaint();
[Link]( 1000 ); // interval specified in milliseconds
}
}
catch (Exception e) { }
}
Output:-
c). Write a JAVA program to create different shapes and fill colors using Applet.
Aim:- A JAVA program to create different shapes and fill colors using Applet
Program:-
import [Link].*;
import [Link].*;
public class Exe13c extends Applet
{
[Link]([Link]);
[Link](20,110,60,30,5,5);
[Link]([Link]);
[Link]("Different shapes ",65,180);
[Link](230,10,200,150);
[Link]([Link]);
[Link](245,25,100,100);
}
}
/*<applet code="Exe13c"width=500 height=400></applet>*/
Output:-
Aim:- A JAVA program that display the x and y position of the cursor movement using
Mouse.
Program:-
//java program that display the x and y position of the cursor movement using mouse
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="Exe14a" width=300 height=300>
</applet>*/
public class Exe14a extends Applet implements MouseListener,MouseMotionListener
{
String msg=" ";
int mousex=0,mousey=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mousex=0;
mousey=10;
msg="MouseClicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mousex=0;
mousey=10;
msg="MouseEntered";
repaint();
}
public void mouseExited(MouseEvent me)
{
mousex=0;
mousey=10;
msg="MouseExited";
repaint();
}
public void mousePressed(MouseEvent me)
{
mousex=0;
mousey=10;
msg="down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mousex=[Link]();
mousey=[Link]();
msg="up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mousex=[Link]();
mousey=[Link]();
msg="*";
showStatus("Dragging mouse at:"+mousex+","+mousey);
repaint();
}
public void mouseMoved(MouseEvent me)
{
mousex=[Link]();
mousey=[Link]();
showStatus("Moving mouse at:"+mousex+","+mousey);
}
public void paint(Graphics g)
{
[Link](msg,mousex,mousey);
}
}
Output:-
14 b).Write a JAVA program that identifies key-up key-down event user entering text in a
Applet.
Aim:- A JAVA program that identifies key-up key-down event user entering text in a
Applet.
Program:-
//java program that identifies key_up key_down event user entering text in a Applet
import [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="Exe14b"width=300 height=400>
</applet>
*/
public class Exe14b extends Applet implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground([Link]);
setForeground([Link]);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=[Link]();
switch(key)
{
case KeyEvent.VK_UP:showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:showStatus("Move to right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("key up");
}
public void keyTyped(KeyEvent k)
{
msg+=[Link]();
repaint();
}
public void paint(Graphics g)
{
[Link](msg,X,Y);
}
}
Output:-
Exercise - 15 (Swings)
a).Write a JAVA program to build a Calculator in Swings
Program:-
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Calculator(){}
public static void main(String[] args) {
createWindow();
}
createUI(frame);
[Link](200, 200);
[Link](null);
[Link](true);
}
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);[Link](calculator);
[Link](calculator);
[Link] = [Link];
[Link] = 0; [Link] = 0; [Link](button1, gbc);
[Link] = 1; [Link] = 0; [Link](button2, gbc);
[Link] = 2; [Link] = 0; [Link](button3, gbc);
[Link] = 3; [Link] = 0; [Link](buttonPlus, gbc);
[Link] = 0; [Link] = 1; [Link](button4, gbc);
[Link] = 1; [Link] = 1; [Link](button5, gbc);
[Link] = 2; [Link] = 1; [Link](button6, gbc);
[Link] = 3; [Link] = 1; [Link](buttonMinus, gbc);
[Link] = 0; [Link] = 2; [Link](button7, gbc);
[Link] = 1; [Link] = 2; [Link](button8, gbc);
[Link] = 2; [Link] = 2; [Link](button9, gbc);
[Link] = 3; [Link] = 2; [Link](buttonDivide, gbc);
[Link] = 0; [Link] = 3; [Link](buttonDot, gbc);
[Link] = 1; [Link] = 3; [Link](button0, gbc);
[Link] = 2; [Link] = 3; [Link](buttonClear, gbc);
[Link] = 3; [Link] = 3; [Link](buttonMultiply, gbc);
[Link] = 3;
}else {
[Link]([Link]() + command);
}
}
if ([Link]("+"))
result = ([Link](operand1) + [Link](operand2));
else if ([Link]("-"))
result = ([Link](operand1) - [Link](operand2));
else if ([Link]("/"))
result = ([Link](operand1) / [Link](operand2));
else
result = ([Link](operand1) * [Link](operand2));
return operand1 + operator + operand2 + "=" +result;
}
}
Output:-
15 b). Write a JAVA program to display the digital watch in swing tutorial.
Program:-
//java program to display the digital watch using swing
import [Link].*;
import java. awt.*;
import [Link].*;
import [Link].*;
public class Exe15b implements Runnable
{
JFrame f;
Thread t=null;
int hours=0,minutes=0,seconds=0;
String time="";
JButton b;
Exe15b()
{
f=new JFrame();
t=new Thread(this);
[Link]();
b=new JButton();
[Link](100,100,100,50);
[Link](b);
[Link](300,400);
[Link](null);
[Link](true);
[Link](f.EXIT_ON_CLOSE);
}
public void run()
{
try
{
while(true)
{
Calendar cal=[Link]();
hours=[Link](Calendar.HOUR_OF_DAY);
if(hours>12)hours-=12;
minutes=[Link]([Link]);
seconds=[Link]([Link]);
SimpleDateFormat obj=new SimpleDateFormat("hh:mm: a");
Date date=[Link]();
time=[Link](date);
printTime();
}
}
catch(Exception e)
{
}
}
public void printTime()
{
[Link](time);
}
public static void main(String args[])
{
new Exe15b();
}
}
Output:-
Aim:- A JAVA program that to create a single ball bouncing inside a JPanel.
Program:-
//java program to create a single ball bouncing inside a JPanel
import [Link].*;
import [Link].*;
public class Exe16a extends JPanel
{
int width;
int height;
float radius=20;
float diameter=radius*2;
float X=radius+50;
float Y=radius+20;
float dx=3;
float dy=3;
public Exe16a()
{
Thread thread=new Thread()
{
public void run()
{
while(true)
{
width=getWidth();
height=getHeight();
X=X+dx;
Y=Y+dy;
if(X-radius<0)
{
dx=-dx;
X=radius;
}
else if(X+radius>width)
{
dx=-dx;
X=width-radius;
}
if(Y-radius<0)
{
dy=-dy;
Y=radius;
}
else if(Y+radius>height)
{
dy=dy;
Y=height-radius;
}
repaint();
try
{
[Link](50);
}
catch(InterruptedException ex)
{
}
}
}
};
[Link]();
}
public void paintComponent(Graphics g)
{
[Link](g);
[Link]([Link]);
[Link]((int)(X-radius),(int)(Y-radius),(int)diameter,(int)diameter);
}
public static void main(String args[])
{
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
[Link](true);
JFrame f=new JFrame("Bouncing Ball");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300,200);
[Link](new Exe16a());
[Link](true);
}
}
Output:-
16 b). Write a JAVA program JTree as displaying a real tree upside down
Program:-
//java program for displaying a real tree using Jtree
import [Link].*;
import [Link];
public class Exe16b
{
JFrame f;
Exe16b()
{
f=new JFrame();
DefaultMutableTreeNode cse=new DefaultMutableTreeNode("CSE");
DefaultMutableTreeNode gents=new DefaultMutableTreeNode("Gents staff");
DefaultMutableTreeNode ladies=new DefaultMutableTreeNode("Ladies staff");
[Link](gents);
[Link](ladies);
DefaultMutableTreeNode s1=new DefaultMutableTreeNode("B V RAM KUMAR");
DefaultMutableTreeNode s2=new DefaultMutableTreeNode("A P V D L KUMAR");
[Link](s1);
[Link](s2);
DefaultMutableTreeNode l1=new DefaultMutableTreeNode("JYOSHNA");
DNR COLLEGE OF ENGINEERING AND TECHNOLOGY
JAVA Programming Lab
Output:-
ADD ON EXPEREMENTS
1.
2.
INTERNAL HOD