Skip to content

Instantly share code, notes, and snippets.

@YoniTsafir
Created June 18, 2012 20:57
Show Gist options
  • Save YoniTsafir/2950656 to your computer and use it in GitHub Desktop.
Save YoniTsafir/2950656 to your computer and use it in GitHub Desktop.
Does it compile? questions in java II
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());
}
}
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());
}
}
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());
}
}
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)); }
}
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();
}
}
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();
}
}
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());
}
}
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]);
}
}
}
- casting
- collections
- exceptions
- misc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment