1216BECE30042.docx

55
Object Oriented Programing With Java 1216BECE30042 Practical No: 17 Aim: Write a program to implement the concept of interfaces Source: interface myinterface1 { void method1(int var1); } interface myinterface2 { void method2(int var2); } class myclass implements myinterface1,myinterface2 { public void method1(int x) { System.out.println("method called by" +x); } public void method2(int y) { System.out.println("method called by"+y); } } LDRP ITR, CE-IT

Transcript of 1216BECE30042.docx

Object Oriented Programing With Java 1216BECE30042Practical No: 17Aim: Write a program to implement the concept of interfaces

Source:

interface myinterface1{ void method1(int var1);} interface myinterface2{ void method2(int var2);} class myclass implements myinterface1,myinterface2{ public void method1(int x) { System.out.println("method called by" +x); } public void method2(int y) { System.out.println("method called by"+y); }} class interfacedemo { public static void main(String args[]) { myinterface1 obj1 = new myclass(); myinterface2 obj2 = new myclass(); obj1.method1(1); obj2.method2(2); } }

OUTPUTmethod called by1method called by2

Practical No: 18Aim: Write a program to implement final keyword

Source: interface myinterface{ void method(int var); static final int a = 50; } class myclass implements myinterface{ public void method(int x) { System.out.println("method is called"); }} class finaldemo{ public static void main(String args[]) { myinterface obj1 = new myclass(); obj1.a = obj1.a+10; //final can't be assigned value again obj1.method(0); } }

OUTPUTFinaldemo.java:21 : error: cannot assign a value to final variable a obj1.a = obj1.a+10; ^1 error

Practical No: 19Aim: Write a program in which a class extends another class & implements an interface also.

Source:interface myinterface{ void call (int x);} class parentclass implements myinterface { public void call(int a) { System.out.println("INTERFACE IS CALLED"); } parentclass() { System.out.println("Inheritance"); } } class child extends parentclass { child() { super();}}

class dualdemo{ public static void main(String args[]) { child obj1 = new child(); myinterface obj2 = new child(); obj2.call(0); } }

OUTPUTInheritanceInheritanceINTERFACE IS CALLED

Practical No: 20Aim: Write a program showing the implementation of how interfaces can be extended.

Source:interface myinterface1{ void method1();}

interface myinterface2 extends myinterface1{ void method2();}

class myclass implements myinterface2{ public void method1() { System.out.println("method 1 is called"); } public void method2() { System.out.println("method 2 is called"); } } class extinter { public static void main(String args[]) { myinterface2 obj1 = new myclass(); obj1.method1(); obj1.method2(); } }

OUTPUTmethod 1 is calledmethod 2 is called

Practical No: 21Aim: Create a package & import the methods of that package in your class.

Source:Code 1:package classbulk;

public class bal{ String name; int bal; public bal(String a, int x) { name=a; bal = x; } public void display() { System.out.println(name + ": $" +bal); } }

Code 2:import classbulk.*;

class testpack{ public static void main(String args[]) { bal obj1 = new bal("ldrp-itr",50000); obj1.display(); } }

OUTPUTldrp-itr: $50000

Practical No: 22Aim: Write a program to implement the concept of Try-Catch block in Exception Handling

Source:class exdemo{ public static void main(String args[]) { try { for(int i=2;i>=0;i--) { System.out.println(10/i);}}catch(ArithmeticException ex){ System.out.println("exception caught"); System.out.println(ex); } } }

OUTPUT510Exception caughtJava.lang.ArithmeticException: / by zero

Practical No: 23Aim: Write a program to implement Try with multiple catches.

Source:class exdemo1{ public static void main(String args[]) { int a[] = new int[9]; try { for(int i=10;i>=0;i--) { System.out.println(10/i);a[i]=i;}}catch(ArithmeticException ex){ System.out.println("exceptioncaughtArithmeticException"); System.out.println(ex); } catch(IndexOutOfBoundsException e){ System.out.println("exception caught IndexOutOfBoundsException"); System.out.println(e); } } }

OUTPUT1exception caught IndexOutOfBoundsExceptionJava.lang.ArrayIndexOutOfBoundsException: 10

Practical No: 28Aim: Write a program to create multiple threads in Java.

Source:Calss A extends Thread { public void run(){ for(int i=1; i