import java.util.Arrays; /* Java application whose purpose is to test instances of the classes ** PointSorterByX and PointSorterByDistFromOrig. ** Author: R. McCloskey ** Date: October 2023 */ public class PointSorterTester { public static void main(String[] args) { // Establish an array of Point objects on which to perform testing. Point[] pointAry = new Point[] { new Point(3.5, 6.2), new Point(-4.8, 0.4), new Point(0.0, 0.0), new Point(-12.5, 1.75), new Point(-2.0, -3.5), new Point(0.5, 6.3), new Point(8.25, 4.7), new Point(1.25, 2.0) }; displayPoints(pointAry, "Array before sorting:"); PointSorterByX psbx = new PointSorterByX(); psbx.sort(pointAry); displayPoints(pointAry, "\nAfter sorting by x-coordinates"); if (isSortedByX(pointAry)) { System.out.println("That is correct!"); } else { System.out.println("Your PointSorterByX class must be faulty!"); } PointSorterByDistFromOrig psbdfo = new PointSorterByDistFromOrig(); psbdfo.sort(pointAry); displayPoints(pointAry, "\nAfter sorting by distances from origin"); if (isSortedByDistFromOrig(pointAry)) { System.out.println("That is correct!"); } else { System.out.println("Your PointSorterByDistFromOrig class must be faulty!"); } System.out.println("\nGoodbye."); } /* Displays the points in the given array after printing the given message. */ private static void displayPoints(Point[] pAry, String message) { System.out.println(message); System.out.println(Arrays.toString(pAry)); } /* Reports whether or not the elements of the given array are in ** ascending order with respect to their x-coordinates. ** It prints a message for every adjacent pair of elements that ** is not in proper order with respect to each other. */ private static boolean isSortedByX(Point[] pAry) { boolean goodSoFar = true; for (int i=1; i < pAry.length; i++) { if (pAry[i-1].x > pAry[i].x) { goodSoFar = false; System.out.printf("ERROR: %s precedes %s\n", pAry[i-1], pAry[i]); } } return goodSoFar; } /* Reports whether or not the elements of the given array are in ** ascending order with respect to their distances from the origin. ** It prints a message for every adjacent pair of elements that ** is not in proper order with respect to each other. */ private static boolean isSortedByDistFromOrig(Point[] pAry) { boolean goodSoFar = true; for (int i=1; i < pAry.length; i++) { if (pAry[i-1].distFromOrig() > pAry[i].distFromOrig()) { goodSoFar = false; System.out.printf("ERROR: %s precedes %s\n", pAry[i-1], pAry[i]); } } return goodSoFar; } }