







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!








class Bear extends Animal, Zoo { ... } class Bear extends Animal implements Zoo { ... }
class Animal extends Bear { ... } class Bear extends Animal { ... } class Zoo { private Animal[] zooAnimals; } class Zoo { private Animal[] zooAnimals; }
class Animal extends Zoo { class Zoo extends Animal { private Bear zooBear; private Bear[] zooAnimals; } }
public static int findSmallest( int[] numbers ) { int smallest = _______________;
for ( int i = _____________; ______________________________________________________; __________ ) { if ( __________________________________________________________________ ) { _______________________________________________________ ; } } return _______________________; }
public class Mystery { public static void main( String[] args ) { Mystery ref = new Mystery();
System.out.println( ref.mystery( 6 ) ); }
public int mystery( int a ) { int b = a - 3; int c = a + 5;
if ( b > 0 ) { System.out.println( a + " " + b + " " + c ); c = b + mystery( --a ); System.out.println( a + " " + b + " " + c ); } else { c = a - b; System.out.println( "Whoa!" ); System.out.println( a + " " + b + " " + c ); }
return c; } }
class Thing { private int count;
public Thing2( int count ) { this.count = count; }
public int getCount() { return this.count; }
public void setCount( int count ) { this.count = count; }
public String toString() { String s = " ";
switch( this.count ) { case 1: s = s + "first ";
case 2: s = s + "mid "; break;
case 3: s = s + "last "; break;
default: s = s + "rest "; break; }
return s; }
public void swap1( Thing2 t2 ) { int temp;
temp = this.getCount(); this.setCount( t2.getCount() ); t2.setCount( temp ); }
public void swap2( Thing2 t2 ) { Thing2 temp; Thing2 t1 = this;
temp = t1; t1 = t2; t2 = temp; } }
public class Test { public static void main( String[] args ) { Thing2 first = new Thing2( 1 ); Thing2 second = new Thing2( 2 );
Thing2 temp = second; second = first; first = temp;
System.out.println( first.toString() ); System.out.println( second.toString() );
Thing2 third = new Thing2( 3 ); Thing2 fourth = new Thing2( 4 );
third.swap1( fourth );
System.out.println( third.toString() ); System.out.println( fourth.toString() );
second.setCount( fourth.getCount() ); third = first;
System.out.println( third == first ); System.out.println( fourth == second ); System.out.println( first.toString().equals( third.toString() ) ); System.out.println( second.toString().equals( fourth.toString() ) );
System.out.println( first.toString() ); System.out.println( second.toString() ); System.out.println( third.toString() ); System.out.println( fourth.toString() );
first = new Thing2( 1 ); second = new Thing2( 2 );
first.swap2( second );
System.out.println( first.toString() ); System.out.println( second.toString() ); } }
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( "Ko Ko" ); } 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( "Great" ); } }
class BigTiger extends Tiger { public BigTiger() { super( "Brina" ); } 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 "Chung Lion " + super.speak(); } public String softer() { return "Katherine " + super.speak(); } }
public class Test14 { public static void main( String[] args ) {
Animal a;
a = new Lion(); System.out.println( a + " says " + a.speak() );
a = new BigTiger(); System.out.println( a + " says " + a.speak() );
a = new Cat(); System.out.println( a + " says " + a.speak() );
a = new Tiger( "Zach" ); System.out.println( a + " says " + a.speak() );
a = new BigTiger( "Katherine" ); System.out.println( a + " says " + ((Cat) a).speak( "Big" ) );
1 public class Test 2 { 3 private int a; 4 private static int b = 3; 5 private int c;
6 public static void main( String[] args ) 7 { 8 Test16 ref = new Test16( 4 );
9 ref.method1( ref.a ); 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 + 2; 20 a = c + 3;
21 System.out.println( "this.a = " + this.a ); 22 System.out.println( "Test16.b = " + Test16.b ); 23 System.out.println( "this.c = " + this.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( b + c ) ); 28 System.out.println( "this.a = " + this.a ); 29 System.out.println( "Test16.b = " + Test16.b ); 30 System.out.println( "this.c = " + this.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.c + Test16.b;
40 x = a = b + c;
41 System.out.println( "this.a = " + this.a ); 42 System.out.println( "Test16.b = " + Test16.b ); 43 System.out.println( "this.c = " + this.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.b = b + 2; 49 this.c = a + c;
50 return x + 5; 51 } 52 }
public class Fubar { public Fubar( int x, int y ) { System.out.println( "Fubar ctor #1" ); }
public Fubar() { this( 10, 20 ); System.out.println( "Fubar ctor #2" ); }
public String toString() { System.out.println( "Fubar.toString" ); return "Fubar.toString"; } }
public class Foo extends Fubar { public Foo( int x, int y, int z ) { this( x, y ); System.out.println( "Foo ctor #1" ); }
public Foo( int x, int y ) { super(); System.out.println( "Foo ctor #2" ); }
public Foo() { this( 4, 2, 0 ); System.out.println( "Foo ctor #3" ); }
public String toString() { System.out.println( "Foo.toString" + " + "
public class FubarTest { public static void main( String[] args ) { Fubar ref = new Foo();
System.out.println( "-----" );
System.out.println( ref.toString() ); } }