Вы находитесь на странице: 1из 161

LIST OF JAVA PROGRAMS:-

S.No

Programs

PgNo.

1

Program to Demonstrate simple JAVA program

4

2

Program to compute the area of a circle

5

3

Program to demonstrate type casting in JAVA

6

4

Program to demonstrate the basic Arithmetic Operations

7

5

Program to find largest of three numbers

8

6

Program to print first 10 Fibonacci numbers

9

7

Program to Calculate the factorial of a number using RECURSION

10

8

Program to print prime numbers using CLA

11

9

Program to Demonstrate Command Line Argument

12

10

Program to convert temperature from Fahrenheit to Celsius using CLA

13

11

Program to produce conversion table dollar and rupees using CLA

14

12

Program to print area of right angled triangle using CLA

15

13

Program to print all the elements in an array using CLA

16

14

Program to find the sum of all the elements in an array

17

15

Program to print all the elements in a 2D array

18

16

Program to find the sum of two matrices

19

17

Program to find the difference of two matrices using CLA

20

18

Program to print the UPPER and LOWER triangle of a matrix

21

19

Program to find the trace of a matrix

22

20

Program to find the transpose of a matrix

23

21

Program to find the product of two matrices

24

22

Program to demonstrate a CLASS

25

23

Program to demonstrate a Constructor

26

24

Program to demonstrate a Parameterized Constructor

27

25

Program to Demonstrate THIS keyword

28

26

Program to Demonstrate Constructor Overloading

29

27

Program to pass objects as parameters

31

28

Program to Demonstrate Queue using Class

32

29

Program to Demonstrate Stack using Class

34

30

Program to Demonstrate Access Specifiers

36

31

Program to Demonstrate Static Variables

37

32

Program to Demonstrate Static Methods

38

33

Program to demonstrate an Inner class

39

34

Program to Demonstrate Type Wrapping

40

35

Program to Demonstrate Single Inheritance

41

36

Program to Demonstrate Multilevel Inheritance

43

37

Program to Demonstrate hierarchical Inheritance

45

38

Program to show SUPER Class variable to refer a sub class object

47

39

Program to Demonstrate Method Overriding

48

40

Program to Demonstrate Runtime Polymorphism

49

41

Program to Demonstrate Abstract Class

51

Runtime Polymorphism 49 41 Program to Demonstrate Abstract Class 51 © www.udaysatya.blogspot.com Page 1
Runtime Polymorphism 49 41 Program to Demonstrate Abstract Class 51 © www.udaysatya.blogspot.com Page 1

42

Program to Demonstrate FINAL Keyword

52

43

Program to Demonstrate FINAL Method

53

44

Program to Demonstrate FINAL Class

54

45

Program to Demonstrate INTERFACE

55

46

Program to access implementation through Interface references

56

47

Program to Demonstrate EXTENDING INTERFACE

58

48

Program to Demonstrate Stack using interface

59

49

Program to Demonstrate Queue using Interface

61

50

Program to Demonstrate a SIMPLE PACKAGE

63

51

Program to Demonstrate Exception

64

52

Program to Demonstrate TRY CATCH

65

53

Program to Demonstrate CATCH ARITHMETIC Exceptions

66

54

Program to Demonstrate Multiple CATCH

67

55

Program to Demonstrate NESTED TRY

68

56

Program to Demonstrate FINALLY clause

69

57

Program to Demonstrate User Defined EXCEPTIONS

70

58

Program to Demonstrate Throws

71

59

Program to Demonstrate MULTITHREADING

72

60

Program to Demonstrate Runnable Interface

74

61

Program to Demonstrate Thread Priorities

76

62

Program to Demonstrate Synchronized Block

78

63

Program to Demonstrate Synchronized Method

80

64

Program to Demonstrate Deadlock

82

65

Program to Demonstrate Thread Methods

84

66

Program to Create Threads using Thread Class

86

67

Program to Demonstrate Join() & isAlive() methods

88

68

Program to Show how to Get User Input from Keyboard

90

69

Program to Create a Frame Window using Applet

91

70

Program to Demonstrate Life cycle of methods in Applet

93

71

Program to display Parameters Passed from HTML

94

72

Program to Demonstrate all shapes in Graphics class

96

73

Program to Show a Hut,Mountains & Face

97

74

Program to Show Status of Applet Window

99

75

Program to Show Position of the Mouse

100

76

Program to Demonstrate Colors

102

77

Program to Pass Parameters perform Mathematical Operations

103

78

Program To Get Document Code Bases

104

79

Program to Show an Image

105

80

Program to demonstrate an Adapter

106

81

Program to Demonstrate Simple Banner

108

82

Program to Demonstrate Button

110

83

Program to Demonstrate Checkbox

112

84

Program to Demonstrate Radio Buttons (Checkbox Group)

114

85

Program to Demonstrate Choice

116

Buttons (Checkbox Group) 114 85 Program to Demonstrate Choice 116 © www.udaysatya.blogspot.com Page 2
Buttons (Checkbox Group) 114 85 Program to Demonstrate Choice 116 © www.udaysatya.blogspot.com Page 2

86

Program to Demonstrate File Dialog

117

87

Program to Demonstrate Labels

121

88

Program to Demonstrate Lists

122

89

Program to Demonstrate Menu

124

90

Program to Demonstrate Text Area

128

91

Program to Make a Login Window

129

92

Program to Make a Login Focus

131

93

Program to Demonstrate Mouse Adapter

133

94

Program to Demonstrate Mouse Events

134

95

Program to Demonstrate the key event handlers

136

96

Program to Demonstrate Border Layout

138

97

Program to Demonstrate Card Layout

139

98

Program to Test Fonts Using Mouse Events

141

99

Program to Demonstrate Grid Layout

143

100

Program to Demonstrate Scrollbar

144

101

Program to Check Connection to Database

146

102

Program to retrieve Records from Database

147

103

