Created
June 18, 2012 20:57
-
-
Save YoniTsafir/2950656 to your computer and use it in GitHub Desktop.
Does it compile? questions in java II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
private int x; | |
public A() { | |
this(7); | |
} | |
public A(int x) { | |
setX(x); | |
} | |
public int getX() { | |
return x; | |
} | |
public void setX(int x) { | |
this.x = x; | |
} | |
public void setXFrom(A other) { | |
setX(other.x); | |
} | |
} | |
class B { | |
private A a1; | |
private A a2; | |
public B(A a1, A a2) { | |
this.a1 = a1; | |
this.a2 = new A(a2.getX()); | |
} | |
public int getFirst() { | |
return a1.getX(); | |
} | |
public int getSecond() { | |
return a2.getX(); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a1 = new A(); | |
A a2 = new A(5); | |
B b = new B(a1, a2); | |
System.out.println(b.getFirst()); | |
System.out.println(b.getSecond()); | |
System.out.println("----"); | |
a1.setXFrom(a2); | |
a2.setX(17); | |
System.out.println(b.getFirst()); | |
System.out.println(b.getSecond()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
private int x; | |
public A(int x) { | |
this.x = x; | |
} | |
public static int square() { | |
return x * x; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a = new A(3); | |
System.out.println(a.square()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Util { | |
public static double pi() { | |
return 3.14; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Util u = new Util(); | |
System.out.println(u.pi()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
private static int x = 3; | |
private int y = 2; | |
public A() { | |
x++; | |
y++; | |
} | |
public static int multiple(A a) { | |
return x * a.y; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a1 = new A(); | |
A a2 = new A(); | |
System.out.println(A.multiple(a2)); } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Printable { | |
void print(); | |
} | |
interface Cool { | |
} | |
class BasePrintable implements Printable { | |
public void print() { | |
System.out.println(toString()); | |
} | |
public String toString() { | |
if (this instanceof Cool) { | |
return getClass().getName() + ": I'm cool"; | |
} else { | |
return getClass().getName() + ": lame..."; | |
} | |
} | |
} | |
class A extends BasePrintable implements Cool { | |
public String toString() { | |
return super.toString() + " and I know it!"; | |
} | |
} | |
class B extends BasePrintable { | |
} | |
public class Main { | |
public static void main(String[] args) { | |
A a = new A(); | |
B b = new B(); | |
a.print(); | |
b.print(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface I { | |
void bla(); | |
} | |
abstract class A implements I { | |
public abstract void blat(); | |
} | |
class B extends A { | |
public void blat() { | |
System.out.println("Hello"); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
B b = new B(); | |
b.blat(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class A { | |
public int calcStuff() { | |
return 7 * smart() * stupid(); | |
} | |
protected abstract int smart(); | |
private int stupid() { | |
return 10 * 20 - 30; | |
} | |
} | |
class B extends A { | |
protected int smart() { | |
return super.stupid() + 8; | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
B b = new B(); | |
System.out.println(b.calcStuff()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Message { | |
public abstract String getMessageType(); | |
} | |
class SleepMessage extends Message { | |
public String getMessageType() { | |
return "Sleep"; | |
} | |
public int getTime() { | |
return 22; | |
} | |
} | |
class EatMessage extends Message { | |
public String getMessageType() { | |
return "Eat"; | |
} | |
public String whichMeal() { | |
return "Breakfast"; | |
} | |
} | |
class MessageHandler { | |
public void handleMessage(Message m) { | |
if (m instanceof SleepMessage) { | |
System.out.println("Sleeping until " + ((SleepMessage)m).getTime()); | |
} else if (m instanceof EatMessage) { | |
System.out.println("Eating " + ((EatMessage)m).whichMeal()); | |
} else { | |
System.out.println("Illegal message type: " + m.getMessageType()); | |
} | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Message[] m = new Message[2]; | |
m[0] = new SleepMessage(); | |
m[1] = new EatMessage(); | |
MessageHandler mh = new MessageHandler(); | |
for (int i = 0; i < 2; i++) { | |
mh.handleMessage(m[i]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- casting | |
- collections | |
- exceptions | |
- misc. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment