CoolFace
Apppublic

KaiquanMah/TurkuBasicOOPinJava

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
08. Comparable Point139 linesDownload Raw Back to Week 6: Methods of OO Programming
1In the program, the class 'Point', which you are familiar from the beginning of the tutorial, is defined.2 3Implement a 'comparison method' for the class:4public int compareTo(Point anotherPoint)5 6The comparison value of two points is based on the distance of the points from the origin - the greater the distance, the "larger" the point.7Note that the class already has a necessary method for calculating the distance.8 9 10 11 12import java.util.ArrayList;13import java.util.Collections;14import java.util.Random;15 16public 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 46class 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 method74    //////////////////////////////////////////////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    @Override85    public String toString() {86        return "(" + x + "," + y + ") - distance: " + getDistance();87    }88    //////////////////////////////////////////////89 90 91 92 93 94    //ADD95    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 119Points before:120(-4,-3) - distance: 5.0121(1,2) - distance: 2.23606797749979122(6,-3) - distance: 6.708203932499369123(-1,0) - distance: 1.0124(-14,1) - distance: 14.035668847618199125(1,11) - distance: 11.045361017187261126(-5,-5) - distance: 7.0710678118654755127(-1,8) - distance: 8.06225774829855128 129Points sorted:130(-1,0) - distance: 1.0131(1,2) - distance: 2.23606797749979132(-4,-3) - distance: 5.0133(6,-3) - distance: 6.708203932499369134(-5,-5) - distance: 7.0710678118654755135(-1,8) - distance: 8.06225774829855136(1,11) - distance: 11.045361017187261137(-14,1) - distance: 14.035668847618199138 139