INDEX
NAME OF THE PROGRAMS
Program to show array Program to show sum and average of array . Program to show two dimension array . Program to show type casting . Program to create an object using classes . Program using static keyword . Program to show inheritance . Program to show method overloading . Program to passing objects or parameters and constructor overloading . Program to show method overriding . Program of creating and importing a package . Program to implement interface .
Program of exception handling by try and catch block . Program to show multiple catch block . Program to show nested try . Program to throw an exception by throw statement . Program to create your own exception .
1.Program to show Array
class abc { public static void main(string arg[]) { int a[]={15,23,30,45}; for(int i=0;i<4;i++) { System.out.println(a[i]); } } }
Output : 15
23 30 45
2.Program to Show Sum and Average of Array
class abc { public static void main(string args[]) { int a[]=(4,8,10,12,55,64,15,7,3,9}; int sum=0; float avg; for(int i=0;i<10;i++) { Sum=sum+a[i];
} avg=sum/10; system.out.println(sum=+sum+ And Average=+avg); } }
Output: Sum=187 And Average=18.0
3.Program to Show Two Dimension Array
Class result { public static void main (string args[]) { int twoD[][]= new int[4][5]; int k=0; for(int i=0;i<4;i++) for(int j=0;j<4;j++) {
twoD[i][j]=k; k++; } System.out.println(The TWO D Array is); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { System.out.print(twoD*i+*j++ ); } System .out.print(); } } }
Output:
The TWO D Array is 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
4.Program to Show Type Casting
class prm { public static void main(string args[]) { byte b=15; short s=1024; int i=5000; float f=5.7; double d=0.123; double result=(b*s)+(i*s)-(f*d); system.out.println(Result is=+result); } }
Output: Result is=4793360.0
5.Program to create an object using classes
class box { double length; double height; double width; } class boxdemo { public static void main(string args[]) { box f1,f2; f1=new box(); f2=new box();
f1.length=7.5; f1.height=8.0; f1.width=6.0; double vol; vol=f1.length*f1.height*f1.width; system.out.println(value of F1=+vol); f2.length=9.6; f2.height=6.7; f2.width=8.5; vol=f2.length*f2.height*f2.width; system.out.println(value of F2=+vol); } } Output: Value of F1=360.0 Value of F2=546.71999999
6.Program using static keyword
class stat { static int a=4, b; static void meth(int x) { System.out.println(x=+x); System.out.println(a=+a); System.out.println(b=+b); } Static { System.out.println(static block initialized); b=a*2;
} public static void main(string args[]) { System.out.println(Main Method); Meth(62); } }
Output: Static block initilazed Main Method x=62 a=4 b=8
7. Program to show inheritance
class A { int I,j; void show_ij() { System.out.println(i=+i); System.out.println(j=+j); } } class B extends A { int k; void show_k()
{ System.out.println(k=+k); } void sum() { System.out.println(sum of I,j and k=+(i+j+k)); } } class inherit { public static void main(string args[]) { A a=new A(); a.i=10; a.j=20; system.out.println(content of object a are=); a.show_ij(); B.b= new B(); b.i=30; b.j=40; b.k=50; system.out.println(content of object b are=); b.show_ij(); b.show_k(); b.sum(); } }
Output: Content of a object a are= i=10 j=20 content of object b are = i=30 j=40
k=50 sum of I,j and k=120
8.Program to Show Method Overloading
class oload { void test() { System.out.println(No Arguments); } void test(int a) { System.out.println(Value of A= +a); } void test(int a,int b) { System.out.println(Value of A=+a+ and B=+b);
} void test(double d) { System.out.println(Value of double D=+d); } } class old { public static void main(string args[]) { oload ol=new oload(); ol.test(); ol.test(70); ol.test(16,39); ol.test(19.753); } } Output:
No Arguments Value of A=70 Value of A=16 and B=39 Value of double D=19.753
9.Program of Passing objects or Parameters and Constructor Overloading
class box4 { double length,height,width; box4(double l,double h,double w) { length=l; height=h; width=w; } box4(double side) { length=height=width=side;
} box4(box4 ob) { length=ob.length; height=ob.height; width=ob.width; } double vol() { double vol=length*height*width; return vol; } } class bd { public static void main(string args[]) { box4 b1=new box4(8.75,6.73,9.1); box4 b2=new box4(8.53); box4 b3=new box4(b1); system.out.println(Value of B1=+b1.vol()); system.out.println(value of B2="()) system.out.println(value of b3=+b3.vol()); } } Output: Value of B1=535.87625 Value of B2=620.6504769999999 Value of B3=535.87625
10.Program to Show Method Overriding
class alpha1 { int a,b; alpha1(int I, int j); { a=i; b=j; } void show() { System.out.println(i=+a+ and j+b); } } class beta1 extends alpha1
{ int c; beta1(int I,int j,int k) { super(I,j); c=k; } void show() { super.show(); system.out.println(k=+c); } } class overidn { public static void main(string args[]) { beta1 b=new beta1(5,10,15); b.show(); } } Output: I=5 and j=10 K=15
11.Program of Creating and Importing a Package Create Package
package fstpkg; public class a { public void display() { System.out.println(Class A of First Package); } } Importing Package import fstpkg.a; class b {
public static void main(string args[]) { a ab=new a(); ab.display(); } } Output: Class A of First Package
12.Program to Implement Interface
interface area { double pi=3.14; double compute(double x, double y); } class circle implements area { public double compute(double x, double y) { return(pi*x*y); } } class rectangle implements area {
public double compute(double x, double y) { return(x*y); } } class itf { public static void main(string args[]) { circle c=new circle(); rectangle r= new rectangle(); system.out.println(Circle=+c.compute(5,5)); system.out.println(Rectangle=+r.compute(6,8)); } } Output: Circle=78.5 Rectangle=48.0
13.Program of Exception Handling by Try and Catch Block
class demo { public static void main(string args[]) { int a=10,b=5,c=5; int x,y; try { x=a/(b-c); system.out.println(X=+x); } catch(ArithmeticException e) { System.out.println(Error! Division by Zero);
} y=a/(b+c); system.out.println(Y=+y); } }
Output: Error! Division by zero Y=1
14.Program to Show Multiple Catch Block
class error { public static void main(string args[]) { int a[]={5,10}; int x=5,y,z; try { y=a[2]/(a[0]-x); system.out.println(Y=+y); } catch(ArithmeticException e) } System.out.println(Divide by Zero);
} catch(ArrayIndexOutOfBoundsException e) { System.out.println(Array index out of Bound); } catch(ArrayStoreException e) { System.out.println(Wrong Data type); } z=a[1]/a[0]; system.out.println(Z=+z); } } Output: Array index out of Bound Z=2
15. Program to Show Nested Try
class ntry { public static void main(string args[]) { int a=args.length; int b=5,c,d; try { System.out.println(This is Outer Try); try { System,out.println(Start Of Inner Try); if(a==1) {
c=b/(b-5); system.out.println(C=+c); } if(a==2) { int x[]={1,2}; system.out.println(X*2+=+x*2+); } System.out.println(End of Inner Try); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(Array Index Out Of Bound); } d=a+b; system.out.println(D=+d); system.out.println(End of Outer Try ); } catch(ArithmeticException e) { System .out.println(Division By Zero ); } System.out.println(End ); } } Output : This is Outer Try Start of Inner Try End of Inner try D=5 End of Outer Try End
16.Program to Throw an Exception by Throw Statement
class thdemo { public static void main(string args[]) { try { int a=5; system.out.println(A=+a); throw new ArithmeticException(); } catch(ArithmeticException e) {E System.out.println(Arithmetic Exception Caught); }
System.out.println(End); } } Output: A=15 Arithmetic Exception Caught END
17.Program to Create your Own Exception
class mexp extends Exception { mexp(String message) { super(message); } } class txp { public static void main(String args[]) { double a=5, b=10000; double z; try
{ z=a/b; if(z<.001) { throw new mexp(Number is too small); } else { System.out.println(Z=+z); } } catch(mexp e) { System.out.println(My exception caught); System.out.println(End ); } } } Output:
My exception caught END
PRACTICAL FILE OF JAVA PROGRAMMING
SUBMITTED TO: MR. RAJDEEP SINGH
SUBMITTED BY: DEEPAK BRANCH B.SC IT SEMESTER- 4TH