Program to demonstrate Insertion into Database

148

104

Program to demonstrate Update into Database

149

105

Program to demonstrate Prepared Statement

150

106

Program to Demonstrate Data Entry using Applet

151

107

Program to Demonstrate Result Set Meta Data

154

108

Program to Demonstrate Transaction handling

155

109

Program to Demonstrate Callable Statement

156

110

Program to Demonstrate Result Set Meta Data

157

111

Program to Demonstrate Database Meta Data

158

112

Program to add Interface

159

113

Program to add Server

159

114

Program to add Client

160

115

Program for implementation for the interface

161

116

Create a Policy to Allow Permission for Everyone

161

interface 161 116 Create a Policy to Allow Permission for Everyone 161 © www.udaysatya.blogspot.com Page 3
interface 161 116 Create a Policy to Allow Permission for Everyone 161 © www.udaysatya.blogspot.com Page 3

01 Program to Demonstrate simple JAVA program

class A

{

public static void main(String args[])

{

System.out.println("Welcome to my World");

}

}

-----------------------------------------------------------------------------------------

Save the above Program as A.java

Compile Program in Command by using javac C:\>javac A.java

Run the Program in Command by using java C:\>java A

---------------------------------------------------------------------------------------------

OUTPUT :

Welcome to my World

OUTPUT : Welcome to my World © www.udaysatya.blogspot.com Page 4
OUTPUT : Welcome to my World © www.udaysatya.blogspot.com Page 4

02

Program to compute the area of a circle

class A2

{

public static void main(String args[])

{

int r=10; System.out.println("Radius of Circle :" + r);

System.out.println("Area of Circle :" + r*r);

}

}

OUTPUT :

Radius of Circle : 10 Area of Circle : 314

of Circle :" + r*r); } } OUTPUT : Radius of Circle : 10 Area of
of Circle :" + r*r); } } OUTPUT : Radius of Circle : 10 Area of

03

Program to demonstrate type casting in JAVA

class conversion

{

public static void main(String args[])

{

byte b; int i=257; double d=323.142; System.out.println("\nConversion of int to byte."); b=(byte) i; System.out.println("i and b " +i+" "+b); System.out.println("\nConversion of double to int.");

i=(int)d;

System.out.println("d and i " +d +" "+i); System.out.println("\nConversion of double to byte."); b=(byte)d; System.out.println("d and b " +d + " " +b);

}

}

OUTPUT :

Conversion of int to byte. i and b 257 1

Conversion of double to int.

d and i 323.142 323

Conversion of double to byte.

d and b 323.142 67

double to int. d and i 323.142 323 Conversion of double to byte. d and b
double to int. d and i 323.142 323 Conversion of double to byte. d and b

04

Program to demonstrate the basic Arithmetic Operations

class arith

{

public static void main(String args[])

{

int a=10,b=3; System.out.println("sum is "+(a+b));

System.out.println("difference is "+(a-b)); System.out.println("product is "+(a*b)); System.out.println("quotient is "+(a/b)); System.out.println("remainder is "+(a%b));

}

}

OUTPUT :

sum is 13 difference is 7 product is 30 quotient is 3 remainder is 1

} } OUTPUT : sum is 13 difference is 7 product is 30 quotient is 3
} } OUTPUT : sum is 13 difference is 7 product is 30 quotient is 3

05

Program to find largest of three numbers

class large

{

public static void main(String args[])

{

int a=10,b=30,c=20; System.out.println("Greatest of given 3 numbers");

if(a>b&&a>c) System.out.println(+a); else if(b>c) System.out.println(+b); else System.out.println(+c);

}

}

OUTPUT :

Greatest of given 3 numbers

30

else System.out.println(+c); } } OUTPUT : Greatest of given 3 numbers 30 © www.udaysatya.blogspot.com Page 8
else System.out.println(+c); } } OUTPUT : Greatest of given 3 numbers 30 © www.udaysatya.blogspot.com Page 8

06

Program to print first 10 Fibonacci numbers

class fib

{

public static void main(String args[])

{

int i=9,f=0,s=1,temp; System.out.println(+f);

while(i>0)

{

System.out.println(+s);

temp=f+s;

f=s;

s=temp;

i--;

}

}

}

OUTPUT :

0

1

1

2

3

5

8

13

21

34

temp=f+s; f=s; s=temp; i--; } } } OUTPUT : 0 1 1 2 3 5 8
temp=f+s; f=s; s=temp; i--; } } } OUTPUT : 0 1 1 2 3 5 8

07

Program to Calculate the factorial of a number using RECURSION

class Factorial

{

int fact(int n)

{

int result;

if(n==1)

return 1;

result=fact(n-1)*n;

return result;

}

}

class Recursion

{

public static void main(String args[]){ Factorial f=new Factorial(); System.out.println("Factorial of 3 is " +f.fact(3)); System.out.println("Factorial of 4 is " +f.fact(4));

System.out.println("Factorial of 5 is " +f.fact(5));

}

}

OUTPUT :

Factorial of 3 is 6 Factorial of 4 is 24 Factorial of 5 is 120

} } OUTPUT : Factorial of 3 is 6 Factorial of 4 is 24 Factorial of
} } OUTPUT : Factorial of 3 is 6 Factorial of 4 is 24 Factorial of

08

Program to print prime numbers using CLA

class primecla

{

public static void main(String s[])

{

int n,r;

n=Integer.parseInt(s[0]);

for(int i=2;i<=n;i++)

{

int c=0;

for(int j=1;j<=i;j++)

{

r=i%j;

if (r==0)

c+=1;

}

if(c==2)

System.out.println(i);

}}}

OUTPUT :

java primecla 10

2

3

5

7

c+=1; } if(c==2) System.out.println(i); }}} OUTPUT : java primecla 10 2 3 5 7 © www.udaysatya.blogspot.com
c+=1; } if(c==2) System.out.println(i); }}} OUTPUT : java primecla 10 2 3 5 7 © www.udaysatya.blogspot.com

