Spaces:
Running
Running
condition compares using double, then return int
Browse files
Week 6: Methods of OO Programming/08. Comparable Point
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the program, the class 'Point', which you are familiar from the beginning of the tutorial, is defined.
|
| 2 |
+
|
| 3 |
+
Implement a 'comparison method' for the class:
|
| 4 |
+
public int compareTo(Point anotherPoint)
|
| 5 |
+
|
| 6 |
+
The comparison value of two points is based on the distance of the points from the origin - the greater the distance, the "larger" the point.
|
| 7 |
+
Note that the class already has a necessary method for calculating the distance.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import java.util.ArrayList;
|
| 13 |
+
import java.util.Collections;
|
| 14 |
+
import java.util.Random;
|
| 15 |
+
|
| 16 |
+
public class Test{
|
| 17 |
+
public static void main(String[] args){
|
| 18 |
+
final Random r = new Random();
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
ArrayList<Point> points = new ArrayList<>();
|
| 22 |
+
|
| 23 |
+
points.add(new Point(-4,-3));
|
| 24 |
+
points.add(new Point(1,2));
|
| 25 |
+
points.add(new Point(6,-3));
|
| 26 |
+
points.add(new Point(-1,0));
|
| 27 |
+
points.add(new Point(-14,1));
|
| 28 |
+
points.add(new Point(1,11));
|
| 29 |
+
points.add(new Point(-5,-5));
|
| 30 |
+
points.add(new Point(-1,8));
|
| 31 |
+
|
| 32 |
+
System.out.println("Points before:");
|
| 33 |
+
points.stream().forEach(pt -> System.out.println("" + pt));
|
| 34 |
+
|
| 35 |
+
Collections.sort(points);
|
| 36 |
+
System.out.println("Points sorted:");
|
| 37 |
+
points.stream().forEach(pt -> System.out.println(pt));
|
| 38 |
+
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class Point implements Comparable<Point>{
|
| 47 |
+
private int x;
|
| 48 |
+
private int y;
|
| 49 |
+
|
| 50 |
+
public Point(int x, int y) {
|
| 51 |
+
this.x = x;
|
| 52 |
+
this.y = y;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
public int getX() {
|
| 56 |
+
return x;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
public void setX(int x) {
|
| 60 |
+
this.x = x;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
public int getY() {
|
| 64 |
+
return y;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
public void setY(int y) {
|
| 68 |
+
this.y = y;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
//////////////////////////////////////////////
|
| 73 |
+
// object method calls class method
|
| 74 |
+
//////////////////////////////////////////////
|
| 75 |
+
public double getDistance() {
|
| 76 |
+
return Point.distanceFromOrigin(this);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
public static double distanceFromOrigin(Point point) {
|
| 80 |
+
return Math.sqrt(point.getX() * point.getX() +
|
| 81 |
+
point.getY() * point.getY());
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
@Override
|
| 85 |
+
public String toString() {
|
| 86 |
+
return "(" + x + "," + y + ") - distance: " + getDistance();
|
| 87 |
+
}
|
| 88 |
+
//////////////////////////////////////////////
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
//ADD
|
| 95 |
+
public int compareTo(Point anotherPoint) {
|
| 96 |
+
double thisDistance = this.getDistance();
|
| 97 |
+
double otherDistance = anotherPoint.getDistance();
|
| 98 |
+
|
| 99 |
+
if (thisDistance < otherDistance) {
|
| 100 |
+
return -1;
|
| 101 |
+
}
|
| 102 |
+
else if (thisDistance > otherDistance) {
|
| 103 |
+
return 1;
|
| 104 |
+
}
|
| 105 |
+
else {
|
| 106 |
+
return 0;
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
Points before:
|
| 120 |
+
(-4,-3) - distance: 5.0
|
| 121 |
+
(1,2) - distance: 2.23606797749979
|
| 122 |
+
(6,-3) - distance: 6.708203932499369
|
| 123 |
+
(-1,0) - distance: 1.0
|
| 124 |
+
(-14,1) - distance: 14.035668847618199
|
| 125 |
+
(1,11) - distance: 11.045361017187261
|
| 126 |
+
(-5,-5) - distance: 7.0710678118654755
|
| 127 |
+
(-1,8) - distance: 8.06225774829855
|
| 128 |
+
|
| 129 |
+
Points sorted:
|
| 130 |
+
(-1,0) - distance: 1.0
|
| 131 |
+
(1,2) - distance: 2.23606797749979
|
| 132 |
+
(-4,-3) - distance: 5.0
|
| 133 |
+
(6,-3) - distance: 6.708203932499369
|
| 134 |
+
(-5,-5) - distance: 7.0710678118654755
|
| 135 |
+
(-1,8) - distance: 8.06225774829855
|
| 136 |
+
(1,11) - distance: 11.045361017187261
|
| 137 |
+
(-14,1) - distance: 14.035668847618199
|
| 138 |
+
|