import java.util.Comparator; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java program having as its purpose to build (and afterwards, display) ** several lists, all containing the same StateInUSA objects, but ** each list ordered by a different criterion as defined by an instance ** of a class that implements the java.util.Comparator interface. ** ** Author: R. McCloskey ** Date: April 2026 */ public class Tester { private static String fileName; public static void main(String[] args) { fileName = args[0]; // name of input file System.out.println("Ordered by name:"); test(new CompareByName()); System.out.println("\n\nOrdered by area:"); test(new CompareByArea()); System.out.println("\n\nOrdered by electoral votes:"); test(new CompareByElectVotes()); System.out.println("\n\nOrdered by electoral votes with name tie-breaker:"); test(new CompareByElectVotesAndName()); System.out.println("\nGoodbye."); } /* Builds and then displays an OrderedIndexedList of StateInUSA ** objects whose descriptions are read from a file. The list's elements ** are intended to be in ascending order in accord with the Comparator ** provided via this method's parameter. If they are not, an error ** message is displayed. */ private static void test(Comparator comparator) { // Set up a Scanner to read from the input file descriptions of // StateInUSA objects. The file's first line is skipped // over. Subsequent lines have the form of CSV-strings with three // fields: name, area, and number of electoral votes. Scanner input = null; try { input = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(System.out); System.out.println("Program aborting"); System.exit(0); } input.nextLine(); // skip first line // Make an empty OrderedIndexedList capable of holding instances // of the StateInUSA class. OrderedIndexedList list; list = new OrderedIndexedList(comparator); // Populate the list with the regions described in the input file while (input.hasNextLine()) { String line = input.nextLine(); StateInUSA region = parseString(line); // System.out.println("Inserting: " + region); list.insert(region); } // Display the list list.printList(); // System.out.println(list); // See if the list's elements really are in ascending order; // if not, display an error message. if (!isAscending(list, comparator)) { System.out.println("** ERROR: List elements are not in ascending order!"); } } /* Given a CSV-string describing a state in the USa (with three fields ** that provide a name, area, and # of electoral votes respectively), ** returns a corresponding new instance of the StateInUSA class. */ private static StateInUSA parseString(String s) { Scanner scanner = new Scanner(s); scanner.useDelimiter(","); String name = scanner.next(); int area = scanner.nextInt(); int electoralVotes = scanner.nextInt(); return new StateInUSA(name, area, electoralVotes); } /* Returns true iff the elements of the given list are in ascending ** order with respect to the ordering defined by the given Comparator. */ private static boolean isAscending(OrderedIndexedList list, Comparator c) { boolean goodSoFar = true; if (list.size() < 2) { } else { int i = 1; StateInUSA prev = list.get(0); while (i != list.size() && goodSoFar) { StateInUSA current = list.get(i); if (c.compare(prev, current) > 0) { goodSoFar = false; } else { prev = current; } i = i+1; } } return goodSoFar; } }