Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recall that Coordinate is a class defined with two double variables x and y repr

ID: 3778853 • Letter: R

Question

Recall that Coordinate is a class defined with two double variables x and y represenging the (x,y)-codinate of a point on the xy-plane. Suppose two coordinates c=(x,y) and c'=(x',y') are compared to each other as follows:

1. c is smaller than c' if either x < x' or ( x = x' and y < y')

2. c is equal to c' if x = x' and y = y'

3. c is greater than c' if either x > x' or ( x = x' and y > y')

According to this rule, write an instance method public int compareTo(Coordinate o) that compares this with o that returns -1, 0, and +1, respectively to the above three cases.

Explanation / Answer

public int compareTo(Coordinate o){
       if(this.x < o.x || (this.x == o.x && this.y < o.y)) return -1;
       if(this.x > o.x || (this.x == o.x && this.y > o.y)) return 1;
       else return 0;
   }