Java Lab Manual
Java Lab Manual
INSTITUTE OF TECHNOLOGY
(APPROVED BY AICTE NEW DELHI, AFFLIATED TO
JNTUK, KAKINADA) CHOWDAVARAM, GUNTUR-19
Roll No:
CERTIFICATE
This is to Certify that the Bonafide Record of the Laboratory Work done by
Mr/Ms……………………………………………………………………………………………
experiments in ……………………………………………………….…………………………
PAGE
EX.
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE
.
PAGE
EX.
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE
.
PAGE
EX.
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE
.
Exercise:
Date: Roll No:
Exercise:
Date: Roll No:
EXERCISE-1(a)
1(a).Write a JAVA program to display default value of all primitive data type of
JAVA
Program:
import [Link].*;
class Ex1a
{
static int i;
static double d;
static float f;
static boolean b;
static String s;
public static void main(String args[])
{
[Link]("int default value = "+ i);
[Link]("double default value = "+ d);
[Link]("float default value = "+ f);
[Link]("boolean default value = "+ b);
[Link]("String default value = "+ s);
}
}
EXERCISE-1(b)
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.
Prpgram:
public class Ex1b
{
public static void main(String[] args)
{
double a = 1, b = 5, c = 6;
double r1, r2;
double det = b * b - 4 * a * c;
if (det > 0) {
else if (det == 0) {
r1 = r2 = -b / (2 * a);
[Link]("root1 = root2 = %.2f;", r1);
}
else
{
double real = -b / (2 * a);
double imag = [Link](-det) / (2 * a);
[Link]("root1 = %.2f+%.2fi", real, imag);
[Link]("\nroot2 = %.2f-%.2fi", real, imag);
}
}
}
EXERCISE-1()
1(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.
Program:
import [Link];
class Ex1c
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int speed[]=new int[5];
for(int i=0;i<5;i++)
{
[Link]("\nEnter the speed of Racer-"+(i+1)+": ");
speed[i]=[Link]();
}
int sum=0;
for(int i=0;i<5;i++)
sum+=speed[i];
double avg=sum/5;
[Link]("\nThe speed of qualifying race is: "+avg);
[Link]("\nThe speed of qualifying racers is: ");
for(int i=0;i<5;i++)
{
if(speed[i]>=avg)
[Link]("\nRacer-"+i+": "+speed[i]);
}
}
}
EXERCISE-2(a)
EXERCISE-2(b)
2(b)//Write a JAVA program to sort for an element in a given list of elements using
bubble sort
Program:
class Ex2b
{
public static void main(String[] args)
{
int arr[] ={10,5,8,2,12,3,9,1};
int n = [Link];
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(arr[j-1] > arr[j])
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
}
EXERCISE-2(c)
2(c)Write a JAVA program to sort for an element in a given list of elements using
merge sort.
Program:
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
i++;
k++;
}
EXERCISE-2(d)
sb = [Link](2,6);
[Link]("After deleting: " + sb);
sb = [Link](0, 7);
[Link]("After deleting: " + sb);
}
}
EXERCISE-3(a)
// Public method
public void Hai()
{
[Link]("Hai KHIT");
}
// Main method
public static void main(String[] args)
{
Hello(); // Call the static method
// Hai(); //This would compile an error
EXERCISE-3(B)
// default constructor
Ex3b()
{
rollno = 501;
name = "CSE";
[Link]("default constructor called");
}
Ex3b(int i, String s)
{
rollno = i;
name = s;
[Link]("parameterized constructor called");
}
void display(){ [Link](rollno+" "+name); }
EXERCISE-4(a)
// default constructor
ex4a()
{
rollno = 501;
name = "CSE";
}
ex4a(int i, String s)
{
rollno = i;
name = s;
}
ex4a(int i, String s, int j)
{
rollno = i;
name = s;
age = j;
}
void display()
{
[Link]("rollno"+"\t"+"name"+"\t"+"age");
[Link](rollno+"\t"+name+"\t"+age); }
EXERCISE-4(b)
{
[Link](c + " "+num);
}
}
class Ex4b
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
[Link]('a');
[Link]('a',10);
}
}
EXERCISE-5(a)
}
}
class SubClass extends SuperClass
{
void sub(int a,int b)
{
int c=a-b;
[Link]("Subtraction is "+c);
}
}
class Ex5a
{
public static void main( String args[] )
{
SuperClass s1=new SuperClass();
[Link](5,6);
SubClass s2=new SubClass();
[Link](15,10);
[Link](15,27);
}
}
EXERCISE-5(b)
}
class B extends A
{
void showB()
{
[Link]("Method B");
}
}
class C extends B
{
void showC()
{
[Link]("Method C");
}
}
class Ex5b
{
public static void main(String[] args)
{
C c1=new C();
[Link]();
[Link]();
[Link]();
}
}
EXERCISE-5(c)
5c) Write a java program for abstract class to find areas of different shapes
Program:
abstract class Shape
{
abstract void findCircle(double r);
abstract void findTriangle(double b, double h);
abstract void findRectangle(double w, double h);
}
class Ex5c extends Shape
{
void findCircle(double r)
{
double a=3.14*r*r;
[Link]("Area of Circle = "+a);
}
void findTriangle(double b, double h)
{
double a=0.5*b*h;
[Link]("Area of Triangle = "+a);
}
void findRectangle(double w, double h)
{
double a=w*h;
[Link]("Area of Rectangle = "+a);
}
public static void main(String[] args)
{
Ex5c as=new Ex5c();
[Link](4.3);
[Link](6.1,4.5);
[Link](5.5,7.2);
}
}
EXERCISE-6(a)
}
class Two extends One
{
int i=20;
void show()
{
[Link]("Sub Class Method i: "+i);
[Link]();
[Link]("Super Class Variable i: "+super.i);
}
}
class Demo
{
public static void main( String args[] )
{
Two t=new Two();
[Link]();
}
}
EXERCISE-6(b)
6b). Write a JAVA program to implement Interface. What kind of Inheritance can
be achieved?
Program:
interface Father
{
double HT=6.2;
void height();
}
interface Mother
{
double HT=5.8;
void color();
}
class Child implements Father, Mother
{
public void height()
{
double ht=([Link]+[Link])/2;
EXERCISE-7(a)
int c=a/b;
[Link](“The Division is “+c);
}
catch(ArithmeticException ae)
{
[Link](“Division with zero is not
possible”);
}
finally{
[Link](“LOGOUT”);
}
}
}
EXERCISE-7(b)
{
try
{
[Link]("WELCOME");
Scanner sc=new Scanner([Link]);
[Link]("Enter a value: ");
int a=[Link]();
[Link]("Enter b value: ");
int b=[Link]();
int c=a/b;
[Link]("The Division is "+c);
}
catch(InputMismatchException ae)
{
[Link]("Wrong Input");
}
catch(ArithmeticException ae)
{
[Link]("Division with zero is not possible");
}
finally
{
[Link]("LOGOUT");
}
}
}
EXERCIS-8(a)
EXERCISE-8(b)
8b). Write a Case study on run time polymorphism, inheritance that implements in
above problem Runtime Polymorphism (or Dynamic polymorphism)
In this previous example we have three classes Bank, SBI and AXIS. Bank is a parent
class and SBI and AXIS are child classes. The child classes are overriding the method
interest() of parent class. In this previous example we have child class object assigned to
the parent class reference so in order to determine which method would be called, the type
of the object would be determined at run-time. It is the type of object that determines
which version of the method would be called (not the type of reference).
EXERCISE-9(a)
EXERCISE-9(b)
EXERCISE-10(a)
10(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)
Program:
class A extends Thread
{
synchronized public void run()
{
try {
int i=0;
while (i<5) {
sleep(1000);
[Link]("Good morning");
i++;
}
}
catch(Exception e)
{}
}
}
class B extends Thread
{
synchronized public void run()
{
try {
int i=0;
while (i<5) {
sleep(2000);
[Link]("Hello");
i++;
}
}
catch(Exception e)
{}
}
}
class C extends Thread
{
synchronized public void run()
{
try {
int i=0;
EXERCISE-10(b)
{
[Link]("run started ");
try {
[Link](500);
}catch(InterruptedException ie){ }
[Link]("run ended ");
}
public static void main(String[] args)
{
Ex10b t1=new Ex10b();
Ex10b t2=new Ex10b();
[Link]();
[Link]([Link]());
[Link]([Link]());
// try{
// [Link](); //Waiting for t1 to finish
// }catch(InterruptedException ie){}
[Link]();
[Link]([Link]());
[Link]([Link]());
}
}
EXERCISE-10(c)
EXERCISE-11(a)
int n;
boolean valueSet = false;
synchronized int get()
{
while(!valueSet)
try
{
wait();
}
catch(InterruptedException e) { }
[Link]("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n)
{
while(valueSet)
try {
wait();
}
catch(InterruptedException e) { }
this.n = n;
valueSet = true;
[Link]("Put: " + n);
notify();
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
newThread(this, "Producer").start();
}
public void run() {
int i = 0;
while(true) {
[Link](i++);
}
}
[Link]();
[Link](1000);}
} catch(Exception e) { }
}
}
class Th9 {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
[Link]("Press Control-C to stop.");
}
}
EXERCISE-11(b)
b) Write a case study on thread Synchronization after solving the above producer
consumer
problem
Synchronization:
When thread is already acting on an object, preventing any other thread from
acting on the same object is called Thread Synchronization or thread safe. Thread
synchronization is recommended when multiple threads are used on the same object.
In the above Producer-Consumer program we are synchronization on
StringBuffer Object. In the Producer Thread [Link]()method is sending a
notification to the Consumer thread that the StringBuffer object sb is available, and
it can be used now.
Meanwhile, what the Consumer thread is doing? It is waiting for the notification
that the StringBuffer object sb (of Producer class) is available. Here, there is no need of
using sleep() method to go into sleep for some time wait() method stops waiting as
soon as it receives the notification. So there is no time delay to receive the data from the
Producer.
ECERCISE-12(a)
12(a). How does the Java run-time system know where to look for packages that
you create?
• The answer has three parts. First, by default, the Java run-time system uses the
current working directory as its starting point. Thus, if your package is in a
subdirectory of the current directory, it will be found.
• Second, you can specify a directory path or paths by setting the CLASSPATH
environmental variable.
• Third, you can use the -classpath option with java and javac to specify the path to
your classes.
Consider an example
This example has two packages. Remember that one package named KHIT held
at E: drive and another named CSE at F: drive of computer system.
public class A
{
public void call()
{
[Link]("Hi I am Class A from KHIT package (E: Drive)");
}
}
EXERCISE-12(b)
12(b). Write a JAVA program that import and use the defined your package in the
previous Problem
Program:
package pack2;
class Addition1
{
public void add(int a,int b)
{
int c=a+b;
[Link](“The sum is “+c);
}
}
Program:
Import pack2.Addition1;
class Demopack
{
public static void main (String args[])
{
Addition a1=new Addition();
[Link](10,30);
}
}
EXERCISE-13(a)
/*
<applet code="Ex13a" width=600 height=600>
</applet>
*/
EXERCISE-13(b)
/*
<applet code="Ex13b" width=600 height=600>
</applet>
*/
@Override
public void init()
{
// Applet window size & color
[Link](new Dimension(800, 400));
setBackground(new Color(50, 50, 50));
new Thread() {
@Override
public void run()
{
while (true) {
repaint();
delayAnimation();
}
}
}.start();
}
// 12 hour format
if (hour > 12) {
hour -= 12;
}
// Labeling
[Link]([Link]);
[Link]("12", 390, 120);
[Link]("9", 310, 200);
[Link]("6", 400, 290);
[Link]("3", 480, 200);
EXERCISE-13(c)
13(c). Write a JAVA program to create different shapes and fill colors using Applet.
Program:
import [Link].*;
import [Link].*;
/*
<applet code="Ex13c" width=800 height=400>
</applet>
*/
[Link](x-r,y-r,100,100);
[Link]([Link]); //Fill the yellow color in circle
[Link]( x-r,y-r, 100, 100 );
[Link]([Link]);
[Link]("Circle",275,100);
[Link](400,50,200,100);
[Link]([Link]); //Fill the yellow color in rectangel
[Link]( 400, 50, 200, 100 );
[Link]([Link]);
[Link]("Rectangel",450,100);
}
}
EXERCISE-14(a)
14(a). Write a JAVA program that display the x and y position of the cursor
movement using Mouse.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="Ex14a" width=800 height=400>
</applet>
*/
EXERCISE-14(b)
14(b). Write a JAVA program that identifies key-up key-down event user entering text in a Applet.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="Ex14b" width=600 height=400>
</applet>
*/