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