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

PLEASE READ CAREFULLY I need two if statements for my code below of rule#2 and r

ID: 3803741 • Letter: P

Question

PLEASE READ CAREFULLY

I need two if statements for my code below of rule#2 and rule#3 I don't quite understand how to implement them

Assignment:

Change the compareTo method so that it implements RULE #2

Change the compareTo method so that it implements RULE #3

Heres the rules:

RULE #2: If the y-coord of obj1 is larger than the obj2 (where obj1 and obj2 are Point objects), then obj1 is larger. Else if the y-coord of obj1 is equal than the obj2, then we would like to compare the x-coord to determine which one is larger. Else the obj2 is larger.

RULE #3: If the sum of x-coord and y-coord obj 1 is larger than the obj2 (where obj1 and obj2 are Point objects), then obj1 is larger. Else the obj2 is larger.

Heres the code:

import java.util.*;
public class PointApplicationsComparable2   {
   public static void main(String args[])   {
       //create 10 random points
       Point [] pArr = new Point[10];
       int xVal, yVal;
       Random rd = new Random();
  
       for (int i = 0; i < 10; i++) {
           xVal = rd.nextInt(300);
           yVal = rd.nextInt(200);
           pArr[i] = new Point(xVal, yVal);
       }
       System.out.println(" Before Sort **********");
       System.out.println();
       for (int i = 0; i < 10; i++) {
           System.out.println(i + " " + pArr[i]);
       }
       Arrays.sort(pArr);
       System.out.println(" After Sort **********");
       for (int i = 0; i < 10; i++) {
           System.out.println(i + " " + pArr[i]);
       }
      
   }

}
class Point implements Comparable<Point> { // this class implements Comparable interface
   int x, y;
   public Point() {
       x = 0;
       y = 0;
   }
   public Point(int xCoor, int yCoor) {
       x = xCoor;
       y = yCoor;
   }
   public int getX() {return x; }
   public int getY() {return y; }
   public String toString() {
       return "point: " + x + "," + y + " The sum of x and y is " + (x + y);
   }
  
  
  
  
   public int compareTo(Point obj) {
  
   // statement goes here

       return x - obj.getX();
   }
  
}

  
  

Explanation / Answer

Hi,

Please see below the classfor two scenarios.

Please comment fro any queries/feedbacks.

Thanks,

Anita

CompareTo() based on RULE# 2

RULE #2: If the y-coord of obj1 is larger than the obj2 (where obj1 and obj2 are Point objects), then obj1 is larger. Else if the y-coord of obj1 is equal than the obj2, then we would like to compare the x-coord to determine which one is larger. Else the obj2 is larger.

PointApplicationsComparable2.java

import java.util.*;
public class PointApplicationsComparable2 {
   public static void main(String args[]) {
       //create 10 random points
       Point [] pArr = new Point[10];
       int xVal, yVal;
       Random rd = new Random();

       for (int i = 0; i < 10; i++) {
           xVal = rd.nextInt(300);
           yVal = rd.nextInt(200);
           pArr[i] = new Point(xVal, yVal);
       }
       System.out.println(" Before Sort **********");
       System.out.println();
       for (int i = 0; i < 10; i++) {
           System.out.println(i + " " + pArr[i]);
       }
       Arrays.sort(pArr);
       System.out.println(" After Sort **********");
       for (int i = 0; i < 10; i++) {
           System.out.println(i + " " + pArr[i]);
       }

   }
}
class Point implements Comparable<Point> { // this class implements Comparable interface
   int x, y;
   public Point() {
       x = 0;
       y = 0;
   }
   public Point(int xCoor, int yCoor) {
       x = xCoor;
       y = yCoor;
   }
   public int getX() {return x; }
   public int getY() {return y; }
   public String toString() {
       return "point: " + x + "," + y + " The sum of x and y is " + (x + y);
   }


  
   /**
   * compareTo()based on RULE #2:
   * RULE #2: If the y-coord of obj1 is larger than the obj2 (where obj1 and obj2 are Point objects),
   * then obj1 is larger.
   * Else if the y-coord of obj1 is equal than the obj2, then we would like to compare the x-coord to determine which one is larger.
   * Else the obj2 is larger.
   *
   * */
   public int compareTo(Point obj ) {

       int retVal =0 ;

       if(y > obj.getY()){
           retVal = 1;
       }
       else if(y < obj.getY()){
           retVal = -1;
       }
       else if(y == obj.getY()){ //Need to check X coordinates if y coordinates are equal
           if(x > obj.getX()){
               retVal = 1;
           }
           else if(x < obj.getX()){
               retVal= -1;
           }
           else if(x == obj.getX()){
               retVal= 0;

           }
       }
       // statement goes here
       return retVal;
   }

}

