CS/SWE 332 In Class Exercise Number 17B
October 25, 2017

Name(s):
This is an "extra" in-class exercise designed to reinforce the last homework and prepare for the next quiz. Everyone in your group should be able to navigate this exercise.
 public class Point implements Comparable <Point> {
    private int x;  private int y;
    public Point(int a, int b) { x = a; y = b; }

    @Override public boolean equals (Object obj) {
       if (!(obj instanceof Point)) { return false; }
       else {
          Point p = (Point) obj;
          return (this.x == p.x && this.y == p.y);
       }
    }

    @Override public int compareTo (Point p) {
      return this.x - p.x;
    }
 }

 Point p = new Point(3, 5);
 Point q = new Point(4, 8);
 Point r = new Point(3, 8);

 p.compareTo(q);     // line 1
 q.compareTo(r);     // line 2
 p.compareTo(r);     // line 3

 Set<Point> t = new TreeSet<Point>();

 t.add(p);
 t.add(q);
 t.add(r);
 t.size();    // line 4
  1. What is the result of line 1?

  2. What is the result of line 2?

  3. What is the result of line 3?

  4. What is the result of line 4?

  5. compareTo(a,b)  is not consistent with a.equals(b). Give an example which shows the problem, and explain the issue. Explain how the problem could be solved by implementing compareTo(a,b) so that it is consistent with a.equals(b).