File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class Coordinate implements Comparable <Coordinate > {
2+
3+ private int x ;
4+ private int y ;
5+
6+ public Coordinate (int x , int y ) {
7+ this .x = x ;
8+ this .y = y ;
9+ }
10+
11+ public int getX () {
12+ return x ;
13+ }
14+
15+ public int getY () {
16+ return y ;
17+ }
18+
19+ public double getDistanceToOriginPoint () {
20+ return Math .hypot (x , y );
21+ }
22+
23+ public int compareTo (Coordinate c ) {
24+ return Double .valueOf (this .getDistanceToOriginPoint ()).compareTo (c .getDistanceToOriginPoint ());
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .Collections ;
3+
14public class Exercise {
25
36 public static void main (String [] args ) {
4- // implement exercise here
7+ ArrayList <Coordinate > coordinates = new ArrayList <>();
8+
9+ coordinates .add (new Coordinate (3 , 5 ));
10+ coordinates .add (new Coordinate (7 , 6 ));
11+ coordinates .add (new Coordinate (2 , 1 ));
12+ coordinates .add (new Coordinate (6 , 8 ));
13+ coordinates .add (new Coordinate (1 , 9 ));
14+
15+ Collections .sort (coordinates );
16+
17+ for (Coordinate c : coordinates ) {
18+ System .out .println (c + ": " + c .getDistanceToOriginPoint ());
19+ }
520 }
621}
You can’t perform that action at this time.
0 commit comments