09

Program to Demonstrate Command Line Argument

class CommandLine

{

public static void main(String args[])

{

for(int i=0; i<args.length; i++) System.out.println("args[" +i + "]: "+args[i]);

}

}

OUTPUT :

java CommandLine 1 3 4 2 5 args[0]: 1 args[1]: 3 args[2]: 4 args[3]: 2 args[4]: 5

java CommandLine 1 3 4 2 5 args[0]: 1 args[1]: 3 args[2]: 4 args[3]: 2 args[4]:
java CommandLine 1 3 4 2 5 args[0]: 1 args[1]: 3 args[2]: 4 args[3]: 2 args[4]:

10

Program to convert temperature from Fahrenheit to Celsius scale using CLA

class ftoccla

{

public static void main(String s[])

{

int f; double c;

f=Integer.parseInt(s[0]);

c=5*(f-32)/9;

System.out.println(f+" fahrenheit = "+c+" celsius");

}

}

OUTPUT :

java ftoccla 34

34 fahrenheit = 1.0 celsius

celsius"); } } OUTPUT : java ftoccla 34 34 fahrenheit = 1.0 celsius © www.udaysatya.blogspot.com Page
celsius"); } } OUTPUT : java ftoccla 34 34 fahrenheit = 1.0 celsius © www.udaysatya.blogspot.com Page

11

Program to produce the conversion table for dollar and rupees using CLA

class dollarcla

{

public static void main(String s[])

{

int n,d;

n=Integer.parseInt(s[0]);

System.out.println("Dollars\t\tRupees" ); for(int i=1;i<=n;i++)

{

d=Integer.parseInt(s[i]); System.out.println(d+"\t=\t"+(d*45) );

}

}

}

OUTPUT :

java dollarcla 4 1 34 36 37

Dollars

Rupees

1

=

45

34

=

1530

36

=

1620

37

=

1665

Rupees 1 = 45 34 = 1530 36 = 1620 37 = 1665 © www.udaysatya.blogspot.com Page
Rupees 1 = 45 34 = 1530 36 = 1620 37 = 1665 © www.udaysatya.blogspot.com Page

12

Program to print area of right angled triangle using CLA

class AreaTri

{

public static void main(String s[])

{

int a,b,area;

a=Integer.parseInt(s[0]);

b=Integer.parseInt(s[1]);

area=a*b/2;

System.out.println("Area of Right angled Triangle = "+area);

}

}

OUTPUT :

java AreaTri 5 4

Area of Right angled Triangle = 10

= "+area); } } OUTPUT : java AreaTri 5 4 Area of Right angled Triangle =
= "+area); } } OUTPUT : java AreaTri 5 4 Area of Right angled Triangle =

13

Program to print all the elements in an array using CLA

class matrixcla

{

public static void main(String s[])

{

int n,m;

n=Integer.parseInt(s[0]);

System.out.println("The length of the matrix is: " +n); System.out.println("The elements of the matrix are:"); for(int i=1;i<=n;i++)

{

m=Integer.parseInt(s[i]);

System.out.println(m);

}

}

}

OUTPUT :

java matrixcla 5 10 34 36 37 99 The length of the matrix is: 5

The elements of the matrix are:

10

34

36

37

99

99 The length of the matrix is: 5 The elements of the matrix are: 10 34
99 The length of the matrix is: 5 The elements of the matrix are: 10 34

14 Program to find the sum of all the elements in an array

class Sum

{

public static void main(String args[])

{

double nums[]={1.1,1.2,1.3,1.4,1.5}; double result=0;

System.out.println("The Elements of Array are :"); for(int i=0;i<5;i++)

{

System.out.println(nums[i]);

result=result+nums[i];

}

System.out.println("Sum is " + result);

}

}

OUTPUT :

The Elements of Array are :

1.1

1.2

1.3

1.4

1.5

Sum is 6.5

result); } } OUTPUT : The Elements of Array are : 1.1 1.2 1.3 1.4 1.5
result); } } OUTPUT : The Elements of Array are : 1.1 1.2 1.3 1.4 1.5

15

Program to print all the elements in a 2D array

class Matrix

{

public static void main(String s[])

{

int a[][]= {{1,2,3},{4,5,6}}; System.out.println("Number of Row= " + a.length); System.out.println("Number of Column= " + a[1].length);

System.out.println("The elements of the matrix are : "); for(int i = 0; i <a.length; i++)

{

for(int j = 0; j < a[1].length; j++)

{

System.out.print(" "+ a[i][j]);

}

System.out.println();

}

}}

OUTPUT :

Number of Row= 2 Number of Column= 3

The elements of the matrix are :

1

4

2 3

5 6

Number of Row= 2 Number of Column= 3 The elements of the matrix are : 1
Number of Row= 2 Number of Column= 3 The elements of the matrix are : 1

16 Program to find the sum of two matrices

class addm

{

public static void main(String args[])

{

int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int b[][]={{1,1,1},{0,1,0},{0,0,1}};

int c[][]= new int[3][3]; int i,j;

for(i=0;i<3;i++)

for( j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j];

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

System.out.print(+c[i][j]+ " "); System.out.println();

}

}

}

OUTPUT :

2

4

7

3

6

8

4

6

10

" "); System.out.println(); } } } OUTPUT : 2 4 7 3 6 8 4 6
" "); System.out.println(); } } } OUTPUT : 2 4 7 3 6 8 4 6

17 Program to find the difference of two matrices using CLA

