I just need the if statements I don\'t quite understand how to implement them to
ID: 3864788 • Letter: I
Question
I just need the if statements I don't quite understand how to implement them to the code
PLEASE READ CAREFULLY
Assignment:
1. Save a copy of working file to PointApplicationsComparable2_firstInitialLastName.java
2. Change the compareTo method so that it implements RULE #2
3. Write your comments at the beginning of the program to detail describe how the array is sorted
4. Copy and paste your output at the end of program in a pair of /* and */ (as comments) so that I can tell the array is sorted according to the rule #2
5. Save a copy of working file to PointApplicationsComparable3_firstInitialLastName.java
6. Change the compareTo method so that it implements RULE #3
7. Write your comments at the beginning of the program to detail describe how the array is sorted
8. Copy and paste your output at the end of program in a pair of /* and */ (as comments) so that I can tell the array is sorted according to the rule #3
Here's 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.
The programming writes the rule and Arrays.sort() will sort your array accordingly
Here's 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);
}
// The If statements go here
public int compareTo(Point obj) {
return x - obj.getX();
}
}
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
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);
}
// The If statements go here
public int compareTo(Point obj) {
if(y > obj.y)
return 1;
else if(y < obj.y)
return -1;
else{// y are equal
if(x > obj.x)
return 1;
else if(x < obj.x)
return -1;
else
return 0;
}
}
}