Sunteți pe pagina 1din 11

(32) class Box { double width; double height; double depth; Box (Box ob) { width=ob.width; height=ob.height; depth=ob.

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; } } class Boxweight extends Box { double weight; Boxweight(double w, double h, double d, double m) { width=w; height=h; depth=d; weight=m; } }

(34) class DemoBoxweight { public static void main(String args[]) { Boxweight mybox1=new Boxweight(10, 20, 15, 34.3); Boxweight mybox2=new Boxweight(2, 3, 4, 0.076); double vol; vol=mybox1.volume(); System.out.println("Vol of mybox1 is "+vol); System.out.println("weight of mybox1 is"+ mybox1.weight); System.out.println(); vol=mybox2.volume(); System.out.println("Vol of mybox2 is "+vol); System.out.println("weight of mybox2 is"+ mybox1.weight); } }

(35) class super1 { int x; super1(int x) { this.x=x; } void display() { System.out.println("super x= "+x); }

class sub extends super1 { int y; sub(int x, int y) { super(x); this.y=y; } void display() { System.out.println("Super X="+x); System.out.println(" sub Y="+y); } }

(36) class overridetest { public static void main(String args[]) { sub s1=new sub(100,200); s1.display(); }} 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"); } }

(37) class Abstractdemo { public static void main(String args[]) { B b=new B(); b.callme(); b.callmetoo(); }}

(38) interface x1 { int x=10; int y=20; public void m1(); public void m2(); } class A1 implements x1 {

public void m1() { System.out.println("hello from method1"); } public void m2() { System.out.println("hello from method2"); } public static void main(String args[]) { A1 ob=new A1(); ob.m1(); ob.m2(); System.out.println("the value of x: "+x); } }

(39) interface m { public void m1(); public void m2(); } interface g { public void m3(); public void m4(); } class C implements m,g { public void m1() { System.out.println("hello from method 1"); } public void m2() { System.out.println("hello from method2"); } public void m3() { System.out.println("hello from method3"); }

public void m4() { System.out.println("hello from method4"); } public static void main(String args[]) { C ob=new C(); ob.m1(); ob.m2(); ob.m3(); ob.m4(); }} package MyPack; public class Balance { String name; double bal; public Balance(String n, double b) { name=n; bal=b; } public void show () { if (bal<0) System.out.print("--> "); System.out.println(name+ ":$"+bal); } }

(40) import MyPack.*; class TestBalance { public static void main(String args[]) { Balance test=new Balance("Venkatesh S.G.", 99.88); test.show(); } }

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

char c[]={'A','T','G','U'}; String s1=new String(c); String s2=new String(s1); System.out.println (s1); System.out.println (s2); } }

(42) class substringcons { public static void main (String args[]) { byte ascii[]={65,66,67,68,69,70}; String s1=new String (ascii); System.out.println(s1); String s2=new String (ascii,2,3); System.out.println(s2); } }

(43) class stringconcat { static int age=7; public static void main (String args[]) { String longstr="This could have been "+age+" "+"a very long line that would have"+"wrapped around. But string concatenation"+"prevents this"; System.out.println (longstr); } }

(44) class equaldemo { public static void main (String args[]) { String s1="hi"; String s2="hi"; String s3="bye"; String s4="HI"; System.out.println (s1+"equals"+s2+"--->"+s1.equals (s2)); System.out.println (s1+"equals"+s3+"--->"+s1.equals (s3));

System.out.println (s1+"equals"+s4+"--->"+s1.equals (s4)); System.out.println (s1+"equals Ignorecase"+s4+"--->"+s1.equalsIgnoreCase (s4)); } }

(45) class appenddemo { public static void main (String args[]) { String s; int a=42; StringBuffer sb=new StringBuffer(40); s=sb.append("a=").append(a).append("!").toString(); System.out.println(s); } }

(46) class insertdemo { public static void main (String args[]) { StringBuffer sb=new StringBuffer ("i java"); sb.insert(2,"like"); System.out.println (sb); } }

(47) class reversedemo { public static void main (String args[]) { StringBuffer s=new StringBuffer ("madam i am adam"); s.reverse(); System.out.println (s); } }

(48) class exc1 { public static void main(String args[]) { int d, a; try { d=0; a=42/d; System.out.println("This will not be printed"); }catch(ArithmeticException e) { System.out.println(" Division by Zero"); } System.out.println("After catch statement."); } }

(49) class exc2 { public static void main (String args[]) { int d,a; try { d=0; a=42/d; System.out.println ("This will not be printed"); } catch (ArithmeticException e) { System.out.println ("Division by Zero"); } System.out.println ("After catch statement"); } }

(50) class exc3 { public static void main (String args[]) { try { int a=args.length;

System.out.println ("a="a); int b=42/a; int e[]={1}; e[42]=99; } catch (ArithmeticException e) { System.out.println ("Division by Zero"); } catch (Array Index Out Of Bounds Exception e) { System.out.println ("Array Index obb="+e); } System.out.println ("After catch statement"); } }

(51) class multicatch { public static void main(String args[]) { try { int a=args.length; System.out.println("a= "+a); int b=42/a; int c[]={1}; c[42]=99; } catch(ArithmeticException e) { System.out.println(" Divide by 0 :"+e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index oob:" +e); } System.out.println(" After try/catch blocks"); }} class MyException extends Exception { MyException(String message) { super(message); } }

(52) class TestMyException { public static void main(String args[]) { int a=10; try { int i=Integer.parseInt(args[0]); if (i==a) { System.out.println("You have entered 10"); }else { throw new MyException("Number entered is not equal to 10"); } } catch(MyException e) { System.out.println("Caught my exception"); System.out.println(e.getMessage()); } finally { System.out.println("I am in Finally"); } }

(53) class Throwdemo { static void demoproc() { try { throw new NullPointerException(" Demo"); } catch (NullPointerException e) { System.out.println("Caught inside demoproc"); throw e; }

} public static void main(String args[]) { try

{ }

demoproc();

} }

catch (NullPointerException e) { System.out.println("Recaught:="+e); } System.out.println("After catch Statement");

(54) class throwsdemo { static void throwone() throws IllegalAccessException { System.out.println("Inside throwone"); throw new IllegalAccessException ("demo"); } public static void main (String args[]) { try { throwone(); } catch(IllegalAccessException e) { System.out.println(" Caught "+e); } finally { System.out.println(" Within Finally"); } } }

S-ar putea să vă placă și