import java.util.Scanner; /* Java application whose purpose is to test various Java classes that ** implement the RedBlueClassifier interface. ** ** Author: R. McCloskey ** Date: April 2026 */ public class Tester { public static void main(String[] args) { // Display menu for user. System.out.println("Choose among these:"); System.out.println(" (1) Classify integers by prime vs. nonprime"); System.out.println(" (2) Classify integers by negative vs. nonnegative"); System.out.println(" (3) Classify integers by small vs. big"); System.out.println(" (4) Classify Strings by word vs. nonword"); System.out.print("\n> "); // Get user's response. Scanner s = new Scanner(System.in); int response = s.nextInt(); // Establish variable f to refer to a RedBlueFilter that // filters according to the criteria chosen by the user. RedBlueFilter rbf = null; if (response == 1) { rbf = new RedBlueFilter(new PrimeVsComposite()); } else if (response == 2) { rbf = new RedBlueFilter(new NegVsPosInt()); } else if (response == 3) { System.out.println("Enter smallest big number: "); int lowestBig = s.nextInt(); rbf = new RedBlueFilter(new SmallVsBigInt(lowestBig)); } else if (response == 4) { rbf = new RedBlueFilter(new WordVsNonWord()); } else { System.out.println("Invalid response; aborting program"); System.exit(0); } // Make an array x[] filled with either some integers or Strings // in accord with the kind of values the user has chosen. Object[] x; if (response < 4) { x = new Integer[] { 3, -4, 77, 0, 95, -2, -5, 64, 13, -1, 41, -22, 0, 27, 23 }; } else { x = new String[] { "glorp", "25.54", "two_words", "ab%6cd", "spock", "xyz@#$%uv", "the", "dog", "4+7", "hello", "Scranton", "R2D2" }; } // Display the pre-partitioned array. System.out.println("\nArray elements:"); displayArraySegment(x, 0, x.length); System.out.println(); // Filter x[] to display its RED elements System.out.println("\nElements that are RED:"); rbf.filter(x, true); System.out.println("\nElements that are BLUE:"); rbf.filter(x, false); System.out.println(); } /* Displays the elements in the given array segment (z[low..high)). */ private static void displayArraySegment(Object[] z, int low, int high) { for (int i = low; i != high; i++) { System.out.print(z[i] + " "); } } }