import java.util.Comparator; import java.util.Arrays; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application for the purpose of illustrating how to make use ** of instances of classes that implement the Comparator and ArraySorter ** interfaces to sort an array. */ public class SelSortTester { private static String inputFileName; public static void main(String[] args) { //throws FileNotFoundException { // The first command line argument is taken to be the name of the // file from which input is to be read. inputFileName = args[0]; // Call method that reads a bunch of points into an array, sorts the // array with the help of a comparator based on distance-to-origin, // and prints the resulting array. readSortPrint(inputFileName, new DistToOrigComparator()); } private static void readSortPrint(String fileName, Comparator comp) { Scanner input = null; // Establish a scanner to read data from the file whose name was given. try { input = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("\nProgram aborting..."); System.exit(0); } // Establish an array containing the points described in the input file PointIn2Space[] points = readPoints(input); // Display the array System.out.println("Array established:"); System.out.println(Arrays.toString(points)); // Establish an object that can sort an array into ascending order // in accord with the given Comparator. SelectionSorter sorter = new SelectionSorter(comp); // Sort the array sorter.sort(points); // Print the resulting array System.out.println("After sorting, the array contains"); System.out.println(Arrays.toString(points)); } /* Returns a new array containing the points described by the input ** accessible to the given scanner, which assumes that the first token ** is on a line by itself and indicates how many points are subsequently ** described. */ private static PointIn2Space[] readPoints(Scanner input) { final int N = input.nextInt(); input.nextLine(); PointIn2Space[] result = new PointIn2Space[N]; for (int i=0; i != N; i++) { double x = input.nextDouble(); double y = input.nextDouble(); result[i] = new PointIn2Space(x,y); } return result; } }