import java.util.Scanner; /** Java application whose purpose is to partition some arrays using ** instances of the RedBluePartitioner class, thereby testing that ** class as well as some classes that implement the RedBlueClassifier ** interface. */ public class RB_PartTester { public static void main(String[] args) { // Display menu for user. System.out.println("Choose among these:"); System.out.println(" (1) Partition int's by prime vs. nonprime"); System.out.println(" (2) Partition int's by negative vs. nonnegative"); System.out.println(" (3) Partition int's by small vs. big"); System.out.println(" (4) Partition 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 p to refer to a RedBluePartitioner that // partitions according to the criteria chosen by the user. RedBluePartitioner p = null; if (response == 1) { p = new RedBluePartitioner(new PrimeVsComposite()); } else if (response == 2) { p = new RedBluePartitioner(new NegVsPosInt()); } else if (response == 3) { System.out.println("Enter smallest big number: "); int lowestBig = s.nextInt(); p = new RedBluePartitioner(new SmallVsBigInt(lowestBig)); } else if (response == 4) { p = new RedBluePartitioner(new WordVsNonWord()); } else { System.out.println("Invalid response; aborting program"); System.exit(1); } // 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(); // Partition x[], storing the index at which the BLUE segment begins. int rbBoundary = p.partition(x); // Display the post-partitioned array. System.out.println("\nAfter partitioning,"); System.out.print(" RED segment: "); displayArraySegment(x, 0, rbBoundary); System.out.println(); System.out.print(" BLUE segment: "); displayArraySegment(x, rbBoundary, x.length); 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] + " "); } } }