class Matrixcla { public static void main(String s[]) { int a[][]=new int[2][2]; int b[][]=new int[2][2]; int x,y,k=0; System.out.println("First Matrix : "); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { x=Integer.parseInt(s[k]); a[i][j]=x; k++; System.out.print(" "+ a[i][j]); } System.out.println(); } System.out.println("Second Matrix : "); for(int i = 0; i <2; i++) { for(int j = 0; j <2; j++) { y=Integer.parseInt(s[k]); b[i][j]=y; k++; System.out.print(" "+b[i][j]); } System.out.println(); } System.out.println("Difference of two matrix : "); for(int i = 0; i <2; i++) { for(int j = 0; j <2; j++) System.out.print(" "+(a[i][j]-b[i][j])); System.out.println(); }}}

OUTPUT :

java Matrixcla 6 7 8 9 5 5 5 5 First Matrix :

6

8

7

9

Second Matrix :

5

5

5

5

Difference of two matrix :

1

3

2

4

First Matrix : 6 8 7 9 Second Matrix : 5 5 5 5 Difference of
First Matrix : 6 8 7 9 Second Matrix : 5 5 5 5 Difference of

18

Program to print the UPPER and LOWER triangle of a matrix

class UppLow{ public static void main(String arsg[]) { int a[][]={{1,2,3},{4,5,6},{7,8,9}}; System.out.println("the given matrix:"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) System.out.print(" "+a[i][j]); System.out.println(); } System.out.println("upper triangle of matrix:"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i<=j) System.out.print(a[i][j] + " "); else System.out.print(" "); } System.out.println(); } System.out.println("lower triangle of matrix:"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i>=j) System.out.print(a[i][j] + " "); else System.out.print(" "); } System.out.println(); }}}

OUTPUT :

The given matrix:

1

4

7

Upper triangle of matrix:

1 2 3

2 3

5 6

8 9

5 6

9

Lower triangle of matrix:

1

4

7

5

8 9

triangle of matrix: 1 2 3 2 3 5 6 8 9 5 6 9 Lower
triangle of matrix: 1 2 3 2 3 5 6 8 9 5 6 9 Lower

19 Program to find the trace of a matrix

class Trace

{

public static void main(String args[])

{

int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int i,j,sum=0;

System.out.println("The given matrix : "); for(int i = 0; i <3; i++)

{

for(int j = 0; j <3; j++) System.out.print(a[i][j] + " ");

System.out.println();

}

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

if(i==j)

sum=sum+a[i][j];

}

System.out.println("Trace is : " +sum);

}

}

OUTPUT :

The given matrix :

1

4

7

Trace is : 15

2 3

5 6

8 9

is : " +sum); } } OUTPUT : The given matrix : 1 4 7 Trace
is : " +sum); } } OUTPUT : The given matrix : 1 4 7 Trace

20 Program to find the transpose of a matrix

class Transpose

{

public static void main(String args[])

{

int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int i,j;

System.out.println("The given matrix : "); for(int i = 0; i <3; i++)

{

for(int j = 0; j <3; j++) System.out.print(a[i][j] + " ");

System.out.println();

}

System.out.println("Transpose of Matrix : ");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

System.out.print(a[j][i]+ " "); System.out.println();

}

}

}

OUTPUT :

The given matrix :

1

4

7

Transpose of Matrix :

1

2

3

2

5

8

3

6

9

7

8

9

4

5

6

: The given matrix : 1 4 7 Transpose of Matrix : 1 2 3 2
: The given matrix : 1 4 7 Transpose of Matrix : 1 2 3 2

21 Program to find the product of two matrices

class Mulm

{

public static void main(String args[])

{

int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int b[][]={{1,1,1},{0,1,0},{0,0,1}};

int c[][]= new int[3][3]; int i,j,k;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

for(k=0;k<3;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

System.out.print(+c[i][j]+ " "); System.out.println();

}

}

}

OUTPUT :

1

4

7

3

9

15 16

4

10

" "); System.out.println(); } } } OUTPUT : 1 4 7 3 9 15 16 4
" "); System.out.println(); } } } OUTPUT : 1 4 7 3 9 15 16 4

22

Program to demonstrate a CLASS

class Democlass //Defining a class

{

int a=34;//Instance Variable of the class

void showa() //Method of the class

{

System.out.println("a = " +a);

}

}//End of the class

class A

{

public static void main(String args[]) { Democlass obj = new Democlass(); obj.showa();

}

}

OUTPUT :

a=34

args[]) { Democlass obj = new Democlass(); obj.showa(); } } OUTPUT : a=34 © www.udaysatya.blogspot.com Page
args[]) { Democlass obj = new Democlass(); obj.showa(); } } OUTPUT : a=34 © www.udaysatya.blogspot.com Page

23

Program to demonstrate a Constructor

class Box

{

double width;

double height;

double depth;

Box()

{

System.out.println("Constructing Box");

width=10;

height=10;

depth=10;

}

double volume()

{

return width*height*depth;

}

}

class BoxDemo6

{

public static void main(String args[]) { Box mybox1=new Box(); Box mybox2=new Box(); double vol;

vol=mybox1.volume();

System.out.println("volume of is " +vol);

vol=mybox2.volume();

System.out.println("volume of is " +vol); }}

OUTPUT :

Constructing Box Constructing Box volume of is 1000.0 volume of is 1000.0

: Constructing Box Constructing Box volume of is 1000.0 volume of is 1000.0 © www.udaysatya.blogspot.com Page
: Constructing Box Constructing Box volume of is 1000.0 volume of is 1000.0 © www.udaysatya.blogspot.com Page

24

Program to demonstrate a Parameterized Constructor

class Box

{

double width; double height; double depth; Box(double w,double h,double d)

{

width=w;

height=h;

depth=d;

}

double volume()

{

return width*height*depth;

}

}

class BoxDemo7

{

public static void main(String args[]) { Box mybox1=new Box(10,20,15); Box mybox2=new Box(3,6,9); double vol;

vol=mybox1.volume();

System.out.println("volume of is " +vol);

vol=mybox2.volume();

System.out.println("volume of is " +vol);

}

}

OUTPUT :

volume of is 3000.0 volume of is 162.0

