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: 3778890 • 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: c is smaller than c' if either x < x' or ( x = x' and y < y') c is equal to c' if x = x' and y = y' 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 +!, respectively to the above three cases.

Explanation / Answer

Coordinate.java

public class Coordinate {
   //Declaring instance variables
double x,y;

//Parameterized constructor
public Coordinate(double x, double y) {
   super();
   this.x = x;
   this.y = y;
}

//Setters and getters
public double getX() {
   return x;
}

public void setX(double x) {
   this.x = x;
}

public double getY() {
   return y;
}

public void setY(double y) {
   this.y = y;
}

//This method will compare the two coordinates
public int compareTo(Coordinate o)
{
   int val=0;
   if((this.x<o.x)|| (this.x==o.x && this.y<o.y))
   {
       val=-1;
   }
   else if(this.x==o.x && this.y==o.y)
   {
       val=0;
   }
   else if((this.x>o.x) || (this.x==o.x && this.y >o.y))
   {
       val=1;
   }
   return val;
}


/* toString() method is used to display
* the contents of an object inside it
*/
@Override
public String toString() {
   return "Coordinate [x=" + x + ", y=" + y + "]";
}


}

____________________

Test.java

public class Test {

   public static void main(String[] args) {
       //Declaring variable
       int val;
      
       //Creating the Coordinate class object by passing the values as arguments
       Coordinate c1=new Coordinate(5.5,6.6);
      
       //Creating the Coordinate class object by passing the values as arguments
       Coordinate c2=new Coordinate(5.5,6.6);
      
       /* Calling the compareTo() method by passing
       * the coordinate class object as argument
       */
       val=c1.compareTo(c2);
      
       /* based on the return value of the method
       * displaying whether the Coordinates are equal,
       * greater than or less than.
       */
       if(val==-1)
           System.out.println(":: C1 is Smaller than c2 ::");
       else if(val==0)
           System.out.println(":: C1 is equal to c2 ::");
       else if(val==1)
           System.out.println(":: C1 is greater than c2 ::");
      
       //Creating the Coordinate class object by passing the values as arguments
       Coordinate c3=new Coordinate(4.5,5.5);
      
      
       /* Calling the compareTo() method by passing
       * the coordinate class object as argument
       */
       val=c2.compareTo(c3);
      
       /* based on the return value of the method
       * displaying whether the Coordinates are equal,
       * greater than or less than.
       */
       if(val==-1)
           System.out.println(":: C2 is Smaller than c3 ::");
       else if(val==0)
           System.out.println(":: C2 is equal to c3 ::");
       else if(val==1)
           System.out.println(":: C2 is greater than c3 ::");
      
       //Creating the Coordinate class object by passing the values as arguments
       Coordinate c4=new Coordinate(6.0,5.0);
      
       /* Calling the compareTo() method by passing
       * the coordinate class object as argument
       */
       val=c3.compareTo(c4);
      
       /* based on the return value of the method
       * displaying whether the Coordinates are equal,
       * greater than or less than.
       */
       if(val==-1)
           System.out.println(":: C3 is Smaller than c4 ::");
       else if(val==0)
           System.out.println(":: C3 is equal to c4 ::");
       else if(val==1)
           System.out.println(":: C3 is greater than c4 ::");
   }

}

____________________________

Output:

:: C1 is equal to c2 ::
:: C2 is greater than c3 ::
:: C3 is Smaller than c4 ::

___________Thank You