Sample output:


Before Sort **********

0 point: 288,171   The sum of x and y is 459
1 point: 20,191   The sum of x and y is 211
2 point: 271,121   The sum of x and y is 392
3 point: 42,64   The sum of x and y is 106
4 point: 82,98   The sum of x and y is 180
5 point: 241,26   The sum of x and y is 267
6 point: 12,118   The sum of x and y is 130
7 point: 223,52   The sum of x and y is 275
8 point: 124,107   The sum of x and y is 231
9 point: 20,80   The sum of x and y is 100

After Sort **********
0 point: 241,26   The sum of x and y is 267
1 point: 223,52   The sum of x and y is 275
2 point: 42,64   The sum of x and y is 106
3 point: 20,80   The sum of x and y is 100
4 point: 82,98   The sum of x and y is 180
5 point: 124,107   The sum of x and y is 231
6 point: 12,118   The sum of x and y is 130
7 point: 271,121   The sum of x and y is 392
8 point: 288,171   The sum of x and y is 459
9 point: 20,191   The sum of x and y is 211

CompareTo() based on RULE# 3

If the sum of x-coord and y-coord obj 1 is larger than the obj2 (where obj1 and obj2 are Point objects), then obj1 is larger. Else the obj2 is larger.

PointApplicationsComparable2.java

import java.util.*;
public class PointApplicationsComparable2 {
   public static void main(String args[]) {
       //create 10 random points
       Point [] pArr = new Point[10];
       int xVal, yVal;
       Random rd = new Random();

       for (int i = 0; i < 10; i++) {
           xVal = rd.nextInt(300);
           yVal = rd.nextInt(200);
           pArr[i] = new Point(xVal, yVal);
       }
       System.out.println(" Before Sort **********");
       System.out.println();
       for (int i = 0; i < 10; i++) {
           System.out.println(i + " " + pArr[i]);
       }
       Arrays.sort(pArr);
       System.out.println(" After Sort **********");
       for (int i = 0; i < 10; i++) {
           System.out.println(i + " " + pArr[i]);
       }

   }
}
class Point implements Comparable<Point> { // this class implements Comparable interface
   int x, y;
   public Point() {
       x = 0;
       y = 0;
   }
   public Point(int xCoor, int yCoor) {
       x = xCoor;
       y = yCoor;
   }
   public int getX() {return x; }
   public int getY() {return y; }
   public String toString() {
       return "point: " + x + "," + y + " The sum of x and y is " + (x + y);
   }


  
   /**
   * compareTo()based on RULE #3:
   * If the sum of x-coord and y-coord obj 1 is larger than the obj2 (where obj1 and obj2 are Point objects),
   * then obj1 is larger. Else the obj2 is larger.
   *
   * */
   public int compareTo(Point obj ) {

       int retVal =0 ;

       if((x+y) > (obj.getY() + obj.getX())){
           retVal = 1;
       }
       else if((x+y) < (obj.getY() + obj.getX())){
           retVal = -1;
       }
       else if((x+y) == (obj.getY() + obj.getX())){ //Need to check X coordinates if y coordinates are equal
          
               retVal= 0;

       }
       // statement goes here
       return retVal;
   }

}

Sample output:


Before Sort **********

0 point: 102,167   The sum of x and y is 269
1 point: 238,74   The sum of x and y is 312
2 point: 70,6   The sum of x and y is 76
3 point: 74,120   The sum of x and y is 194
4 point: 264,120   The sum of x and y is 384
5 point: 92,108   The sum of x and y is 200
6 point: 270,17   The sum of x and y is 287
7 point: 247,53   The sum of x and y is 300
8 point: 191,184   The sum of x and y is 375
9 point: 80,188   The sum of x and y is 268

After Sort **********
0 point: 70,6   The sum of x and y is 76
1 point: 74,120   The sum of x and y is 194
2 point: 92,108   The sum of x and y is 200
3 point: 80,188   The sum of x and y is 268
4 point: 102,167   The sum of x and y is 269
5 point: 270,17   The sum of x and y is 287
6 point: 247,53   The sum of x and y is 300
7 point: 238,74   The sum of x and y is 312
8 point: 191,184   The sum of x and y is 375
9 point: 264,120   The sum of x and y is 384

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote