JAVA Lab - Manual
JAVA Lab - Manual
PORBANDAR
affiliated to
Laboratory Manual
JAVA PROGRAMMING
(3350704)
D. E. Third Year (Semester– V)
Address: Bokhira,Porbandar
Subject: JAVA
Branch: Computer
Certificate
______________________ ___________________
GOVERNMENT POLYTECHNIC
PORBANDAR
M2: Foster close networking with the industry, alumni and the guardians.
COMPUTER ENGINEERING
DEPARTMENT
Course Outcomes:
CO1: Understand the use of OOPs concepts.
CO2: Apply basic building blocks of Java Programming Language and OOP
concepts in program.
Hardware Requirements:
Computer system with Intel i3 (or equivalent) processor and at least 2
GB of RAM.
Software Requirements:
JDK 1.2 or higher.
List of Practicals
Sr. Mapping Mapping with
Practical Title
No. with COs POs, PSOs
6 Write a program in Java to convert number into words & print it CO2 PO1,PSO2
Write a program in Java to develop user defined exception for PO1, PO3, PO4,
24 CO4
'Divide by Zero' error. PSO2
Write a program in Java to demonstrate multiple try block and PO1, PO3, PO4,
25 CO4
multiple catch exception PSO2
* Programs has
no errors or
errors are
minimal
INDEX
Staff PA
Sr. Page
Practical Date Signatur LA
No No LP
e
Install JDK, write a simple “Hello World” or similar java
1 program, compilation, debugging, executing using java 08/06/2021
compiler and interpreter.
2 Write a program in Java to generate first n prime numbers. 10/06/2021
Write a program in Java to find maximum of three
3 10/06/2021
numbers using conditional operator
Write a program in Java to find second maximum of n
4 15/06/2021
numbers without using arrays
Write a program in Java to reverse the digits of a number
5 17/06/2021
using while loop
Write a program in Java to convert number into words &
6 22/06/2021
print it
Write programs in Java to use Wrapper class of each
7 01/07/2021
primitive data types
8 Write a program in Java to multiply two matrix 06/07/2021
Write a static block which will be executed before main( )
9 06/07/2021
method in a class.
Write a program in Java to demonstrate use of this
10 keyword. Check whether this can access the private 15/07/2021
members of the class or not.
Write a program in Java to develop overloaded constructor.
11 Also develop the copy constructor to create a new object 20/07/2021
with the state of the existing object.
Write a program in Java to demonstrate the use of private
12 constructor and also write a method which will count the 22/07/2021
number of instances created using default constructor only.
Write a program in Java to demonstrate the use of 'final'
13 keyword in the field declaration. How it is accessed using 27/07/2021
the objects.
Develop minimum 4 program based on variation in
14 methods i.e. passing by value, passing by reference, 29/07/2021
returning values and returning objects from methods.
Write a program in Java to demonstrate single inheritance, 03/08/2021
15
multilevel inheritance and hierarchical inheritance.
Create a class to find out whether the given year is leap 05/08/2021
16
year or not. (Use inheritance for this program)
Write an application that illustrates how to access a hidden
variable. Class A declares a static variable x. The class B 10/08/2021
17
extends A and declares an instance variable x. display( )
method in B displays both of these variables.
18 Write a program in Java in which a subclass constructor 12/08/2021
invokes the constructor of the super class and instantiate
the values.
Write a program that illustrates interface inheritance.
Interface P12 inherits from both P1 and P2. Each interface
19 declares one constant and one method. The class Q 02/09/2021
implements P12. Instantiate Q and invoke each of its
methods. Each method displays one of the constants.
Write an application that illustrates method overriding in
20 the same package and different packages. Also demonstrate 07/09/2021
accessibility rules in inside and outside packages.
Describe abstract class called Shape which has three
subclasses say Triangle, Rectangle, Circle. Define one
method area()in the abstract class and override this area() 09/09/2021
21
in these three subclasses to calculate for specific object i.e.
area() of Triangle subclass should calculate area of triangle
etc. Same for Rectangle and Circle
Write a program in Java to demonstrate implementation of 14/09/2021
22
multiple inheritance using interfaces.
23 Write a program in Java to demonstrate use of final class. 14/09/2021
Write a program in Java to develop user defined exception 16/09/2021
24
for 'Divide by Zero' error.
Write a program in Java to demonstrate multiple try block 18/09/2021
25
and multiple catch exception
Write an small application in Java to develop Banking
Application in which user deposits the amount Rs 1000.00
26 and then start withdrawing of Rs 400.00, Rs 300.00 and it 21/09/2021
throws exception "Not Sufficient Fund" when user
withdraws Rs. 500 thereafter.
Write a program that executes two threads. One thread
displays “Thread1” every 2,000 milliseconds, and the other 23/09/2021
27
displays “Thread2” every 4,000 milliseconds. Create the
threads by extending the Thread class
Write a program that executes two threads. One thread will
28 print the even numbers and the another thread will print 24/09/2021
odd numbers from 1 to 50.
Write a program in Java to demonstrate use of
29 synchronization of threads when multiple threads are trying 28/09/2021
to update common variable.
Write a program in Java to create, write, modify, read 30/09/2021
30
operations on a Text file.
Page: JAVA (3350704)
Practical - 1
AIM:
Install JDK, write a simple “Hello World” or similar java program, compilation,
debugging, executing using java compiler and interpreter.
PROGRAM: /
Step 2: Next,
Download latest Java JDK for your version(32 or 64 bit) of java for
Windows.
Download latest Java JDK for your version(32 or 64 bit) of java for
Windows.
Step 3: Once the download is complete, run the exe for install JDK. Click
Next
The PATH variable gives the location of executables like javac, java etc. It
is possible to run a program without specifying the PATH but you will
need to give full path of executable like C:\Program Files\Java\
jdk1.8.0_131\bin\javac A.java instead of simple javac A.java
The CLASSPATH variable gives the location of the Library Files.
Let's look into the steps to set the PATH and CLASSPATH
Step 1: Right Click on the My Computer and Select the properties
Step 6: Copy the path of bin folder which is installed in JDK folder.
Step 7: Paste Path of bin folder in Variable value and click on OK Button.
Note: In case you already have a PATH variable created in your PC, then
paste your path separated with a semi-comma (;), edit the PATH variable
to
Here, %PATH% appends the existing path variable to our new value
Note: In case you java installation does not work after installation, change
classpath to
PROGRAM:
public class Practical1
OUTPUT:
Practical - 2
AIM:
Write a program in Java to generate first n prime numbers.
PROGRAM:
public class Practical2
int ct=0,n=0,i=1,j=1;
while(n<10)
j=1;
ct=0;
while(j<=i)
if(i%j==0)
ct++;
j++;
}
if(ct==2)
System.out.printf("%d ",i);
n++;
i++;
OUTPUT:
Practical - 3
AIM:
Write a program in Java to find maximum of three numbers using conditional
operator.
PROGRAM:
mport java.util.Scanner;
i
int a, b, 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();
System.out.println("a is big");
System.out.println("B is big");
else {
System.out.println("c2 is big");
OUTPUT:
Practical - 4
AIM:
Write a program in Java to find second maximum of n numbers without using
arrays.
PROGRAM:
ublic class Practical4
P
int n1=40,n2=9000,n3=20,n4=60,n5=21;
int m1,m2,m3,m4,largest,secondLargest,thirdLargest;
largest=max1(n1,n2,n3,n4,n5);
System.out.println("Largest="+largest);
if(largest==n1)
n1=n2;
n2=n3;
n3=n4;
n4=n5;
else if(largest==n2)
n1=n1;
n2=n3;
n3=n4;
n4=n5;
else if(largest==n3)
n1=n1;
n2=n2;
n3=n4;
n4=n5;
else if(largest==n4)
n1=n1;
n2=n2;
n3=n3;
n4=n5;
else if(largest==n5)
n1=n1;
n2=n2;
n3=n3;
n4=n4;
secondLargest=max2(n1,n2,n3,n4);
System.out.println("2nd Largest="+secondLargest);
int m1,m2,m3;
m1=max(a,b);
m2=max(m1,c);
m3=max(m2,d);
return m3;
int m1,m2,m3,m4,m5,l;
m1=max(n1,n2);
m2=max(n3,n4);
m3=max(m1,n5);
m4=max(m2,n5);
l=max(m3,m4);
return l;
if(a>b)
return a;
else
return b;
OUTPUT:
Practical - 5
AIM:
Write a program in Java to reverse the digits of a number using while loop.
PROGRAM:
import java.util.Scanner;
int reverse=reverse(number);
int reverse = 0;
int remainder =0;
while(number>0)
remainder = number%10;
reverse = reverse*10+remainder;
number = number/10;
return reverse;
OUTPUT:
Practical - 6
AIM:
Write a program in Java to convert number into words & print it.
PROGRAM:
mport java.util.Scanner;
i
int number = 0;
try {
number = scanner.nextInt();
if (number == 0)
else
catch (Exception e)
scanner.close();
"eighteen", "nineteen" };
String tensArray[] = { "zero", "ten", "twenty", "thirty", "for
ty", "fifty",
if (number == 0)
return "zero";
if (number < 0)
numberStr = numberStr.substring(1);
number %= 1000000;
number %= 1000;
}
number %= 100;
if (number > 0)
words += unitsArray[number];
else
return words;
OUTPUT:
Practical - 7
AIM:
Write programs in Java to use Wrapper class of each primitive data types.
PROGRAM:
class Practical7
int i=iOb.intValue();
OUTPUT:
Practical - 8
AIM:
Write a program in Java to multiply two matrix.
PROGRAM:
mport java.util.Scanner;
i
rows = sc.nextInt();
columns = sc.nextInt();
arr1[i][j] = sc.nextInt();
arr2[i][j] = sc.nextInt();
}
System.out.println("");
OUTPUT:
Practical - 9
AIM:
Write a static block which will be executed before main( ) method in a class.
PROGRAM:
public class Practical9
static
}
OUTPUT:
Practical - 10
AIM:
Write a program in Java to demonstrate use of this keyword. Check whether this
can access the private members of the class or not.
PROGRAM:
public class Practical10
obj1.setdata(5);
obj1.getdata();
class Test
private int n;
void setdata(int n)
void getdata()
System.out.println("n=" + n);
OUTPUT:
Practical - 11
AIM:
Write a program in Java to develop overloaded constructor. Also develop the copy
constructor to create a new object with the state of the existing object.
PROGRAM:
class Box {
int length;
int width;
int height;
Box()
length = 10;
width = 10;
height = 10;
length = l;
width = w;
height = h;
Box(Box b)
length = b.length;
width = b.width;
height = b.height;
void volume()
b1.volume();
b2.volume();
b3.volume();
OUTPUT:
Practical - 12
AIM:
Write a program in Java to demonstrate the use of private constructor and also
write a method which will count the number of instances created using default
constructor only.
PROGRAM:
class Box
int length;
int width;
int height;
Box()
length = 10;
width = 10;
height = 10;
Box.count_instances();
length = l;
width = w;
height = h;
count++;
OUTPUT:
Practical - 13
AIM:
Write a program in Java to demonstrate the use of 'final' keyword in the field
declaration. How it is accessed using the objects.
PROGRAM:
class FinalDemo
obj.display();
OUTPUT:
Practical - 14
AIM:
Develop minimum 4 program based on variation in methods i.e. passing by value,
passing by reference, returning values and returning objects from methods.
PROGRAM:
class PBV {
int i;
void set_data(int a) {
i=a;
void display()
obj.set_data(5);
obj.display();
OUTPUT:
Practical - 15
AIM:
Write a program in Java to demonstrate single inheritance, multilevel inheritance
and hierarchical inheritance.
PROGRAM:
import java.util.*;
class A
class B extends A
class C extends B
class D extends A
class U4P1
B B1 = new B();
C C1 = new C();
D D1 = new D();
B1.PrintB();
C1.PrintC();
D1.PrintD();
OUTPUT:
Practical - 16
AIM:
Create a class to find out whether the given year is leap year or not. (Use
inheritance for this program).
PROGRAM:
import java.util.Scanner;
System.out.println("Thanki Shivam");
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
leap = true;
else
leap = false;
else
leap = true;
else
leap = false;
if (leap)
else
OUTPUT:
Practical - 17
AIM:
Write an application that illustrates how to access a hidden variable. Class A
declares a static variable x. The class B extends A and declares an instance
variable x. display() method in B displays both of these variables.
PROGRAM:
import java.util.*;
class A
static int x;
class B extends A
int x;
B(int a,int b)
void display()
class U4P3
obj.display();
OUTPUT:
Practical - 18
AIM:
Write a program in Java in which a subclass constructor invokes the constructor of
the super class and instantiate the values.
PROGRAM:
class A
int x;
A(int a)
x=a;
class B extends A
int y;
B(int a,int b)
super(a);
y = b;
void display()
obj.display();
OUTPUT:
Practical - 19
AIM:
Write a program that illustrates interface inheritance. Interface P12 inherits from
both P1 and P2. Each interface declares one constant and one method. The class Q
implements P12. Instantiate Q and invoke each of its methods. Each method
displays one of the constants.
PROGRAM:
import java.util.*;
interface P1
int I = 10;
void showI();
interface P2
int J = 30;
void showJ();
int K = 50;
void showK();
class U4P5
obj.showI();
obj.showJ();
obj.showK();
OUTPUT:
Practical - 20
AIM:
Write an application that illustrates method overriding in the same package and
different packages. Also demonstrate accessibility rules in inside and outside
packages.
PROGRAM:
import java.util.*;
class A
void show()
System.out.println("Inside Class-A");
}class B extends A
void show()
System.out.println("Inside Class-B");
class U4P6
B obj=new B();
OUTPUT:
Create Package:
package p1;
Inside:
import p1.*;
class inside
System.out.println("n = "+p1.n);
System.out.println("n_pub = "+p1.n_pub);
Inside Output:
Outside:
import p1.*;
class outside
System.out.println("n = "+p1.n);
System.out.println("n_pub = "+p1.n_pub);
Outside Output:
Practical - 21
AIM:
Describe abstract class called Shape which has three subclasses say Triangle,
Rectangle, Circle. Define one method area() in the abstract class and override this
area() in these three subclasses to calculate for specific object i.e. area() of
Triangle subclass should calculate area of triangle etc. Same for Rectangle and
Circle.
PROGRAM:
import java.util.*;
int base,height;
Triangle(int b,int h)
base=b;
height=h;
void area()
int length,width;
Rectangle(int l,int w)
length=l;
width=w;
void area()
float ar = length*width;
int radius;
Circle(int r)
radius = r;
void area()
class U4P7
t1.area();
r1.area();
c1.area();
OUTPUT:
Practical - 22
AIM:
Write a program in Java to demonstrate implementation of multiple inheritance
using interfaces.
PROGRAM:
interface A
void method1();
interface B
void method2();
System.out.println("Inside Method-1");
System.out.println("Inside Method-2");
obj.method1();
obj.method2();
OUTPUT:
Practical - 23
AIM:
Write a program in Java to demonstrate use of final class.
PROGRAM:
import java.util.*;
final class A
int i;
void showI()
class B extends A
int j;
void showJ()
class Finalclass
System.out.println("Thanki Shivam");
obj.i = 20;
obj.j = 30;
obj.showI();
obj.showJ();
OUTPUT:
Practical - 24
AIM:
Write a program in Java to develop user defined exception for 'Divide by Zero'
error.
PROGRAM:
final class A
int i;
void showI()
class B extends A
int j;
void showJ()
obj.i = 2;
obj.j = 3;
obj.showI();
obj.showJ();
OUTPUT:
Practical - 25
AIM:
Write a program in Java to demonstrate multiple try block and multiple catch
exception.
PROGRAM:
public class p25
try
int a= 4/0;
try
System.out.println(b[5]);
catch(ArrayIndexOutOfBoundsException e)
catch(ArithmeticException e)
System.out.println("Divide by zero.");
OUTPUT:
Practical - 26
AIM:
Write an small application in Java to develop Banking Application in which user
deposits the amount Rs 1000.00 and then start withdrawing of Rs 400.00, Rs
300.00 and it throws exception "Not Sufficient Fund" when user withdraws Rs.
500 thereafter.
PROGRAM:
import java.util.*;
import java.io.*;
class Bank
float fund;
fund=amount;
float newFund=fund-money;
if(newFund<500)
else
fund=newFund;
b.deposit(1000.00f);
try
float money;
money=sc.nextInt();
b.withdraw(money);
catch(Exception e)
System.out.println(e.getMessage());
OUTPUT:
Practical - 27
AIM:
Write a program that executes two threads. One thread displays “Thread1” every
2,000 milliseconds, and the other displays “Thread2” every 4,000 milliseconds.
Create the threads by extending the Thread class.
PROGRAM:
class NewThread extends Thread
NewThread(String threadname)
super(threadname);
if( getName().equals("Thread1")==true)
for(int i=1;i<=5;i++)
System.out.println("Thread1");
try
Thread.sleep(2000);
catch(InterruptedException e)
System.out.println("Exception Occurred.");
else
for(int i=1;i<=5;i++)
System.out.println("Thread2");
try
Thread.sleep(4000);
catch(InterruptedException e)
System.out.println("Exception Occurred.");
t1.start();
t2.start();
OUTPUT:
Practical - 28
AIM:
Write a program that executes two threads. One thread will print the even numbers
and the another thread will print odd numbers from 1 to 50.
PROGRAM:
class NewThread extends Thread
NewThread(String threadname)
super(threadname);
if( getName().equals("Odd")==true)
for(int i=1;i<=50;i=i+2)
try
Thread.sleep(1000);
catch(InterruptedException e)
System.out.println("Exception Occurred.");
else
for(int i=2;i<=50;i=i+2)
try
Thread.sleep(1000);
catch(InterruptedException e)
System.out.println("Exception Occurred.");
t1.start();
t2.start();
OUTPUT:
Practical - 29
AIM:
Write a program in Java to demonstrate use of synchronization of threads when
multiple threads are trying to update common variable.
PROGRAM:
class Counter
int c=0;
c++;
try
Thread.sleep(1000);
catch(InterruptedException e)
Counter obj;
NewThread(Counter o)
obj = o;
obj.increment();
t1.start();
t2.start();
t3.start();
OUTPUT:
Practical - 30
AIM:
Write a program in Java to create, write, modify, read operations on a Text file.
PROGRAM:
import java.io.*;
try
if (!file.exists())
file.createNewFile();
byte b[]=s.getBytes();
fout.write(b);
fout.close();
System.out.println("Writing Complete!");
int i;
while((i=fin.read())!=-1)
System.out.print((char)i);
fin.close();
fout=new FileOutputStream("E:\\newfile.txt");
b=s.getBytes();
fout.write(b);
fout.close();
while((i=fin.read())!=-1)
System.out.print((char)i);
fin.close();
catch (IOException e)
OUTPUT: