Java Lab Manual
Java Lab Manual
Java Lab Manual
Practical -1
Aim: Install JDK, write a simple “Hello World” or similar java program,
compilation, debugging, executing using java compiler and interpreter
Output:
Hello World
Page 1
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -2
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of n:");
int n = in.nextInt();
int counter = 0;
int flag;
for (int num = 2; ; num++)
{
flag = 0;
for (int div = 2; div < num; div++)
{
if (num % div == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
System.out.println(num);
counter++;
if(counter == n)
{
break;
}
}
}
}
}
Page 2
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Output:
Page 3
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -3
Output:
30 is maximum
Page 4
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -4
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int high=0,sec_high=0;
int n;
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of n:");
n= in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print("Enter the value:");
int value = in.nextInt();
if(value>high)
{
sec_high=high;
high=value;
}
else
{
if(value>sec_high)
{
sec_high=value;
}
}
}
System.out.println("Highest value:" + high);
System.out.println("Second Highest value:" + sec_high);
}
}
Page 5
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Output:
Page 6
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -5
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the Value: ");
int value = in.nextInt();
int reversenum=0;
while( value != 0 )
{
reversenum = (reversenum * 10)+(value%10);
value = value/10;
}
System.out.print("Reverse Number: " + reversenum);
}
}
Output:
Page 7
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -6
Aim: Write a program in Java to convert number into words & print it
import java.util.Scanner;
public class Example
{
public static void word(int n,String s)
{
String one[] =
{"","one","two","three","four","five","six","seven","eight","nine","te
n","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seven
teen","eighteen","nineteen"};
String ten[] =
{"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","nine
ty"};
if(n>19)
{
System.out.print(ten[n/10] + " " + one[n%10] + s);
}
else
{
if(n>0)
System.out.print(one[n] + s);
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the Value: ");
int value = in.nextInt();
word(value/10000000," crore ");
value = value%10000000;
word(value/100000," lack ");
value = value%100000;
word(value/1000," thousand ");
value = value%1000;
word(value/100," hundred ");
value = value%100;
Page 8
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
word(value,"");
}
}
Output -1:
Output -2:
Output -3:
Page 9
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -7
Aim: Write programs in Java to use Wrapper class of each primitive data
types
Output:
i =5
f =2.3
c =A
b =true
Page 10
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -8
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int a[][]= {{1,2},{3,4}}, b[][] = {{3,4},{1,2}};
int c[][]=new int[2][2];
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
c[i][j]=0;
for(int k=0;k<=1;k++)
{
c[i][j]=c[i][j] + (a[i][k]*b[k][j]);
}
}
}
System.out.print("Result:\n");
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
System.out.print(c[i][j] + " ");
}
System.out.print("\n");
}
}
}
Output:
Result:
58
13 20
Page 11
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -9
Aim: Write a static block which will be executed before main( ) method in
a class
Output:
Page 12
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -10
class Test
{
private int n;
void setdata(int n)
{
this.n = n; //accessing private member (n) using this keyword.
}
void getdata()
{
System.out.println("n=" + n);
}
}
public class Example
{
public static void main(String[] args)
{
Test obj1 = new Test();
obj1.setdata(5);
obj1.getdata();
}
}
Output:
n=5
Page 13
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
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.
class Box
int length;
int width;
int height;
Box()
length = 10;
width = 10;
height = 10;
length = l;
width = w;
height = h;
Page 14
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Box(Box b)
length = b.length;
width = b.width;
height = b.height;
void volume()
b1.volume();
b2.volume();
b3.volume();
Page 15
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Output:
Inside Default Constructor
Page 16
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -12
class Box
{
int length;
int width;
int height;
static int count=0;
Box()
{
length = 10;
width = 10;
height = 10;
Box.count_instances();
}
Box(int l, int w,int h)
{
length = l;
width = w;
height = h;
}
//following method counts number of objects created using default constructor.
static void count_instances()
{
count++;
}
}
public class Example
{
public static void main(String[] args)
{
Box b1 = new Box();
Box b2 = new Box();
System.out.println("No of Objects Created: " + Box.count );
}
Page 17
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Output:
No of Objects Created: 2
Page 18
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -13
class FinalDemo
{
final double PI=3.14159;
final void display()
{
System.out.println("PI : " + PI);
}
}
public class Example
{
public static void main(String[] args)
{
FinalDemo obj = new FinalDemo();
obj.display();
}
}
Output:
PI: 3.14159
Page 19
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -14
class PBV
{
int i;
void set_data(int a)
{
i=a;
}
void display()
{
System.out.print("i : " + i);
}
}
public class Example
{
public static void main(String[] args)
{
PBV obj = new PBV();
obj.set_data(5); //Pass by Value
obj.display();
}
}
Output:
i:5
Page 20
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
class PBR
{
int i;
void set_data(PBR o)
{
i = o.i;
}
void display()
{
System.out.print("i : " + i);
}
}
public class Example
{
public static void main(String[] args)
{
PBR obj1 = new PBR();
PBR obj2 = new PBR();
obj1.i=7;
obj2.set_data(obj1); //Pass by Reference
obj2.display();
}
}
Output:
i:7
Page 21
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
class RBV
{
int i;
void set_data(int a)
{
i=a;
}
int incrByOne()
{
i++;
return (i);
}
}
public class Example
{
public static void main(String[] args)
{
RBV obj1 = new RBV();
obj1.set_data(5); //Pass by Value
int value = obj1.incrByOne(); // Return by Value
System.out.println("i : " + value);
}
}
Output:
i:6
Page 22
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
class RBR
{
int i;
RBR Copy()
{
RBR temp = new RBR();
temp.i = i;
return temp;
}
void display()
{
System.out.println("i : "+ i);
}
}
public class Example
{
public static void main(String[] args)
{
RBR obj1 = new RBR();
obj1.i=7;
RBR obj2 = obj1.Copy(); //Return by Reference
obj1.display();
obj2.display();
}
}
Output:
i:7
i:7
Page 23
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -15
Aim: - Write a program in Java to demonstrate single inheritance,
multilevel inheritance and hierarchical inheritance.
Output:
i+j : 12
Page 24
JAVA PROGRAMMING (3350703) ENROLLMENT
NO. :
Practical: 15.2 (Multilevel Inheritance)
class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
void sum()
{
System.out.println("i+j+k : " + (i+j+k));
}
}
public class Example
{
public static void main(String[] args)
{
C c = new C();
c.i=5;
c.j=7;
c.k=9;
c.sum();
}
}
Output:
i+j+k : 21
Page 25
JAVA PROGRAMMING (3350703) ENROLLMENT
NO. :
Practical: 15.3 (Hierarchical Inheritance)
class A
{
int i;
}
class B extends A
{
int j;
void sum()
{
System.out.println("i+j : " + (i+j));
}
}
class C extends A
{
int k;
void sum()
{
System.out.println("i+k : " + (i+k));
}
}
public class Example
{
public static void main(String[] args)
{
B b = new B();
b.i=2;
b.j=4;
b.sum();
C c = new C();
c.i=3;
c.k=5:
c.sum();
}
}
Output: i+j=6
i+k=8
Page 26
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -16
Aim: - Create a class to find out whether the given year is leap year
or not. (Use inheritance for this program).
class LeapYear
{
int year;
void isLeapYear()
{
if(year%4==0)
System.out.println(year + " is leap year.");
else
System.out.println(year + " is not leap year.");
}
}
public class Example
{
public static void main(String[] args)
{
LeapYear l = new LeapYear();
l.year = 2015;
l.isLeapYear();
l.year = 2016;
l.isLeapYear();
}
}
Output:
Page 27
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
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 class B
displays both of these variables.
class A
{
int x;
}
class B extends A
{
int x;
B(int a,int b)
{
super.x = a; //super.x refers variable x of class A
x = b; // x refers to variable x of class B
}
void display()
{
//super.x prints value of variable x of class A
System.out.println("X of A :" + super.x);
//x prints value of variable x of class B
System.out.println("X of B :" + x);
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B(2,4);
obj.display();
}
}
Output:
X of A :2
X of B :4
Page 28
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical -18
Aim: - Write a program in Java in which a subclass constructor
invokes the constructor of the super class and instantiate the values.
class A
{
int x;
A(int a)
{
x=a;
}
}
class B extends A
{
int y;
B(int a,int b)
{
super(a); //super() calls the constructor of the class A
y = b;
}
void display()
{
System.out.println("X :" + x);
System.out.println("Y :" + y);
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B(2,4);
obj.display();
}
}
Output:
X:2
Y:4
Page 29
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
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.
interface P1
{
int I = 1;
void showI();
}
interface P2
{
int J = 2;
void showJ();
}
interface P12 extends P1, P2
{
int K = 3;
void showK();
}
class Q implements P12
{
public void showI()
{
System.out.println("I : " + I);
}
public void showJ()
{
System.out.println("J : " + J);
}
public void showK()
{
System.out.println("K : " + K);
}
}
Page 30
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Output:
I:1
J:2
K:3
Page 31
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical – 20
Aim: - (1) Write an application that illustrates method overriding.
(2) Also demonstrate accessibility rules in inside and outside
packages.
1. Method Overriding:
class A
{
void show()
{
System.out.println("Inside Class-A");
}
}
class B extends A
{
// Overriding method show of class A
void show()
{
System.out.println("Inside Class-B");
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B();
obj.show();
//show() method of class-B will be called.
Output:
I:1
J:2
K:3
Page 32
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
package p1;
public class Protection
{
int n = 1;
public int n_pub = 2;
}
public class Example
{
public static void main(String[] args)
{
Protection p1 = new Protection();
System.out.println("n = " + p1.n);
System.out.println("n_pub = " +p1.n_pub);
}
}
Output:
n=1
n= 2
Page 33
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Protection.java
package p1;
public class Protection
{
int n = 1;
public int n_pub = 2;
}
Example.java
import p1.*;
public class Example
{
public static void main(String[] args)
{
Protection p1 = new Protection();
System.out.println("n = " + p1.n); //error
System.out.println("n_pub = " +p1.n_pub);
}
}
Page 34
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
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.
Page 35
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
}
}
class Circle extends Shape
{
int radius;
Circle(int r)
{
radius = r;
}
void area()
{
float ar = 3.14f * radius * radius;
System.out.println("Area of Circle: "+ar);
}
}
public class Example
{
public static void main(String[] args)
{
Triangle t1 = new Triangle(2,4);
t1.area();
Rectangle r1 = new Rectangle(3,5);
r1.area();
Circle c1= new Circle(2);
c1.area();
}
}
Output:
Page 36
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 22
Aim: - Write a program in Java to demonstrate implementation of
multiple inheritance using interfaces.
interface A
{
void method1();
}
interface B
{
void method2();
}
class InterfaceDemo implements A , B
{
public void method1()
{
System.out.println("Inside Method-1");
}
public void method2()
{
System.out.println("Inside Method-2");
}
}
public class Example
{
public static void main(String[] args)
{
InterfaceDemo obj = new InterfaceDemo();
obj.method1();
obj.method2();
}
}
Output:
Inside Method-1
Inside Method-2
Page 37
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 23
Aim: - Write a program in Java to demonstrate use of final class.
final class A
{
int i;
void showI()
{
System.out.println("I : " + i);
}
}
// The following class is illegal.
// ERROR! Can't subclass A
class B extends A
{
int j;
void showJ()
{
System.out.println("J : " + j);
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B();
obj.i = 2;
obj.j = 3;
obj.showI();
obj.showJ();
}
}
Page 38
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Output:
Page 39
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 24
Aim: - Write a program in Java to develop user defined exception
for 'Divide by Zero' error.
Output:
Page 40
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 25
Aim: Write a program in Java to demonstrate multiple try block and
multiple catch exception.
Output:
Divide by zero.
Page 41
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 26
Aim: Write a 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.
}
}
else
{
amount = amount -a;
System.out.println(a + " rupees withdrawn.");
System.out.println("New balance is : " + amount);
}
}
}
public class Example
{
public static void main(String[] args)
{
Account a1 = new Account(1000);
a1.withdraw(400.00);
a1.withdraw(300.00);
a1.withdraw(500.00);
}
}
Output:
Page 43
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
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.
Page 44
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
}
}
Page 45
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
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.
Page 46
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 29
Aim: Write a program in Java to demonstrate use of synchronization of
threads when multiple threads are trying to update common variable.
Class Printing
{
Synchronized void printnumber(int n)
{
System.out.println(“start”);
for(int j =n;j>0;j--)
{
try
{
if(j==n/2)
Thread.sleep(100);
}
catch(InterruptedException e)
{
;
}
System.out.println(“”+j);
}
System.out.println(“End”);
}
}
class Threadserve implements Runnable
{
int n;
Printing pt;
Thread th;
Page 47
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Threadserve(Printing p, intx)
{
n=x;
pt=p;
th=new Thread(this);
th.start();
}
Public void run()
{
pt.printnuber(n);
}
}
public class SynchronizedThread
{
Public static void main(String args[])
{
Printing p= new Printing();
Threadserve ts1= new Threadserve(p,16);
Threadserve ts2= new Threadserve(p,8);
Threadserve ts3= new Threadserve(p,10);
}
}
Page 48
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
Practical - 30
Aim: Write a program in Java to create, write, modify, read operation on
a Text file.
import java.io.*;
public class FileDemo
{
public static void main(String args[])
{
try
{
FileOutputStream fout= new FileOutputStream(“f.txt”);
System.out.println(“File Created”);
String s = “India is Great”;
byte b[] = s.getBytes();
fout.write(b);
fout.close();
System.out.println(“Writing Completed!”);
}
catch(Exception e)
{
System.out.println(e);
}
Page 49
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :
try
{
FileInputStream fin = new FileInputStream(“f.txt”);
int i;
while((i=fin.read())!=-1)
System.out.println((char)i);
Fin.close();
System.out.println(“Reading Complete!”);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Page 50