of is " +vol); } } OUTPUT : volume of is 3000.0 volume of is 162.0
of is " +vol); } } OUTPUT : volume of is 3000.0 volume of is 162.0

25

Program to Demonstrate THIS keyword

class Box

{

double width; double height; double depth; Box(double width,double height,double depth)

{

this.width=width;

this.height=height;

this.depth=depth;

}

double volume()

{

return width* height* depth;

}

}

class BoxThis

{

public static void main(String args[])

{

double vol; Box mybox1=new Box(10,20,15); Box mybox2=new Box(3,6,9);

vol=mybox1.volume();

System.out.println("Volume is " +vol);

vol=mybox2.volume();

System.out.println("Volume is " +vol);

}

}

OUTPUT :

Volume is 3000.0 Volume is 162.0

is " +vol); } } OUTPUT : Volume is 3000.0 Volume is 162.0 © www.udaysatya.blogspot.com Page
is " +vol); } } OUTPUT : Volume is 3000.0 Volume is 162.0 © www.udaysatya.blogspot.com Page

26

Program to Demonstrate Constructor Overloading

class Box

{

double width;

double height;

double depth;

Box(double w,double h,double d)

{

width=w;

height=h;

depth=d;

}

Box()

{

width=-1;

height=-1;

depth=-1;

}

Box(double len)

{

width=height=depth=len;

}

double volume()

{

return width*height*depth;

}

}

len) { width=height=depth=len; } double volume() { return width*height*depth; } } © www.udaysatya.blogspot.com Page 29
len) { width=height=depth=len; } double volume() { return width*height*depth; } } © www.udaysatya.blogspot.com Page 29

class OverloadCons

{

public static void main(String args[]) { Box mybox1=new Box(10,20,15); Box mybox2=new Box(); Box mycube=new Box(7); double vol;

vol=mybox1.volume();

System.out.println("volume of mybox1 is " +vol);

vol=mybox2.volume();

System.out.println("volume of mybox2 is " +vol);

vol=mycube.volume();

System.out.println("volume of mycube is " +vol);

}

}

OUTPUT :

volume of mybox1 is 3000.0 volume of mybox2 is -1.0 volume of mycube is 343.0

: volume of mybox1 is 3000.0 volume of mybox2 is -1.0 volume of mycube is 343.0
: volume of mybox1 is 3000.0 volume of mybox2 is -1.0 volume of mycube is 343.0

27

Program to pass objects as parameters

class Test

{

int a,b;

Test(int i,int j)

{

a=i;

b=j;

}

boolean equals(Test o)

{

if(o.a==a && o.b==b) return true; else return false;

}

}

class PassOb

{

public static void main(String args[])

{

Test ob1=new Test(100,22); Test ob2=new Test(100,22); Test ob3=new Test(-1,-1);

System.out.println("ob1 == ob2 : " +ob1.equals(ob2)); System.out.println("ob1 == ob3 : " +ob1.equals(ob3));

}

}

OUTPUT :

ob1 == ob2 : true ob1 == ob3 : false

ob3 : " +ob1.equals(ob3)); } } OUTPUT : ob1 == ob2 : true ob1 == ob3
ob3 : " +ob1.equals(ob3)); } } OUTPUT : ob1 == ob2 : true ob1 == ob3

28

Program to Demonstrate Queue using Class

class A

{

int q[] = new int[10];

int f,s;

A()

{

f =-1;

s=-1;

}

void push(int item)

{

if(f==9)

System.out.println("Queue is Full");

else

q[++f]=item;

}

int pop()

{

if(f==s)

{

System.out.println("Queue is Underflow"); return 0;

 

}

else

 

return q[++s];

}

}

Underflow"); return 0;   } else   return q[++s]; } } © www.udaysatya.blogspot.com Page 32
Underflow"); return 0;   } else   return q[++s]; } } © www.udaysatya.blogspot.com Page 32

class que{ public static void main(String args[]){ A obj1 = new A(); for(int i=0;i<10;i++)

obj1.push(i);

System.out.println("Queue elements are : "); for(int i = 0 ;i<10;i++)

System.out.println(obj1.pop());

}

}

OUTPUT :

Queue elements are :

0

1

2

3

4

5

6

7

8

9

} } OUTPUT : Queue elements are : 0 1 2 3 4 5 6 7
} } OUTPUT : Queue elements are : 0 1 2 3 4 5 6 7

29 Program to Demonstrate Stack using Class

class A

{

int s[] = new int[10];

int tos;

A()

{

tos=-1;

}

void push(int item)

{

if(tos==9)

System.out.println("Stack is Full");

else

s[++tos]=item;

}

int pop()

{

if(tos<0)

{

System.out.println("Stack is Underflow");

 

return 0;

}

else

 

return s[tos--];

}

}

  return 0; } else   return s[tos--]; } } © www.udaysatya.blogspot.com Page 34
  return 0; } else   return s[tos--]; } } © www.udaysatya.blogspot.com Page 34

class stack{ public static void main(String args[]){ A obj1 = new A(); for(int i=0;i<10;i++)

obj1.push(i);

System.out.println("Stack elements are : "); for(int i = 0 ;i<10;i++)

System.out.println(obj1.pop());

}

}

OUTPUT :

Stack elements are :

9

8

7

6

5

4

3

2

1

0

} } OUTPUT : Stack elements are : 9 8 7 6 5 4 3 2
} } OUTPUT : Stack elements are : 9 8 7 6 5 4 3 2

30

Program to Demonstrate Access Specifiers

class Test

{

int a; public int b; private int c; void setc(int i)

{

c=i;

}

int getc() { return c;

}

}

class AcessTest

{

public static void main(String args[])

{

Test ob=new Test(); //These are OK,a and b may be accessed directly

ob.a=10;

ob.b=20;

//This is not OK and will cause an error //ob.c=100; //error! //you must access c through its methods ob.setc(100); //Ok

System.out.println("a,b,and c: " +ob.a+" " +ob.b +" "+ob.getc());

}

}

