Java Programming LabR23
Java Programming LabR23
II Year – I Semester L T P C
0 0 3 2
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.
Exercise - 2
b) Write a JAVA program to sort for an element in a given list of elements using
bubble sort
Exercise - 3
a) Write a JAVA program to implement class mechanism. – Create a class, methods and invoke them
inside main method.
Exercise - 4
c) Write a JAVA program for abstract class to find areas of different shapes
Exercise - 5
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 - 6
a) Write a JAVA program that describes exception handling mechanism
Exercise - 7
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)
Exercise – 8
a) Write a JAVA program that import and use the user defined packages
b) Write a program that creates a user interface to perform integer divisions. The user enters two numbers
in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when
the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a Number
Format Exception. If Num2 were Zero, the program would throw an Arithmetic Exception Display the
exception in a message dialog box.
Exercise – 9
a)Write a Java Program That works as a simple calculator using Grid layout to arrange buttons for the digits
and +, -, * % operations. Add a text filed to print the result.
II B.Tech I Sem CSE Java Lab Manual (R23)
Exercise - 1
a) Displaying default value of all primitive data types
Aim: To write a JAVA program to display default value of all primitive data type of JAVA
Program:
class defaultdemo
{
static byte b;
static short
s; static int i;
static long l;
static float f;
static double
d; static char
c;
static boolean bl;
public static void main(String[] args)
{
System.out.println("The default values of primitive data types
are:"); System.out.println("Byte :"+b);
System.out.println("Short :"+
s);
System.out.println("Int :"+i);
System.out.println("Long :"+l)
;
System.out.println("Float :"+f
);
System.out.println("Double :"
+d); System.out.println("Char
:"+c);
System.out.println("Boolean :
"+bl);
}
}
Output:
The default values of primitive data types are:
Byte :0
Short :0
Int :0
Long :0
Float :0.0
Double :0.0
Char :
Boolean :fal
se
II B.Tech I Sem CSE Java Lab Manual (R23)
Page 1
II B.Tech I Sem CSE Java Lab Manual (R23)
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.
Program:
import java.util.*;
class
quadraticdemo
{
public static void main(String[] args)
{
int a, b, c;
double r1, r2,
D;
Scanner s = new Scanner(System.in);
System.out.println("Given quadratic equation:ax^2 +
bx + c"); System.out.print("Enter a:");
a = s.nextInt();
System.out.print("Enter
b:"); b = s.nextInt();
System.out.print("Enter
c:"); c = s.nextInt();
D=b*b-4*a
* c; if(D > 0)
{
System.out.println("Roots are real and
unequal"); r1 = ( - b + Math.sqrt(D))/(2*a);
r2 = (-b - Math.sqrt(D))/(2*a);
System.out.println("First root
is:"+r1);
System.out.println("Second root
is:"+r2);
}
else if(D == 0)
{
System.out.println("Roots are real and
equal"); r1 = (-b+Math.sqrt(D))/(2*a);
System.out.println("Root:"+r1);
}
els
e
{ System.out.println("Roots are imaginary");
}
}
}
Output:
Given quadratic equation:ax^2
+ bx + c Enter a:2
Enter
b:3
Enter
c:1
Roots are real and
II B.Tech I Sem CSE Java Lab Manual (R23)
unequal First root is:-
0.5
Second root is:-1.0
II B.Tech I Sem CSE Java Lab Manual (R23)
c) 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 and
print back the speed of qualifying racers.
Program:
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:");
}
}
Output:
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:
II B.Tech I Sem CSE Java Lab Manual (R23)
Exercise - 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
Program:
import
java.util.Scanner;
class
binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last,
middle; int a[ ]=new
int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of
elements:"); n = s.nextInt();
System.out.println("Enter elements in sorted
order:"); for (i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.println("Enter the search
value:"); num = s.nextInt();
first = 0;
last = n -
1;
middle = (first +
last)/2; while( first
<= last )
{
if ( a[middle] <
num ) first =
middle + 1;
else if ( a[middle] == num )
{
System.out.println("number
found"); break;
}
els
e
{ last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println( " Number is not found");
}
}
Output:
Enter total number of
elements: 5
II B.Tech I Sem CSE Java Lab Manual (R23)
Enter elements:
24689
Enter the search
value: 8
number found
II B.Tech I Sem CSE Java Lab Manual (R23)
b) Bubble sort
Aim: To write a JAVA program to sort for an element in a given list of elements using bubble
sort
Program:
import
java.util.Scanner;
class bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of
elements:"); n = s.nextInt();
System.out.println("Enter
elements:"); for (i = 0; i < n; i+
+)
a[i] =
s.nextInt();
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]=te
mp;
}
}
}
System.out.println("The sorted elements are:"); for(i=0;i<n;i+
+)
System.out.print("\t"+a[i]);
}
}
Output:
Enter total number of elements:
10
Enter elements:
3257689140
The sorted elements are:
0 1 2 3 4 5 6 7 8 9
II B.Tech I Sem CSE Java Lab Manual (R23)
c) Merge sort:
Aim: To write a JAVA program to sort for an element in a given list of elements using merge
sort
Program:
import
java.util.*;
class
mergedemo
{
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(System.in);
System.out.println("Enter number of elements in first
array:"); n1 = s.nextInt();
System.out.println("Enter sorted elements of first
array:"); for (i = 0; i < n1; i++)
a[i] = s.nextInt();
System.out.println("Enter number of elements in second
array:"); n2 = s.nextInt();
System.out.println("Enter sorted elements of second
array:"); for (j = 0; j < n2; j++)
b[j] =
s.nextInt(); 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++];
System.out.println("After merging the elements
are:\n"); for(i = 0; i < (n1 + n2); i++)
System.out.print("\t"+c[i]);
}
}
Output:
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
II B.Tech I Sem CSE Java Lab Manual (R23)
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
II B.Tech I Sem CSE Java Lab Manual (R23)
d) Implementing StringBuffer
Aim: To write a JAVA program using StringBuffer to delete, remove character
Program:
class stringbufferdemo
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello
World"); sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some
Content"); System.out.println(sb2);
sb2.delete(0,
sb2.length());
System.out.println(sb2)
;
StringBuffer sb3 = new StringBuffer("Hello World");
sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
Output:
World
Some
Content ello
World
II B.Tech I Sem CSE Java Lab Manual (R23)
Exercise - 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
Programs:
1. no return type and without parameter-list:
class A
{
int
l=10,b=20;
void
display()
{
System.out.println
(l);
System.out.println
(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new
A();
} a1.display();
}
Output:
10
20
}
}
Output:
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);
}
II B.Tech I Sem CSE Java Lab Manual (R23)
}
Output:
The area is:200
II B.Tech I Sem CSE Java Lab Manual (R23)
Constructor Overloading
Aim: To write a JAVA program to implement constructor overloading
Program:
class A
{
int
l,b;
A()
{
l=10
;
b=2
0;
}
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);
}
}
Output:
The area is:
200 The area
is: 1200
II B.Tech I Sem CSE Java Lab Manual (R23)
Method Overloading
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);
}
}
Output:
The area is:
200 The area
is: 100
II B.Tech I Sem CSE Java Lab Manual (R23)
Exercise - 4
a)Implementing Single Inheritance
Program:
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();
}
}
Output:
Inside A's
Constructor Inside
B's Constructor
II B.Tech I Sem CSE Java Lab Manual (R23)
Inheritance Program:
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();
}
}
Output:
Inside A's
Constructor Inside
B's Constructor
Inside
C's
Constructor
II B.Tech I Sem CSE Java Lab Manual (R23)
c) Abstract Class
Aim: To write a java program for abstract class to find areas of different shapes
Program:
Output:
The area of rectangle is:
31.25 The area of triangle
II B.Tech I Sem CSE Java Lab Manual (R23)
is: 13.65 The area of
square is: 26.0
II B.Tech I Sem CSE Java Lab Manual (R23)
Exercise - 5
Programs:
(i) Using super to call super class constructor (Without parameters)
class A
{
int
l,b;
A()
{
l=10
;
b=2
0;
}
}
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);
}
}
Output:
The vol. is:6000
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=b1.volume();
System.out.println("The vol. is:
"+r);
}
}
Output:
The vol. is:18000
II B.Tech I Sem CSE Java Lab Manual (R23)
b) Implementing interface
Aim: To write a JAVA program to implement Interface.
Programs:
(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();
}
}
Output:
B's
method
C's
method
(Runtime Polymorphism)
a)Runtime Polymorphism
Program:
class A
{
void display()
{
System.out.println("Inside A class");
}
}
class B extends A
{
void display()
{
System.out.println("Inside B class");
}
}
class C extends A
{
void display()
{
System.out.println("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;
ref.display
(); ref=b1;
ref.display
(); ref=a1;
ref.display
();
}
}
Output:
Inside C
class Inside
B class
Inside A
class
II B.Tech I Sem CSE Java Lab Manual (R23)
Exercise - 6
a) Exception handling mechanism
Aim: To write a JAVA program that describes exception handling mechanism
Program:
Usage of Exception Handling:
class trydemo
{
public static void main(String args[])
{
tr
y
{ int
a=10,b=0;
int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
}
Output:
java.lang.ArithmeticException: /
by zero After the catch statement
II B.Tech I Sem CSE Java Lab Manual (R23)
Output:
java.lang.ArrayIndexOutOfBoundsExcepti
on: 10 After the catch statement
II B.Tech I Sem CSE Java Lab Manual (R23)
Program:
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
}
Output:
java.lang.NullPointerException: demo
II B.Tech I Sem CSE Java Lab Manual (R23)
b) creation of illustrating finally
}
}
}
Output:
java.lang.ArithmeticException: /
by zero This is inside finally block
Program(ii):
class finallydemo
{
public static void main(String args[])
{
tr
y
{ int
a=10,b=5;
int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finall
y
{ System.out.println("This is inside finally block");
}
}
}
Output:
2
II B.Tech I Sem CSE Java Lab Manual (R23)
This is inside finally block
II B.Tech I Sem CSE Java Lab Manual (R23)
Programs:
Output:
java.lang.ArithmeticException: / by zero
(ii)NullPointer Exception
class nullpointerdemo
{
public static void main(String args[])
{
tr
y
{ String a = null;
System.out.println(a.charAt(0));
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
}
Output:
java.lang.NullPointerException
(iii)StringIndexOutOfBound Exception
class stringbounddemo
{
public static void main(String args[])
{
try
II B.Tech I Sem CSE Java Lab Manual (R23)
{
II B.Tech I Sem CSE Java Lab Manual (R23)
String a = "This is like
chipping "; char c =
a.charAt(24);
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
Output:
java.lang.StringIndexOutOfBoundsException: String index out of range: 24
(iv)FileNotFound Exception
import java.io.*;
class filenotfounddemo
{
public static void main(String args[])
{
tr
y
{ File file = new File("E://file.txt");
FileReader fr = new
FileReader(file);
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
}
}
Output:
java.io.FileNotFoundException: E:\file.txt (The system cannot find the file specified)
(v)NumberFormat Exception
class numberformatdemo
{
public static void main(String args[])
{
tr
y
{ int num = Integer.parseInt
("akki") ;
System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
}
}
Output:
II B.Tech I Sem CSE Java Lab Manual (R23)
java.lang.NumberFormatException: For input string: "akki"
II B.Tech I Sem CSE Java Lab Manual (R23)
(vi)ArrayIndexOutOfBounds Exception
class arraybounddemo
{
public static void main(String args[])
{
tr
y
{ int a[] = new
int[5]; a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println (e);
}
}
}
Output:
java.lang.ArrayIndexOutOfBoundsException: 6
II B.Tech I Sem CSE Java Lab Manual (R23)
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
A: demo
II B.Tech I Sem CAI Java Lab Manual (R20)
Exercise – 7 (Threads)
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)
Programs:
(i) Creating multiple threads using Thread class
class A extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sleep(1000);
System.out.println("good
morning");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class B extends Thread
{
public void run()
{
try
{
for(int j=1;j<=10;j++)
{
sleep(2000);
System.out.println("hello");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
II B.Tech I Sem CAI Java Lab Manual (R20)
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();
}
}
Output:
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
hello
hello
welcome
hello
welcome
welcome
welcome
welcome
II B.Tech I Sem CAI Java Lab Manual (R20)
Output:
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
hello
welcome
hello
hello
welcom
e hello
welcom
II B.Tech I Sem CAI Java Lab Manual (R20)
e hello
hello
welcom
e
welcom
e
welcom
e
welcom
e
II B.Tech I Sem CAI Java Lab Manual (R20)
Program:
h
e
l
l
o
w
e
l
c
o
m
e
h
e
l
l
o
h
e
l
l
o
w
e
l
c
o
m
e
h
e
l
l
o
w
e
l
II B.Tech I Sem CAI Java Lab Manual (R20)
Program:
Output:
daemon thread
work user thread
work user thread
work
II B.Tech I Sem CAI Java Lab Manual (R20)
(Threads continuity)
a)Producer- Consumer problem
Aim: Write a JAVA program Producer Consumer Problem
Program:
class A
{
int n;
boolean b=false;
synchronized int
get()
{
if(!
b)
try
{ wait();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Got:"
+n); b=false;
notify();
return
n;
}
synchronized void put(int n)
{
if(b
)
try
{ wait();
}
catch(Exception e)
{
System.out.println(e);
}
this.n=
n;
b=true;
System.out.println("Put:"+n);
notify();
}
}
class producer implements Runnable
{
A a1;
Thread t1;
producer(A
a1)
{
this.a1=a1;
II B.Tech I Sem CAI Java Lab Manual (R20)
t1=new
Thread(this);
t1.start();
}
public void run()
II B.Tech I Sem CSE Java Lab Manual (R20)
{
for(int i=1;i<=10;i++)
{
a1.put(i);
}
}
}
❖ We can use wait, notify and notifyAll methods to communicate between threads in Java.
❖ For example, if we have two threads running in your program e.g.Producer and
Consumer then producer thread can communicate to the consumer that it can start
consuming now because there are items to consume in the queue.
❖ Similarly, a consumer thread can tell the producer that it can also start putting items
now because there is some space in the queue, which is created as a result of
consumption.
❖ A thread can use wait() method to pause and do nothing depending upon some
condition.
❖ For example, in the producer-consumer problem, producer thread should wait if the
queue is full and consumer thread should wait if the queue is empty.
❖ If some thread is waiting for some condition to become true, we can use notify
and notifyAll methods to inform them that condition is now changed and they
can wake up.
❖ Both notify() and notifyAll() method sends a notification but notify sends the
notification to only one of the waiting thread, no guarantee which thread will receive
notification and notifyAll() sends the notification to all threads.
Things to remember:
Exercise – 8 (Packages)
Output:
10
20
II B.Tech I Sem CSE Java Lab Manual (R20)
b) Write a program that creates a user interface to perform integer divisions. The user enters two
numbers in the textfields, Num1 and Num2. The division of Num1 and Num2 is displayed in the
Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program
would throw a Number Format Exception. If Num2 were Zero, the program would throw an
Arithmetic Exception Display the exception in a message dialog box.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
BuildGUI() {
actualWindow = new JFrame("Experiment 4");
container = new JPanel();
container.setLayout(new FlowLayout());
container.add(txt_num1);
container.add(txt_num2);
container.add(btn_div);
container.add(txt_result);
actualWindow.add(container);
actualWindow.setSize(300, 300);
actualWindow.setVisible(true);
}
II B.Tech I Sem CSE Java Lab Manual (R20)
}
II B.Tech I Sem CSE Java Lab Manual (R20)
II B.Tech I Sem CSE Java Lab Manual (R20)
II B.Tech I Sem CSE Java Lab Manual (R20)
Exercise – 9 Write a Java Program That works as a simple calculator using Grid layout to arrange
buttons for the digits and +, -, * % operations. Add a text filed to print the result.
Output: