SWE 619 In Class Exercise # 13 Extra


Consider Bloch's Point/ColorPoint example from exercise 9A.
public class Point {  // routine code
   private int x; private int y;    

   public Point(int x; int y) {
      this.x = x; this.y = y;
      //  Bloch wouldn't like a call to an overridable method here.  Why not?
   }
  
   @Override public boolean equals(Object obj) {  // Standard recipe
      if (!(obj instanceof Point)) return false;

      Point p = (Point) obj;
      return p.x == x && p.y == y;
   }
}
  1. Bloch would like to see hashCode() overridden. Do it!

  2. Suppose we want to have Point implement the Comparable interface. Analyze the following possibilites from the contract perspective. As usual, try to come up with test cases where appropriate.

    • Comparisons just based on the value of x.
    • Comparisons based on the sum of x and y.
    • Comparisons based first on the value of x, and if these match, then on the value of y.


  3. Why wouldn't Bloch like a call to an overridable method in the constructor? Be concrete!