OUTPUT :

a,b,and c: 10 20 100

" +ob.b +" "+ob.getc()); } } OUTPUT : a,b,and c: 10 20 100 © www.udaysatya.blogspot.com Page
" +ob.b +" "+ob.getc()); } } OUTPUT : a,b,and c: 10 20 100 © www.udaysatya.blogspot.com Page

31

Program to Demonstrate Static Variables

class Test

{

static int count;

static

{

System.out.println("Welcome");

count=0;

}

Test()

{

count++;

}

}

class Teststaticvariable

{

public static void main(String args[])

{

Test t1=new Test(); Test t2=new Test(); Test t3=new Test();

System.out.println(Test.count + " objects are created");

}

}

OUTPUT :

Welcome 3 objects are created

+ " objects are created"); } } OUTPUT : Welcome 3 objects are created © www.udaysatya.blogspot.com
+ " objects are created"); } } OUTPUT : Welcome 3 objects are created © www.udaysatya.blogspot.com

32

Program to Demonstrate Static Methods

class Mymath

{

static int add(int x,int y)

{

return(x+y);

}

static int subtract(int x,int y)

{

return (x-y);

}

static int square(int x)

{

return (x*x);}

}

class demostaticmethod

{

public static void main(String args[])

{

System.out.println(Mymath.add(100,100));

System.out.println(Mymath.subtract(200,100));

System.out.println(Mymath.square(5));

}

}

OUTPUT :

200

100

25

System.out.println(Mymath.square(5)); } } OUTPUT : 200 100 25 © www.udaysatya.blogspot.com Page 38
System.out.println(Mymath.square(5)); } } OUTPUT : 200 100 25 © www.udaysatya.blogspot.com Page 38

33

Program to demonstrate an Inner class

class Outer

{

int outer_x=100;

void test()

{

Inner inner=new Inner();

inner.display();

}

class Inner

{

void display()

{

System.out.println("display: outer_x= "+outer_x);

}

}

}

class InnerClassDemo

{

public static void main(String args[])

{

Outer outer=new Outer(); outer.test();

}

}

OUTPUT :

display: outer_x= 100

{ Outer outer=new Outer(); outer.test(); } } OUTPUT : display: outer_x= 100 © www.udaysatya.blogspot.com Page 39
{ Outer outer=new Outer(); outer.test(); } } OUTPUT : display: outer_x= 100 © www.udaysatya.blogspot.com Page 39

34 Program to Demonstrate Type Wrapping

class Wrap {

public static void main(String args[])

{

Integer obj = new Integer(34); int x = obj.intValue(); System.out.println("x = " +x);

System.out.println("obj = " +obj);

}

}

OUTPUT :

x = 34 obj = 34

+x); System.out.println("obj = " +obj); } } OUTPUT : x = 34 obj = 34 ©
+x); System.out.println("obj = " +obj); } } OUTPUT : x = 34 obj = 34 ©

35 Program to Demonstrate Single Inheritance

class A

{

int i,j;

void showij()

{

System.out.println("i and j : " +i+ " " +j);

}

}

class B extends A

{

int k;

void showk()

{

System.out.println("k : "+k);

}

void sum()

{

System.out.println("i+j+k: "+(i+j+k));

}

}

: "+k); } void sum() { System.out.println("i+j+k: "+(i+j+k)); } } © www.udaysatya.blogspot.com Page 41
: "+k); } void sum() { System.out.println("i+j+k: "+(i+j+k)); } } © www.udaysatya.blogspot.com Page 41

class SimpleInheritance

{

public static void main(String args[])

{

A

superob = new A();

B

subob = new B();

superob.i=10;

superob.j=20;

System.out.println("Contents of superob: "); superob.showij(); System.out.println();

subob.i=7;

subob.j=8;

subob.k=9;

System.out.println("Contents of subob: "); subob.showij(); System.out.println();

System.out.println("Sum of i,j & k in subob: "); subob.sum();

}

}

OUTPUT :

Contents of superob:

i and j : 10 20

Contents of subob:

i and j : 7 8

Sum of i,j & k in subob:

i+j+k: 24

j : 10 20 Contents of subob: i and j : 7 8 Sum of i,j
j : 10 20 Contents of subob: i and j : 7 8 Sum of i,j

36

Program to Demonstrate Multilevel Inheritance

class Student

{

int rollno;

void getroll(int a)

{

rollno=a;

}

void putroll()

{

System.out.println("Roll number : "+rollno);

}

}

class Test extends Student //first level inheritence

{

int marks1,marks2; void getmarks(int x,int y)

{

marks1=x;

marks2=y;

}

void putmarks()

{

System.out.println("Marks in subject1 = "+marks1); System.out.println("Marks in subject2 = "+marks2);

}

}

= "+marks1); System.out.println("Marks in subject2 = "+marks2); } } © www.udaysatya.blogspot.com Page 43
= "+marks1); System.out.println("Marks in subject2 = "+marks2); } } © www.udaysatya.blogspot.com Page 43

class Result extends Test

{

float total;

void display()

{

//second level derivation

total=marks1+marks2;

putroll();

putmarks();

System.out.println("Total = "+total);

}

}

class Multilevel

{

public static void main(String args[])

{

Result student1 = new Result();

//object of class result is created

student1.getroll(34);

student1.getmarks(40,99);

student1.display();

}

}

OUTPUT :

Roll number : 34 Marks in subject1 = 40 Marks in subject2 = 99 Total = 139.0

: Roll number : 34 Marks in subject1 = 40 Marks in subject2 = 99 Total
: Roll number : 34 Marks in subject1 = 40 Marks in subject2 = 99 Total

37

Program to Demonstrate hierarchical Inheritance

class Student

{

int roll;

void getroll(int x)

{

roll=x;

}

void putroll()

{

System.out.println(roll);

}

}

class Result1 extends Student

