0% found this document useful (0 votes)
97 views52 pages

Practical: 1: Government Polytechnic For Girls, Surat

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 52

Government Polytechnic For Girls , Surat

Information Technology Department


Java Programming (3350703)

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

• Java Development Kit [ JDK ]


• Java Runtime Environment [ JRE ]
• Java Virtual Machine [ JVM ]

 Download JDK from Oracle website

 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.

Enrolment no : 186150316006 Page no : 1


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

 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.

Enrolment no : 186150316006 Page no : 2


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

 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.

 Example of Java Programming

class Demo
{
public static void main(String args[])
{
System.out.println("Hello");
}
}

Compilation :- Javac Demo.java


Execution :- java Demo
Output :- Hello

Enrolment no : 186150316006 Page no : 3


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 :-

Enter Number 1 :
68
Enter Number 2 :
93
Enter Number 3 :
59
Maximum number is : 93

Enrolment no : 186150316006 Page no : 4


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 :-
Enter Number:
5
First 5 prime numbers are :
2
3
5
7
11

Enrolment no : 186150316006 Page no : 5


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 6


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 7


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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;

Enrolment no : 186150316006 Page no : 8


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

default :

System.out.println("invalid");
}
no = no/10;
}
System.out.println("\nNUMBER IN WORD :\n"+a);
}
}

Output :-

ENTER ANY NUMBER :


49032

NUMBER IN WORD :
FOUR NINE ZERO THREE TWO

Process finished.

Enrolment no : 186150316006 Page no : 9


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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;

Byte b1 = new Byte(b);


Integer i1 = new Integer(i);
Float f1 = new Float(f);
Short s1 = new Short(s);
Double d1 = new Double(d);

System.out.println("Value of wrapping objects.. ");


System.out.println("Byte Object = "+b1);
System.out.println("Integer Object = "+i1);
System.out.println("Float Object = "+f1);
System.out.println("Short Object = "+s1);
System.out.println("Double Object = "+d1);
}
}

Output :-

Value of wrapping objects..


Byte Object = 3
Integer Object = 10
Float Object = 2.5
Short Object = 20
Double Object = 40.5

Enrolment no : 186150316006 Page no : 10


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 8

AIM : Write a program in Java to multiply two matrix.

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;

Enrolment no : 186150316006 Page no : 11


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 number of matrix :


4

Enter Matrix A :
1739
2782
1903
4721

Ente Matrix B :
2385
1029
4827
1938

Multiplication of Matrix A and B :


30 108 55 161
45 88 52 145
14 30 35 110
24 37 53 105

Process finished.

Enrolment no : 186150316006 Page no : 12


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 9

AIM : Write a program in Java to demonstrate use of this keyword.

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

Enrolment no : 186150316006 Page no : 13


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 10

AIM : Write a program in Java to demonstrate use of final keyword.

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

Enrolment no : 186150316006 Page no : 14


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 15


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 16


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 17


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 13

AIM : Write a java program that demonstrate the use of static


keyword.

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

Enrolment no : 186150316006 Page no : 18


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 14

AIM : Write a program in Java to demonstrate the use of private constructor.


class book
{
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 :-

Book Title : Abc


Book Author Name : Xyz
Book Price : 100

Enrolment no : 186150316006 Page no : 19


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 15

AIM : Write a program in Java to demonstrate the use of copy constructor.


class CopyConst
{
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 :-
Student Id : 6
Student name : Vaishali
Student Id : 6
Student name : Vaishali

Enrolment no : 186150316006 Page no : 20


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 16

AIM : Write a program in Java to demonstrate the use of constructor overloading.


class box
{
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 :-
Volume of box b1 :0.0
Volume of box b2 :64.0
Volume of box b3 :6.0

Enrolment no : 186150316006 Page no : 21


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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()

Enrolment no : 186150316006 Page no : 22


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)
{
System.out.println("Roll number : "+no);
}
}
class Test extends student
{
int s1,s2;
void setmarks(int s1,int s2)
{
this.s1=s1; this.s2=s2;
}
void show()
{
System.out.println("Subject 1 Marks : "+s1);
System.out.println("Subject 2 Marks : "+s2);
}
}
class Result extends Test
{
void display()
{
int total=s1+s2;
System.out.println("Total Marks : "+total);
}
}
class main
{
public static void main (String args[])
{
Result r = new Result();
r.setdata(06);
r.getdata();
r.setmarks(45,75);
r.show();
r.display();
}
}
Output :-
Roll number : 6
Subject 1 Marks : 45
Subject 2 Marks : 75
Total Marks : 120

Enrolment no : 186150316006 Page no : 23


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

(c)Write a java program that demonstrate the use of hierarchical inheritance.

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..

Enrolment no : 186150316006 Page no : 24


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 :-

Value of X in Superclass : 100


Value of X in Subclass : 200

Enrolment no : 186150316006 Page no : 25


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

AIM : (b).Write a java program in which subclass constructor invokes


the constructor of super class using super keyword.

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;

Enrolment no : 186150316006 Page no : 26


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)
}

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

Enrolment no : 186150316006 Page no : 27


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 28


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 29


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 30


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

