Practical: 1: Government Polytechnic For Girls, Surat
Practical: 1: Government Polytechnic For Girls, Surat
Practical: 1: Government Polytechnic For Girls, Surat
Practical : 1
AIM : Write down steps for java environment setup & write a simple “Hello
World” or similar java program, compilation, debugging, executing using java
compiler and interpreter.
Prerequisite
Run the .exe file and follow the instructions to install java on your machine.
Go to Control panel--> System and Security-->System.
Under the advance system settings option click on Environment Variable
You have to alter the “PATH” variable under System variables so that it also
contains the path to the Java environment.
Select the “PATH” variable and click on Edit button as highlighted below.
You will see list of different paths, click on New button and then add path where
java is installed.
By default, java is installed in “C:\Program Files\Java\jdk\bin”
In case, you have installed java at any other location, then add that path.
Click on OK, Save the settings and you are done !! Now to check whether
installation is done correctly, open command prompt and type javac -version.
You will see that java version is running on your machine.
In order to make sure whether compiler is setup, type javac in command prompt.
You will see a list related to javac.
class Demo
{
public static void main(String args[])
{
System.out.println("Hello");
}
}
Practical : 2
Output :-
Enter Number 1 :
68
Enter Number 2 :
93
Enter Number 3 :
59
Maximum number is : 93
PRactical : 3
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 :-
Enter the number you have to enter: 4
Enter numbers :
10
39
58
26
Second maximum Number is 39
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 :-
Enter Number :
54321
Reverse of number :
12345
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;
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 :-
NUMBER IN WORD :
FOUR NINE ZERO THREE TWO
Process finished.
Practical : 7
AIM : Write programs in Java to use Wrapper class of each primitive data types.
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
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();
}
}
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 :-
Enter Matrix A :
1739
2782
1903
4721
Ente Matrix B :
2385
1029
4827
1938
Process finished.
Practical : 9
Class thiskey) {
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 :-
X=4
Y=5
Practical : 10
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 :-
A = 30
Practical : 11
Aim : (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;
}
public static void main(String args[])
{
value v = new value ();
System.out.println("Value of a : "+v.);
v.data(70);
System.out.println("after Chang Value of a : "+v.a);
}
}
Output :-
Value of a : 60
After Chang Value of a : 60
AIM : (b) Write a java program that make use of pass by reference in method.
class value
{
int a=60;
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 :-
Value of a : 60
After Chang Value of a : 100
Practical : 12
AIM : (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 :-
Value of l : 30
AIM : (b) Write a java program that make use of method returning
value.
class Test
{
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 :-
Length of t1 object : 40
Breadth of t1 object : 50
Length of t2 object : 10
Breadth of t2 object : 20
Practical : 13
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 :-
Hello
Value of I = 10
Value of J = 20
Hello World
Practical : 14
Output :-
Practical : 15
Output :-
Student Id : 6
Student name : Vaishali
Student Id : 6
Student name : Vaishali
Practical : 16
Output :-
Volume of box b1 :0.0
Volume of box b2 :64.0
Volume of box b3 :6.0
Practical : 17
AIM : (a).Write a java program that demonstrate the use of single inheritance.
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 :-
Value of a = 3
Value of a = 4
AIM : (b).Write a java program that demonstrate the use of multilevel inheritance.
class student
{
int no;
void setdata(int no)
{
this.no=no;
}
void getdata()
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();
mathtecher m = new mathtecher();
m.setname("Xyz");
m.techmath();
}
}
Output :-
Abc Run bizness..
Xyz Teach Mathes..
Practical : 18
AIM : (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 :-
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;
transportationVehicle(int id , String man , int load)
{
super(id,man);
load_capacity=load;
}
void display_transportation()
{
System.out.println("Transportation Vehicle");
display_vehicle();
System.out.println("Load Capacity : "+load_capacity);
}
}
class passengerVehicle extends vehicle
{
int no_passenger;
passengerVehicle(int id, String man,int pas)
{
super(id,man);
no_passenger=pas;
void display_passenger()
{
System.out.println("Passenger Vehicle");
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 :-
Transportation Vehicle
Vehicle Id : 112233
Vehicle Company : TATA
Load Capacity : 500
Passenger Vehicle
Vehicle Id : 332211
Vehicle Company : BAJAJ
Number of passenger : 10
Practical : 19
AIM : Write program that demonstrate the concept of Dynamic method dispatch.
abstract class bank
{
abstract float getRateOfInterest();
}
class SBI extends bank
{
float getRateOfInterest()
{
return 4.5f;
}
}
class ICICI extends bank
{
float getRateOfInterest()
{
return 6.0f;
}
}
class AXIS extends bank
{
float getRateOfInterest()
{
return 7.5f;
}
}
class Personal
{
public static void main(String args[])
{
bank s = new SBI();
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 :-
Rate of Interest in SBI : 4.5
Rate of Interest in ICICI : 6.0
Rate of Interest AXIS : 7.5
Practical : 20
AIM : Write a program in Java which demonstrate the use of abstract class.
abstract class shape
{
abstract void area();
}
class rectangle extends shape
{
int a,b;
rectangle(int a,int b)
{
this.a=a;
this.b=b;
}
void area()
{
System.out.println("Area of rectangle is "+(a*b));
}
}
class circle extends shape
{
int r;
circle(int r)
{
this.r=r;
}
void area()
{
System.out.println("Area of circle is "+(3.14*r*r));
}
}
class main
{
public static void main(String args[])
{
shape s1;
s1 = new rectangle(6,5);
s1.area();
s1 = new circle(5);
s1.area();
}
}
Output :-
Area of rectangle is 30
Area of circle is 78.5
Practical : 21
AIM : Write a program that illustrates the concept of accessibility rules inside and
outside the packages.
1. Same Class :-
Protection.Java File
package pa;
public class Protection
{
int def=2;
private int pri=4;
protected int pro = 8;
public int pub=10;
Protection()
{
System.out.println("Default..."+def);
System.out.println("Private..."+pri);
System.out.println("Protected..."+pro);
System.out.println("Public..."+pub);
}
}
Demo.java file
package pa;
public class demo
{
public static void main(String[] args)
{
Protection p = new Protection();
}
}
Output :-
Default...2
Private...4
Protected...8
Public...10
Protection.java File
Package pkg;
class Protection
{
int def=2;
private int pri=4;
protected int pro = 8;
public int pub=10;
Protection()
{
System.out.println("\nProtection Class...");
System.out.println("Default..."+def);
System.out.println("Private..."+pri);
System.out.println("Protected..."+pro);
System.out.println("Public..."+pub);
}
}
Derived.java File
Package pkg;
Class Derived extends Protection
{
Derived()
{
System.out.println(“\nDerived Class…”);
System.out.println(“Default…”+def);
// System.out.println(“Private…”+pri);
System.out.println(“Protected…”+pro);
System.out.println(“Public…”+pub);
}
}
Demo.Java File
Package pkg;
Class demo
{
Public static void main(String[] args)
{
Output :-
Protection Class…
Default…2
Private…4
Protected…8
Public…10
Protection Class…
Default…2
Private…4
Protected…8
Public…10
Derived Class…
Default…2
Protected…8
Public…10
Same.java File
Package P;
Class Same
{
int def=2;
Private int pri=4;
Protected int pro = 8;
Public int pub=10;
Same()
{
System.out.println(“\nSame Class…”);
System.out.println(“Default : “+def);
System.out.println(“Private : “+pri);
System.out.println(“Protected : “+pro);
System.out.println(“Public : “+pub);
}
}
NonSub.java File
Package P;
Class NonSub
{
NonSub()
{
Same s = new Same();
System.out.println(“\nNon Sub Class…”);
System.out.println(“Default : “+s.def);
//System.out.println(“Private…”+s.pri);
System.out.println(“Protected : “+s.pro);
System.out.println(“Public : “+s.pub);
}
}
Demo.java File
Package P;
Class Demo
{
Public static void main(String[] args)
{
Same s = new Same();
NonSub n = new NonSub();
}
}
Output :-
Same Class…
Default : 2
Private : 4
Protected : 8
Public : 10
Same Class…
Default : 2
Private : 4
Protected : 8
Public : 10
Protection.java File
Package Pka;
Import Pki.*;
Public class Protection
{
Int def=2;
Private int pri=4;
Protected int pro = 8;
Public int pub=10;
Public Protection()
{
System.out.println(“\nPka Package Protection Class…”);
System.out.println(“Default : “+def);
System.out.println(“Private : “+pri);
System.out.println(“Protected : “+pro);
System.out.println(“Public : “+pub);
}
}
Demo.java File
Package Pka;
Class Demo
{
Public static void main(String[] args)
{
Protection p = new Protection ();
}
}
PkiSub.java File
Package Pki;
Import Pka.*;
Public class PkiSub extends Protection
{
PkiSub()
{
System.out.println(“\nPki Package PkiSub Class : “);
// System.out.println(“Default : “+def);
//System.out.println(“Private : “+pri);
System.out.println(“Protected : “+pro);
System.out.println(“Public : “+pub);
}
}
OtherDemo.java File
Package Pki;
Class OtherDemo
{
Public static void main(String[] args)
{
PkiSub r = new PkiSub();
}
}
Output :-
Protection.java File
Package Poa;
Public class Protection
{
int def=2;
Private int pri=4;
Protected int pro = 8;
Public int pub=10;
Public Protection()
{
Demo.java File.
Package Poa;
Public class Demo
{
Public static void main(String[] args)
{
Protection pk = new Protection();
}
}
PoiNonSub.java File
Package Poi;
Import Poa.*;
Class PoiNonSub
{
Protection a = new Protection();
PoiNonSub()
{
System.out.println(“\nPoi Package Poi Non Sub Class : “);
//System.out.println(“Default : “+a.def);
//System.out.println(“Private : “+a.pri);
//System.out.println(“Protected : “+a.pro);
System.out.println(“Public : “+a.pub);
}
}
OtherDemo.java File
Package Poi;
Public class OtherDemo
{
Public static void main(String args[])
{
Output :-
Demo.java File Execution
Poa Package Protection Class…
Default : 2
Private : 4
Protected : 8
Public : 10
Practical : 22
Practical : 23
Write a java program to develop userdefined exception for "Divide by zero" error.
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[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers : ");
int a = sc.nextInt();
int b = sc.nextInt();
if(b==0)
{
try
{
throw new DividedByZero("\nDivide by Zero");
}
catch(DividedByZero e)
{
System.out.println("Exception Caught..."+e);
}
}
else
{
System.out.println("Answer : "+(a/b));
}
}
}
Output :-
Enter two numbers :
50
Exception Caught…
Divide by Zero
Practical : 24
AIM : 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);
}
catch(NullPointerException n)
{
System.out.println("Null Pointer Exception caught.."+n);
}
System.out.println("Hello");
}
}
Output :-
ArithmeticException caught..5
Hello
Practical : 25
AIM : 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..");
}
}
Output :-
Arithmetic Exception caught..
Java.lang.ArithmeticException: / by zero
Outer try..
Practical : 26
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);
}
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 :-
Exception Caught…Sorry !!
InSufficientFund Exception..
Wthdrawal amount : 4000
Balance : 2000
Account Number : 100
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
}
}
class threaddemo
{
public static void main (String args[])
{
thread1 td1 = new thread1();
thread2 td2 = new thread2();
td1.start();
td2.start();
}
}
Output :-
Thread1..
Thread2..
Thread1..
Thread1..
Thread2..
Thread1..
Thread1..
Thread2..
Thread1..
Thread2..
Thread1..
Thread1..
Thread2..
Thread1..
Thread1..
Thread2..
Thread1..
Thread1..
Thread2..
Thread1..
Thread1..
Thread2..
Thread1..
Practical : 28
AIM : 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.
class TwoThread
{
public static void main(String arg[])
{
Odd e1=new Odd("ODD");
e1.start();
Even e2=new Even("EVEN");
e2.start();
}
}
Output :-
Thread-0
Odd Numbers from 1 To 50 :-
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
Thread-1
Even Numbers from 1 To 50 :-
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
Process finished.
Practical : 29
Practical : 30
AIM : Write a program in Java to create, write, modify, read operations on a text
file.
import java.io.*;
public class FileDemo
{
public static void main(String args[])
{
System.out.println("File Created !!\n");
String s = "Hello World";
byte b[] = s.getBytes();
try
{
FileOutputStream fout = new FileOutputStream("./file.txt");
fout.write(b);
fout.close();
}
catch(Exception e)
{
System.out.println("Exception Caught..\n"+e);
}
System.out.println("\nWriting Complete !!\n");
try
{
FileOutputStream fout=new FileOutputStream("./file.txt");
s="Good Morning";
b=s.getBytes();
fout.write(b);
fout.close();
}
catch(Exception e)
{
System.out.println("Exception Caught..\n"+e);
}
System.out.println("\nModification Complete !!\n");
try
{
FileInputStream fin = new FileInputStream("./file.txt");
int i;
{
System.out.println((char)i);
}
fin.close();
}
catch(Exception e)
{
System.out.println("Exception Caught..\n"+e);
}
System.out.println("\nReading Complete !!\n");
}
}
Output :-
File Created !!
Exception Caught..
Java.io.FileNotFoundException: ./file.txt (Permission denied)
Writing Complete !!
Exception Caught..
Java.io.FileNotFoundException: ./file.txt (Permission denied)
Modification Complete !!
Exception Caught..
Java.io.FileNotFoundException: ./file.txt (No such file or directory)
Reading Complete !!
Process finished.