1. Consider the following class definition:
public class A {

  private Object x;
  private Object y;

  public A(Object x, Object y) {
    this.x = x;
    this.y = y;
  }

  public static void go() {
    A a1 = new A(new Integer(1), new Integer(2));
    A a2 = new A(new Integer(3), new Integer(4));
    A a3 = new A(new Integer(1), new Integer(2));
    A a4 = a1;

    System.out.println("a1 == a2 is: " + (a1 == a2));
    System.out.println("a1 == a3 is: " + (a1 == a3));
    System.out.println("a1 == a4 is: " + (a1 == a4));
    System.out.println();

    System.out.println("a1 equals a2 is: " + a1.equals(a2));
    System.out.println("a1 equals a3 is: " + a1.equals(a3));
    System.out.println("a1 equals a4 is: " + a1.equals(a4));
  }

}

  1. What is printed to the screen when the go method is executed?
  2. Assume that the following equals method is added to the class definition of A:

    public boolean equals(Object o) {
      if (o == this) return true;
      else if (o == null) return false;
      else if (!(o instanceof A)) return false;
      else {
        A a = (A)o;
        return this.x.equals(a.x) && this.y.equals(a.y);
      }
    }
    

    What is printed to the screen when the go method is executed now?

  3. What is "wrong" with the following equals method for class A?

    public boolean equals(Object o) {
      if (o == this) return true;
      else if (o == null) return false;
      else if (!(o instanceof A)) return false;
      else {
        A a = (A)o;
        return a.x == this.x && a.y == this.y;
      }
    }