{

int marks1; void getmarks1(int y)

{

marks1=y;

}

void showmarks1()

{

System.out.print("Roll no of 1st student is = "); putroll();

System.out.println("Marks of 1st student is = "+marks1);

}

}

putroll(); System.out.println("Marks of 1st student is = "+marks1); } } © www.udaysatya.blogspot.com Page 45
putroll(); System.out.println("Marks of 1st student is = "+marks1); } } © www.udaysatya.blogspot.com Page 45

class Result2 extends Student { int marks2; void getmarks2(int z)

{

marks2=z;

}

void showmarks2()

{

System.out.print("Rollno of 2nd student is = "); putroll(); System.out.println("Marks of 2nd student is = "+marks2); } }

class Hierarchial

{

public static void main(String args[])

{

Result1 r1 = new Result1();

Result2 r2 = new Result2();;

r1.getroll(36);

r1.getmarks1(99);

r1.showmarks1();

r2.getroll(34);

r2.getmarks2(40);

r2.showmarks2();

}

}

OUTPUT :

Roll no of 1st student is = 36 Marks of 1st student is = 99 Rollno of 2nd student is = 34 Marks of 2nd student is = 40

of 1st student is = 99 Rollno of 2nd student is = 34 Marks of 2nd
of 1st student is = 99 Rollno of 2nd student is = 34 Marks of 2nd

38 Program to Demonstrate SUPER Class variable to reference a sub class object

class A {

int i,j; A(int a,int b) {

i=a;

j=b;

}

void show() {

System.out.println("i and j: " +i + " " +j);

}}

class B extends A { int k; B(int a,int b,int c) { super(a,b); k=c; } void show() {

System.out.println("k: "+k);

}}

class Supref { public static void main(String args[]) {

B

subOb=new B(1,2,3);

A

a;

a=subOb;

System.out.println("Using Sub Class Object :"); subOb.show();

System.out.println("Using Super class Variable : "); a.show();

}}

OUTPUT :

Using Sub Class Object :

k: 3 Using Super class Variable :

k: 3

}} OUTPUT : Using Sub Class Object : k: 3 Using Super class Variable : k:
}} OUTPUT : Using Sub Class Object : k: 3 Using Super class Variable : k:

39

Program to Demonstrate Method Overriding

class A {

int i,j; A(int a,int b)

{

i=a;

j=b;

}

void show() { System.out.println("i and j: " +i + " " +j); }}

class B extends A {

int k; B(int a,int b,int c)

{

super(a,b);

k=c;

}

void show()

{

System.out.println("k: "+k); }}

class Override {

public static void main(String args[])

{

B subOb=new B(1,2,3);

subOb.show();

}

}

OUTPUT :

k: 3

void main(String args[]) { B subOb=new B(1,2,3); subOb.show(); } } OUTPUT : k: 3 © www.udaysatya.blogspot.com
void main(String args[]) { B subOb=new B(1,2,3); subOb.show(); } } OUTPUT : k: 3 © www.udaysatya.blogspot.com

40

Program to Demonstrate Runtime Polymorphism

class figure

{

double dim1;

double dim2;

figure(double a,double b)

{

dim1=a;

dim2=b;

}

double area()

{

System.out.println("area for figure is undefined"); return 0;

}

}

class rectangle extends figure

{

rectangle(double a,double b)

{

super(a,b);

}

double area()

{

System.out.println("Inside Area for rectangle"); return dim1*dim2;

}

}

{ System.out.println("Inside Area for rectangle"); return dim1*dim2; } } © www.udaysatya.blogspot.com Page 49
{ System.out.println("Inside Area for rectangle"); return dim1*dim2; } } © www.udaysatya.blogspot.com Page 49

class triangle extends figure { triangle(double a,double b)

{

super(a,b);

}

double area()

{

System.out.println("Inside area for triangle"); return (dim1*dim2)/2; }}

class FindAreas { public static void main(String args[]) { figure f=new figure(10,10); rectangle r=new rectangle(9,5); triangle t=new triangle(10,8); figure figref; figref=r; System.out.println("Area is " +figref.area()); figref=t; System.out.println("Area is " +figref.area()); figref=f; System.out.println("Area is " +figref.area()); }}

OUTPUT :

Inside Area for rectangle Area is 45.0 Inside area for triangle Area is 40.0 area for figure is undefined Area is 0.0

Inside area for triangle Area is 40.0 area for figure is undefined Area is 0.0 ©
Inside area for triangle Area is 40.0 area for figure is undefined Area is 0.0 ©

41

Program to Demonstrate Abstract Class

abstract class A

{

abstract void callme(); void callmetoo(){ System.out.println("this is a concrete method."); }}

class B extends A

{

void callme()

{

System.out.println("B's implementation of callme."); }}

class AbstractDemo

{

public static void main(String args[]) {

B b=new B(); b.callme(); b.callmetoo();

}

}

OUTPUT :

B's implementation of callme. this is a concrete method.

} } OUTPUT : B's implementation of callme. this is a concrete method. © www.udaysatya.blogspot.com Page
} } OUTPUT : B's implementation of callme. this is a concrete method. © www.udaysatya.blogspot.com Page

42 Program to Demonstrate FINAL Keyword

class A

{

final int x =3; int y; A()

{

//x=4;this results in error

y=4;

}

}

class Finaldemo { public static void main(String args[]) {

A a = new A();

System.out.println("x = " +a.x); System.out.println("y = " +a.y);

//a.x=5;this also results in error

a.y=6;

System.out.println("x = " +a.x); System.out.println("y = " +a.y);

}

}

OUTPUT :

x

y

x

y

= 3

= 4

= 3

= 6

System.out.println("y = " +a.y); } } OUTPUT : x y x y = 3 = 4
System.out.println("y = " +a.y); } } OUTPUT : x y x y = 3 = 4

43

Program to Demonstrate FINAL Method

