







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








quarter_.25 Half-Time 25Cents double
quarter25 SuperBowl_XLVII QUARTER_25 string_cheese
public class Tricky { public static void main( String[] args ) { foo1(); System.out.println( "main1" ); foo2(); System.out.println( "main2" ); foo3(); System.out.println( "main3" ); }
public static void foo1() { System.out.println( "A" ); }
public static void foo2() { System.out.println( "B" ); foo3(); System.out.println( "C" ); }
public static void foo3() { System.out.println( "D" ); foo1(); System.out.println( "E" ); } }
public class Mystery { public static void main( String[] args ) { Mystery ref = new Mystery();
System.out.println( ref.mystery( 7 ) ); }
public int mystery( int a ) { int b = a - 3; int c = a + 3;
if ( b > 1 ) { System.out.println( a + " " + b + " " + c ); c = a + mystery( b + 2 ); System.out.println( a + " " + b + " " + c ); } else { c = a + b; System.out.println( "Stop!" ); System.out.println( a + " " + b + " " + c ); }
return c; } }
class Snow { public void method2() { System.out.println("Snow 2"); }
public void method3() { System.out.println("Snow 3"); } }
class Rain extends Snow { public void method1() { System.out.println("Rain 1"); method3(); }
public void method2() { super.method2(); System.out.println("Rain 2"); method3(); } }
class Sleet extends Snow { public void method2() { super.method2(); System.out.println("Sleet 2"); method3(); }
public void method3() { super.method3(); System.out.println("Sleet 3"); } }
class Fog extends Sleet { public void method1() { System.out.println("Fog 1"); }
public void method3() { super.method3(); System.out.println("Fog 3"); } }
method
method
method
method
(method3)
method
(method2)
method
method
method
Snow ref1;
ref1 = new Fog();
((Fog) ref1).method1(); System.out.println( "-----" ); ref1.method2(); System.out.println( "-----" ); ref1.method3();
Snow ref1;
ref1 = new Rain();
((Rain) ref1).method1(); System.out.println( "-----" ); ref1.method2(); System.out.println( "-----" ); ref1.method3();
Thing1 thing1; Thing2 thing2; Doable doable;
public interface Doable { public abstract void doit(); }
public class Thing1 implements Doable { private static final String SPEAK = "Me";
public Thing1() { // ctor initialization here }
public String speak() { return SPEAK; }
public void doit() { // Thing1 does its thing } }
public class Thing2 implements Doable { public static final String SPEAK = "No, Me";
public Thing2() { // ctor initialization here }
public String speak( String s ) { return SPEAK + s; }
public void doit() { // Thing2 does its thing } }
thing1 = new Thing1(); _______
thing1.speak(); _______
thing1.doit(); _______
thing1.speak( " Mine" ); _______
String s1 = Thing1.SPEAK; _______
thing2 = new Thing2(); _______
thing2.speak(); _______
thing2.doit(); _______
thing2.speak( " Mine" ); _______
String s2 = Thing2.SPEAK; _______
doable = new Thing1(); _______
doable.speak(); _______
doable.doit(); _______
doable = thing2; _______
doable.speak( " Mine" ); _______
doable.doit(); _______
thing1 = thing2; _______
thing1 = doable; _______
doable = new Doable(); _______
abstract class Animal { private String name; public Animal() { this( "Animal" ); } public Animal( String name ) { this.name = name; } public String toString() { return this.name; } public abstract String speak(); }
class Cat extends Animal { public Cat() { this( "Brina" ); } public Cat( String name ) { super( name + " Cat" ); } public String speak() { return "Meow"; } public String speak( String name ) { return name + " Meow"; } }
class Tiger extends Cat { public Tiger( String name ) { super( name + " Tiger" ); } public String speak() { return super.speak( "Jennifer" ); } }
class BigTiger extends Tiger { public BigTiger() { super( "Ko Ko" ); } public BigTiger( String name ) { super( name ); } public String speak() { return "Roar"; } public String speak( String name ) { return name + " Roar"; } }
final class Lion extends Cat { public String speak() { return "Mo Lion " + super.speak(); } public String softer() { return "Marjori " + super.speak(); } }
public class Test14 { public static void main( String[] args ) {
Animal a;
a = new Cat(); System.out.println( a + " says " + a.speak() );
a = new Lion(); System.out.println( a + " says " + ((Lion) a).softer() );
a = new BigTiger(); System.out.println( a + " says " + a.speak() );
a = new Tiger( "Max" ); System.out.println( a + " says " + a.speak() );
a = new BigTiger( "Zach" ); System.out.println( a + " says " + ((Cat) a).speak( "Big" ) );
} }
1 public class Test 2 { 3 private int a; 4 private int b; 5 private static int c = 7;
6 public static void main( String[] args ) 7 { 8 Test16 ref = new Test16( 3 );
9 ref.method1( ref.b ); 10 }
11 public Test16( int a ) 12 { 13 this.a = a; 14 }
15 public void method1( int x ) 16 { 17 int c = x; 18 int b;
19 b = a; 20 a = c;
21 System.out.println( "this.a = " + this.a ); 22 System.out.println( "this.b = " + this.b ); 23 System.out.println( "Test16.c = " + Test16.c ); 24 System.out.println( "c = " + c ); 25 System.out.println( "b = " + b ); 26 System.out.println( "a = " + a ); 27 System.out.println( "result = " + method2( a ) ); 28 System.out.println( "this.a = " + this.a ); 29 System.out.println( "this.b = " + this.b ); 30 System.out.println( "Test16.c = " + Test16.c ); 31 System.out.println( "x = " + x ); 32 System.out.println( "a = " + a ); 33 System.out.println( "b = " + b ); 34 System.out.println( "c = " + c ); 35 }
36 private int method2( int x ) 37 { 38 int b = x; 39 int c = this.b + Test16.c;
40 x = a = b + c;
41 System.out.println( "this.a = " + this.a ); 42 System.out.println( "this.b = " + this.b ); 43 System.out.println( "Test16.c = " + Test16.c ); 44 System.out.println( "x = " + x ); 45 System.out.println( "a = " + a ); 46 System.out.println( "b = " + b ); 47 System.out.println( "c = " + c );
48 Test16.c = c + 2; 49 this.a = a + c;
50 return x + 5; 51 } 52 }
public class Foo { public Foo() { System.out.println( "Foo ctor #1" ); }
public Foo( int x, int y ) { this(); System.out.println( "Foo ctor #2" ); }
public String toString() { System.out.println( "Foo.toString" ); return "Foo"; } }
public class Fubar extends Foo { public Fubar( int x, int y, int z ) { super( x, y ); System.out.println( "Fubar ctor #1" ); }
public Fubar( int x, int y ) { this ( x, y, 42 ); System.out.println( "Fubar ctor #2" ); }
public Fubar() { this( 4, 2 ); System.out.println( "Fubar ctor #3" ); }
public String toString() { String s = super.toString() + " + " + "Fubar"; System.out.println( s ); return "Fubar.toString"; } }
public class FubarTest { public static void main( String[] args ) { Foo ref = new Fubar();
System.out.println( "*****" );
System.out.println( ref.toString() ); } }