2. Same Package Sub Class :-

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)
{

Enrolment no : 186150316006 Page no : 31


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Protection p =new Protection();


Derived d = new Derived();
}
}

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

3. Same Package Non Sub Class :-

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);
}
}

Enrolment no : 186150316006 Page no : 32


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Non Sub Class…


Default : 2
Protected : 8
Public : 10

Enrolment no : 186150316006 Page no : 33


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

4. Different Package Sub Class

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);
}
}

Enrolment no : 186150316006 Page no : 34


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

OtherDemo.java File
Package Pki;
Class OtherDemo
{
Public static void main(String[] args)
{
PkiSub r = new PkiSub();
}
}

Output :-

Demo java Main class execution


Pka Package Protection Class…
Default : 2
Private : 4
Protected : 8
Public : 10

Other Demo.java Main class execution


Pka Package Protection Class...
Default : 2
Private : 4
Protected : 8
Public : 10

Pki Package PkiSub Class :


Protected : 8
Public : 10

5. Different Package Non Sub Class

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()
{

Enrolment no : 186150316006 Page no : 35


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

System.out.println(“\nPoa 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 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[])
{

Enrolment no : 186150316006 Page no : 36


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

PoiNonSub ps = new PoiNonSub();


}
}

Output :-
Demo.java File Execution
Poa Package Protection Class…
Default : 2
Private : 4
Protected : 8
Public : 10

OtherDemo.java File Execution


Poa Package Protection Class…
Default : 2
Private : 4
Protected : 8
Public : 10

Poi Package Poi Non Sub Class :


Public : 10

Enrolment no : 186150316006 Page no : 37


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 22

AIM : Write a program in Java to demonstrate implementation of multiple


inheritance using interfaces.
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()
{
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 :-
PDF is generated..
4 Copies are generated..
Document is printing...

Enrolment no : 186150316006 Page no : 38


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 39


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 40


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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..

Enrolment no : 186150316006 Page no : 41


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

Practical : 26

AIM : Write a program to develop a userdefined exception "Not sufficient fund"


for an Account class.

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)

Enrolment no : 186150316006 Page no : 42


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

{
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

Enrolment no : 186150316006 Page no : 43


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 thread1 extends Thread


{
public void run()
{
while (true)
{
System.out.println("Thread1..");
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println("Exception Caught.."+e);
}
}
}
}
class thread2 extends Thread
{
public void run()
{
while(true)
{
System.out.println("Thread2..");
try
{
Thread.sleep(4000);
}
catch(InterruptedException e)
{
System.out.println("Exception Caught.."+e);
}
}

Enrolment no : 186150316006 Page no : 44


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

}
}
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..

Enrolment no : 186150316006 Page no : 45


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 Odd extends Thread


{
String name;
int i;
Odd(String s)
{
name = s;
}
public void run()
{
for( i=0;i<5;i++)
{
System.out.println(Thread.currentThread().getName());
try
{
if(name.equals("ODD"))
{
System.out.println("Odd Numbers from 1 To 50 :- ");
for(i=0;i<50;i++)
{
if(i%2!=0)
{
System.out.println(+i);
}
}
}
}
catch(Exception e)
{
System.out.println("Exception Caught.."+e);
}
}
}
}

Enrolment no : 186150316006 Page no : 46


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

class Even extends Thread


{
String name;
Even(String s)
{
name = s;
}
public void run()
{
for(int j=0;j<5;j++)
{
System.out.println(Thread.currentThread().getName());
try
{
if(name.equals("EVEN"))
{
System.out.println("Even Numbers from 1 To 50 :- ");
for(j=0;j<50;j++)
{
if(j%2==0)
{
System.out.println(+j);
}
}
}
}
catch(Exception e)
{
System.out.println("Exception Caught.."+e);
}
}
}
}

class TwoThread
{
public static void main(String arg[])
{
Odd e1=new Odd("ODD");
e1.start();
Even e2=new Even("EVEN");
e2.start();
}
}

Enrolment no : 186150316006 Page no : 47


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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

Enrolment no : 186150316006 Page no : 48


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

20
22
24
26
28
30
32
34
36
38
40
42
44
46
48

Process finished.

Enrolment no : 186150316006 Page no : 49


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)

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 Caller
{
synchronized public void display(String msg)
{
System.out.print("["+msg);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.print("]");
}
}
class CallerThread extends Thread
{
Caller c;
String s;
CallerThread(Caller c,String s)
{
this.c=c; this.s=s;
}
public void run()
{
c.display(s);
}
}
class SynchronisationDemo2
{
public static void main(String[] args)
{
Caller cr = new Caller();
CallerThread cth1 = new CallerThread(cr,"good");
CallerThread cth2 = new CallerThread(cr,"morning");
cth1.start();
cth2.start();
}
}
Output :-

Enrolment no : 186150316006 Page no : 50


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)
[good][morning]

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;

Enrolment no : 186150316006 Page no : 51


Government Polytechnic For Girls , Surat
Information Technology Department
Java Programming (3350703)
while((i=fin.read())!=-1)

{
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.

Enrolment no : 186150316006 Page no : 52

You might also like