Java Practical List
Java Practical List
1 write down steps for java environment setup & write a simple “Hello
World” or similar java program, compilation, debugging, executing using
java compiler and interpreter.
186150316006 Page no : 1
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 2
Aim : Write a program in Java to find maximum of three numbers using
conditional operator.
import java.util.*;
class Max
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number 1 : ");
int a = sc.nextInt();
System.out.println("Enter Number 2 : ");
int b = sc.nextInt();
System.out.println("Enter Number 3 : ");
int c = sc.nextInt();
if (a>b && a>c)
{
System.out.println("Maximum number is : "+a);
}
else if(b>a && b>c)
{
System.out.println("Maximum number is : "+b);
}
else
{
System.out.println("Maximum number is : "+c);
}
}
}
Output :-
186150316006 Page no : 2
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 3
Aim : Write a program in Java to generate first n prime numbers.
import java.util.*;
class Prime
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int x=2,i,j;
System.out.println("Enter Number: ");
int n=sc.nextInt();
System.out.println("First " +n+ " prime numbers are :");
for(j=0;j<n;)
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
break;
}
}
if(i==x)
{
System.out.println(""+x);
n--;
}
x++;
}
}
}
Output :-
186150316006 Page no : 3
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 4
Aim : Write a program to find second maximum of number without using
arrays
import java.util.*;
class Smax
{
public static void main(String args[])
{
int max=0,smax=0;
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number you have to enter: ");
int no=sc.nextInt();
System.out.println("Enter numbers : ");
for(int i=0;i<no;i++)
{
int n=sc.nextInt();
if(i==0)
{
max=n;
}
else if(n>max)
{
smax=max;
max=n;
}
else if(n>smax)
{
smax=n;
}
}
System.out.println("Second maximum Number is "+smax);
}
}
Output :-
186150316006 Page no : 4
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 5
Aim : Write a program in Java to reverse the digits of a number using while loop.
import java.util.*;
class Reverse
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number : ");
int a = sc.nextInt();
int num = 0;
while(a > 0)
{
num = num * 10 + a % 10;
a = a / 10;
}
System.out.println("Reverse of number : \n"+num);
}
}
Output :-
186150316006 Page no : 5
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 6
Aim : Write a program in Java to convert number into words & print it.
import java.util.*;
class Dcoder
{
public static void main(String args[])
{
String a="";
Scanner sc=new Scanner(System.in);
System.out.println("ENTER ANY NUMBER : ");
int no=sc.nextInt();
int r;
while(no>0)
{
r=no%10;
switch(r)
{
case 0:
a=" ZERO"+ a;
break;
case 1:
a=" ONE"+ a;
break;
case 2:
a=" TWO"+ a;
break;
case 3:
a=" THREE"+ a;
break;
case 4:
a=" FOUR"+ a;
break;
case 5:
a=" FIVE"+ a;
break;
case 6:
a=" SIX"+ a;
break;
case 7:
a=" SEVEN"+ a;
break;
case 8:
a=" EIGHT"+ a;
break;
186150316006 Page no : 6
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
case 9:
a=" NINE"+ a;
break;
case 10:
a=" TEN"+ a;
break;
default :
System.out.println("invalid");
}
no = no/10;
}
System.out.println("\nNUMBER IN WORD :\n"+a);
}
}
Output :-
Practical : 7
186150316006 Page no : 7
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Aim : Write programs in Java to use Wrapper class of each primitive data types.
import java.util.*;
class wrap1
{
public static void main(String args[])
{
byte b = 3;
int i = 10;
float f = 2.5f;
short s = 20;
double d = 40.5;
}
}
Output :-
Practical : 8
186150316006 Page no : 8
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
import java.util.*;
class Multiplication
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter number of matrix:");
int n= sc.nextInt();
int a[ ][ ]=new int[n][n];
int b[ ][ ]=new int[n][n];
int c[ ][ ]=new int[n][n];
System.out.println("\nEnter Matrix A ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("\nEnte Matrix B:");
for (int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
b[i][j]=sc.nextInt();
}
}
System.out.println("\nMultiplication of Matrix A and B:");
for ( int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
c[i][j]=0;
for(int k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output :-
186150316006 Page no : 9
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 9
186150316006 Page no : 10
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Aim : Write a program in Java to demonstrate use of this keyword.
Class thiskey
{
Int x,y;
Void getdata(int x,int y)
{
This.x=x;
This.y=y;
}
Void putdata()
{
System.out.println(“X = “+x);
System.out.println(“Y = “+y);
}
}
Class main
{
Public static void main(String args[])
{
Thiskey f = new thiskey();
f.getdata(4,5);
f.putdata();
}
}
Output :-
Practical : 10
Aim : Write a program in Java to demonstrate use of final keyword.
186150316006 Page no : 11
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
class finalkey
{
final int a=30;
void display ()
{
// a=10;error: cannot assign a value to final variable a=10;
System.out.println("A = "+a);
}
}
class main
{
public static void main(String args[])
{
finalkey f = new finalkey();
f.display();
}
}
Output :-
Practical : 11
Aim :
186150316006 Page no : 12
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
(a) Write a java program that make use of pass by value in method
class value
{
int a=60;
void data(int a)
{
a=a+40;
}
Output :-
(b) Write a java program that make use of pass by reference in method.
class value
{
int a=60;
186150316006 Page no : 13
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
void data(value b)
{
b.a=b.a+40;
}
public static void main(String args[])
{
value b = new value ();
System.out.println("Value of a : "+b.a);
b.data(b);
System.out.println("after Chang Value of a : "+b.a);
}
Output :-
Practical : 12
Aim :
186150316006 Page no : 14
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
(a) Write a java program that make use of method returning object.
class Test
{
int l;
void setdata(int l)
{
this.l=l;
}
int getdata()
{
return l;
}
}
class main
{
public static void main (String args[])
{
Test r = new Test ();
r.setdata(30);
int len = r.getdata();
System.out.println("value of l : "+len);
}
}
Output :-
class Test
186150316006 Page no : 15
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
{
int l;
int b;
Test(int l,int b)
{
this.l=l;
this.b=b;
}
Test getdata()
{
Test a = new Test(10,20);
return a;
}
}
class main
{
public static void main (String args[])
{
Test t1 = new Test (40,50);
Test t2;
t2=t1.getdata();
System.out.println("length of t1 object : "+t1.l);
System.out.println("breadth of t1 object : "+t1.b);
System.out.println("length of t2 object : "+t2.l);
System.out.println("breadth of t2 object : "+t2.b);
}
}
Output :-
Practical : 13
Aim : Write a java program that demonstrate the use of static keyword.
186150316006 Page no : 16
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
class Test
{
static int i=10, j=20;
void display()
{
System.out.println("Hello World");
}
static
{
System.out.println("Hello");
}
}
class Main
{
public static void main(String args[])
{
Test d1 = new Test();
System.out.println("Value of I = "+Test.i);
System.out.println("Value of J = "+Test.j);
d1.display();
}
}
Output :-
Practical : 14
class book
186150316006 Page no : 17
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
{
String title;
String author;
int price;
private book(String t, String a,int p)
{
title = t;
author = a;
price = p;
}
static book creatbook()
{
book x = new book("Abc","Xyz",100);
return x;
}
void display ()
{
System.out.println("Book Title : "+title);
System.out.println("Book Author Name : "+author);
System.out.println("Book Price : "+price);
}
}
class main
{
public static void main (String args[])
{
book b = book.creatbook();
b.display();
}
}
Output :
Practical : 15
Aim : Write a program in Java to demonstrate the use of copy constructor.
import java.util.*;
class CopyConst
186150316006 Page no : 18
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
{
int id;
String name;
CopyConst(int id, String name)
{
this.id=id;
this.name=name;
}
CopyConst(CopyConst x)
{
id = x.id;
name = x.name;
}
void display()
{
System.out.println("Student Id : "+id);
System.out.println("Student name : "+name);
}
}
class main
{
public static void main(String args[])
{
CopyConst c1 = new CopyConst(006,"Vaishali");
c1.display();
CopyConst c2 = new CopyConst(c1);
c2.display();
}
}
Output :-
Practical : 16
186150316006 Page no : 19
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
{
int l,w,h;
box()
{
l=0;
w=0;
h=0;
}
box(int a)
{
this.l=a;
this.w=a;
this.h=a;
}
box(int l,int w,int h)
{
this.l=l;
this.w=w;
this.h=h;
}
double volume ()
{
return l*w*h;
}
}
class main
{
public static void main(String args[])
{
box b1 = new box();
System.out.println("Volume of box b1 :"+b1.volume());
box b2 = new box(4);
System.out.println("Volume of box b2 :"+b2.volume());
box b3 = new box(1,2,3);
System.out.println("Volume of box b3 :"+b3.volume());
}
}
Output :-
186150316006 Page no : 20
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 17
Aim :
class Test
{
int a,b;
void setdata(int a,int b)
{
this.a=a;
this.b=b;
}
}
class abc extends Test
{
void getdata()
{
System.out.println("Value of a = "+a);
System.out.println("Value of a = "+b);
}
}
class main
{
public static void main (String args[])
{
abc a = new abc();
a.setdata(3,4);
a.getdata();
}
}
Output :-
186150316006 Page no : 21
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
class student
{
int no;
void setdata(int no)
{
this.no=no;
}
void getdata()
{
System.out.println("Roll number : "+no);
}
}
class main
186150316006 Page no : 22
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
{
public static void main (String args[])
{
Output :-
186150316006 Page no : 23
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
import java.util.*;
class person
{
String name;
void setname(String name)
{
this.name=name;
}
void displayname()
{
System.out.println("Name :"+name);
}
}
class mathtecher extends person
{
void techmath()
{
System.out.println(name+ " Teach Mathes..");
}
}
class bizznesman extends person
{
void runbizznes()
{
System.out.println(name+" Run bizznes..");
}
}
class main
{
public static void main(String args[])
{
bizznesman b = new bizznesman();
b.setname("Abc");
b.runbizznes();
Output :-
186150316006 Page no : 24
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Practical : 18
(a).Write a java program that illustrate how to access hidden variables of super
class into subclass using super keyword.
Class Superclass
{
Int x = 100;
}
Class Subclass extends Superclass
{
Int x = 200;
Void printNumber()
{
System.out.println(“Value of X in Superclass : “+super.x);
System.out.println(“Value of X in Subclass : “+x);
}
}
Class main
{
Public static void main(String args[])
{
Subclass s= new Subclass();
s.printNumber();
}
}
Output :-
186150316006 Page no : 25
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
(b).Write a java program in which subclass constructor invokes the constructor
of super class using super keyword.
//import java.util.*;
class vehicle
{
int id_vehicle;
String manufactur;
vehicle(int i, String m)
{
id_vehicle=i;
manufactur=m;
}
void display_vehicle ()
{
System.out.println("Vehicle Id : "+id_vehicle);
System.out.println("Vehicle Company : "+manufactur);
}
}
class transportationVehicle extends vehicle
{
int load_capacity;
186150316006 Page no : 26
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
display_vehicle();
System.out.println("Number of passenger : "+no_passenger);
}
}
class Personal
{
public static void main(String args[])
{
transportationVehicle t = new transportationVehicle(112233,"TATA",500);
t.display_transportation();
System.out.println();
passengerVehicle p =new passengerVehicle(332211,"BAJAJ",10);
p.display_passenger();
}
}
Output :-
186150316006 Page no : 27
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
186150316006 Page no : 28
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
System.out.println("Rate of Interest in SBI : "+s.getRateOfInterest());
bank i = new ICICI();
System.out.println("Rate of Interest in ICICI : "+i.getRateOfInterest());
bank a = new AXIS ();
System.out.println("Rate of Interest AXIS : "+a.getRateOfInterest());
}
}
Output :-
186150316006 Page no : 29
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
}
}
class main
{
public static void main(String args[])
{
shape s1;
s1 = new rectangle(6,5);
s1.area();
s1 = new circle(5);
s1.area();
}
}
Output :-
interface scanner
{
void generatePDF();
}
interface copier
{
void generateCOPY(int a);
}
interface printer extends scanner,copier
{
void printDOC();
}
class printerDemo implements printer
{
public void printDOC()
{
System.out.println("Document is printing...");
}
public void generatePDF()
{
186150316006 Page no : 30
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
System.out.println("PDF is generated..");
}
public void generateCOPY(int a)
{
System.out.println(a+" Copies are generated..");
}
}
class main
{
public static void main (String args[])
{
printerDemo p = new printerDemo();
p.generatePDF();
p.generateCOPY(4);
p.printDOC();
}
}
Output :-
import java.util.*;
class DividedByZero extends Exception
{
String msg;
DividedByZero(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
class demo
{
public static void main (String args[])
{
//System.out.println("Enter number : ");
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers : ");
int a = sc.nextInt();
int b = sc.nextInt();
if(b==0)
186150316006 Page no : 31
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
{
try
{
throw new DividedByZero("\nDivide by Zero");
}
catch(DividedByZero e)
{
System.out.println("Exception Caught..."+e);
}
}
else
{
System.out.println("Answer : "+(a/b));
}
}
}
Output :-
24 Write a java program that demonstrate the use of single try block and
multiple catch clauses.
class exceptiondemo
{
public static void main (String [] args)
{
int a=5;
int b=0;
int x[] = new int[5];
try
{
int c=a/b;
x[6]=12;
}
catch(ArithmeticException m)
{
System.out.println("ArithmeticException caught.."+a);
186150316006 Page no : 32
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
}
catch(NullPointerException n)
{
System.out.println("Null Pointer Exception caught.."+n);
}
System.out.println("Hello");
}
}
Output :-
25 Write a Java program that demonstrate the use of nested try block.
class exceptiondemo
{
public static void main (String [] args)
{
int a=5;
int b=0;
int x[] = new int[5];
try
{
int c=a/b;
try
{
int d=a/b;
x[6]=12;
}
catch(ArrayIndexOutOfBoundsException m)
{
System.out.println("Array Index Out Of Bound Exception caught.."+a);
}
System.out.println("Inner try..");
}
catch(ArithmeticException n)
{
System.out.println("Arithmetic Exception caught..\n"+n);
}
System.out.println("Outer try..");
}
}
186150316006 Page no : 33
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
Output :-
import java.util.*;
class InsufficientFund extends Exception
{
String msg;
InsufficientFund(String msg)
{
this.msg=msg;
}
public String toString()
{
return "Sorry !!" +msg;
}
}
class account
{
int acc_no, balance, amount;
account (int b,int a)
{
balance = b;
acc_no = a;
}
void display()
{
System.out.println("Balance : "+balance);
System.out.println("Account Number : "+acc_no);
}
186150316006 Page no : 34
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
void withdraw(int amount)
{
if(amount>balance)
{
try
{
throw new InsufficientFund("\nInSufficientFund Exception..");
}
catch(InsufficientFund v)
{
System.out.println("Exception Caught..."+v);
}
System.out.println("Wthdrawal amount : "+amount);
}
else
{
balance = balance - amount;
System.out.println(amount+ " Withdraw Successfully...");
}
}
}
class main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
account a = new account (2000,100);
a.withdraw(4000);
a.display();
}
}
Output :-
186150316006 Page no : 35
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
"Thread1" every 2,000 milliseconds, and the other displays "Thread2"
every 4,000 milliseconds. Create the threads by extending the Thread class
td1.start();
td2.start();
}
186150316006 Page no : 36
Government Polytechnic for Girls,Surat (IT DEPARTMENT )
Subject : Java Programming
}
Output :-
28 Write a program that executes two threads. One thread will print the
even numbers and the other thread will print odd numbers from 1 to 50.
29 Write a program in Java to demonstrate use of synchronization of
threads when multiple threads are trying to update common variable.
30 Write a program in Java to create, write, modify, read operations on a
text file.
186150316006 Page no : 37