Spaces:
Running
Running
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()); | |
} | |
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 | |