class A {

int i,j; A(int a,int b) { i=a; j=b;

}

void show() {

System.out.println("i and j: " +i + " " +j);

}

final void display() //Cannot be Overriden

{

System.out.println("i and j: " +i + " " +j); }} class B extends A { int k; B(int a,int b,int c) { super(a,b); k=c;

}

void show() { System.out.println("k: "+k); }}

class Finalmeth { public static void main(String args[])

{

B subOb=new B(1,2,3); System.out.println("Overriden Method call :"); subOb.show(); System.out.println("Final Method call :");

subOb.display();

}}

OUTPUT :

Overriden Method call :

k: 3 Final Method call :

i and j: 1 2

}} OUTPUT : Overriden Method call : k: 3 Final Method call : i and j:
}} OUTPUT : Overriden Method call : k: 3 Final Method call : i and j:

44 Program to Demonstrate FINAL Class

final class Democlass //Cannot be inherited

{

int a=34;

void showa()

{

System.out.println("a = " +a);

}

}

class A

{

public static void main(String args[])

{

Democlass obj = new Democlass(); obj.showa();

}

}

OUTPUT :

a=34

args[]) { Democlass obj = new Democlass(); obj.showa(); } } OUTPUT : a=34 © www.udaysatya.blogspot.com Page
args[]) { Democlass obj = new Democlass(); obj.showa(); } } OUTPUT : a=34 © www.udaysatya.blogspot.com Page

45

Program to Demonstrate INTERFACE

interface Sum // defining Interface

{

int z=2;//final variable declared

int add();//Abstract Method declared

}

class A implements Sum // Class implementing Interface

{

int x,y; public int add() {

return x+y+z;

}

}

class InterfaceDemo

{

public static void main(String args[])

{

A a = new A();

a.x=14;

a.y=18;

System.out.println("Sum : " +a.add());

}

}

OUTPUT :

Sum : 34

a.y=18; System.out.println("Sum : " +a.add()); } } OUTPUT : Sum : 34 © www.udaysatya.blogspot.com Page 55
a.y=18; System.out.println("Sum : " +a.add()); } } OUTPUT : Sum : 34 © www.udaysatya.blogspot.com Page 55

46

Program to access implementation through Interface references

interface figure

{

double area();

}

class rectangle implements figure

{

double dim1,dim2; rectangle(double a,double b)

{

dim1=a;dim2=b;

}

public double area()

{

return dim1*dim2;

}

}

class triangle implements figure

{

double dim1,dim2;

triangle(double a,double b)

{

dim1=a;dim2=b;

}

public double area()

{

return (dim1*dim2)/2;

}

}//implements through interface

} public double area() { return (dim1*dim2)/2; } }//implements through interface © www.udaysatya.blogspot.com Page 56
} public double area() { return (dim1*dim2)/2; } }//implements through interface © www.udaysatya.blogspot.com Page 56

class demo

{

public static void main(String args[])

{

rectangle r=new rectangle(100,50); triangle t=new triangle(10,20); figure f; f=r; System.out.println(f.area());

f=t;

System.out.println(f.area());

}

}

OUTPUT :

5000.0

100.0

f=t; System.out.println(f.area()); } } OUTPUT : 5000.0 100.0 © www.udaysatya.blogspot.com Page 57
f=t; System.out.println(f.area()); } } OUTPUT : 5000.0 100.0 © www.udaysatya.blogspot.com Page 57

47

Program to Demonstrate EXTENDING INTERFACE

interface A {

void meth1();

void meth2();

}

interface B extends A { void meth3();

}

class Myclass implements B { public void meth1() {

System.out.println("method1");

}

public void meth2() {

System.out.println("method2");

}

public void meth3() {

System.out.println("method3");

}}

class Extint { public static void main(String args[]) { Myclass m = new Myclass();

m.meth1();

m.meth2();

m.meth3();

}}

Output :

method1

method2

method3

Myclass(); m.meth1(); m.meth2(); m.meth3(); }} Output : method1 method2 method3 © www.udaysatya.blogspot.com Page 58
Myclass(); m.meth1(); m.meth2(); m.meth3(); }} Output : method1 method2 method3 © www.udaysatya.blogspot.com Page 58

48

Program to Demonstrate Stack using interface

interface stack

{

void push(int item); int pop();

}

class A implements stack

{

int s[];

int tos;

A(int size)

{

s = new int[size];

tos = -1;

}

public void push(int item)

{

if(tos == s.length-1) System.out.println("STACK is FULL");

else

s[++tos]=item;

}

public int pop()

{

if(tos<0)

{

System.out.println("STACK IS EMPTY"); return 0;

}

else

return s[tos--];

}}

System.out.println("STACK IS EMPTY"); return 0; } else return s[tos--]; }} © www.udaysatya.blogspot.com Page 59
System.out.println("STACK IS EMPTY"); return 0; } else return s[tos--]; }} © www.udaysatya.blogspot.com Page 59

class S { public static void main(String args[]){ int i; A stack1 = new A(5);

for(i=0;i<5;i++)

stack1.push(i);

System.out.println("Stack elements are : "); for(i =0;i<5;i++)

System.out.println(stack1.pop());

}

}

OUTPUT :

Stack elements are :

4

3

2

1

0

System.out.println(stack1.pop()); } } OUTPUT : Stack elements are : 4 3 2 1 0 © www.udaysatya.blogspot.com
System.out.println(stack1.pop()); } } OUTPUT : Stack elements are : 4 3 2 1 0 © www.udaysatya.blogspot.com

49

Program to Demonstrate Queue using Interface

interface queue

{

void push(int item); int pop();

}

class B implements queue

{

int s[];

int f,l;

B(int size)

{

s

= new int[size];

f

= -1;

l

= -1;

}

public void push(int item)

{

if(f == s.length-1)

System.out.println("Q is FULL");

else

s[++f]=item;

}

public int pop()

{

if(f==l)

{