import IntSeq; /** * Test some of the methods given in IntSeq. * * @author Samuel A. Rebelsky * @version 1.1 of January 1999 */ public class IntSeqTester { /** * Provide a ``front end'' for testing Intseq. */ public static void main(String[] args) { // Prepare to read input and generate output SimpleInput in = new SimpleInput(); SimpleOutput out = new SimpleOutput(); // Set up a sequence IntSeq numbers = new IntSeq(); // The intended length of the sequence int length; // Should we print the sequence? 0 for no, 1 for yes int print; // A counter variable for loops int i; // The user's command. String command; // Are we done? boolean done = false; // Keep going until the user indicates that the program is complete. while ((!done) && (!in.eof())) { // Print a summary of options. out.println(); out.println("What do you want to do? "); out.println(" fill: fill the vector."); out.println(" set: set elements in the vector."); out.println(" search: search the vector."); out.println(" print: print the vector."); out.println(" small: find small elements in the vector."); out.println(" quit: quit the program."); out.print("Command: "); // Get a command from the user. command = in.readString(); // Convert it to lower case for testing. command = command.toLowerCase(); // Determine what to do based on the command. The options are // organized alphabetically. // Should we fill the sequence? if (command.equals("fill")) { // The option selected by the user (see below). int option; // The starting index of the range to fill. int lb; // The ending index of the range to fill. int ub; // The start value (for option 1 below). int start; // The step value (for option 1 below). int step; // The random seed (for options 2 and 4 below). int seed; // Give the options. out.println("Select an option:"); out.println(" 1: Fill a range with a regular sequence"); out.println(" 2: Fill a range with a specified random sequence"); out.println(" 3: Fill a range with an unspecified random sequence"); out.println(" 4: Fill a range with a specified increasing random sequence"); out.println(" 5: Fill a range with an unspecified increasing random sequence"); out.print("Command: "); // Read the response option = in.readInt(); // Read the bounds. out.print("Enter start index: "); lb = in.readInt(); out.print("Enter end index: "); ub = in.readInt(); // Do whatever is appropriate for the specified option. switch (option) { case 1: // Regular sequence out.print("Enter starting value: "); start = in.readInt(); out.print("Enter step value: "); step = in.readInt(); numbers.fill(lb,ub,start,step); break; case 2: // Specified random sequence out.print("Enter seed: "); seed = in.readInt(); numbers.randomFill(lb,ub,seed); break; case 3: numbers.randomFill(lb,ub,0); break; case 4: out.print("Enter seed: "); seed = in.readInt(); numbers.randomFillIncreasing(lb,ub,seed); break; case 5: numbers.randomFillIncreasing(lb,ub,0); break; default: out.println("Invalid selection: " + option); } // switch } // Fill // Should we print the sequence? else if (command.equals("print")) { out.println(numbers.toString()); } // Print // Should we quit the program? else if (command.equals("quit")) { done = true; } // Quit // Should we search through the sequence? else if (command.equals("search")) { // The value to search for. int requested; // The search method selected. String method; // The index found. int index; // Prompt for value to search for. out.print("What value should we search for? "); requested = in.readInt(); // Prompt for a search method. out.print("Enter search method (sequential or binary): "); method = in.readString(); method = method.toLowerCase(); // Do the search if (method.equals("sequential")) { index = numbers.sequentialSearch(requested); } else if (method.equals("binary")) { index = numbers.binarySearch(requested); } else { out.println("Unknown search method: '" + method + "'"); index = -1; } // Print the results. if (method.equals("sequential") || (method.equals("binary"))) { if (index == -1) { out.println("Sorry, can't find that element."); } else { out.println("Element " + requested + " is at position " + index); } } // if it's a legal search method } // Search // Should we allow the user to set elements? else if (command.equals("set")) { // The index of the elment to set. int index; // The value to set it to. int val; out.print("Enter the index of the value to set: "); index = in.readInt(); out.print("Enter the value for position " + index + ": "); val = in.readInt(); numbers.set(index,val); } // Set // Should we find smaller elements? else if (command.equals("small")) { // The number of the algorithm to run (see the instructions). int algorithm; // Give the user an option. out.println("Select an algorithm: "); out.println(" 1: find the smallest element"); out.println(" 2: find the two smallest elements"); out.println(" 3: find the five smallest elements using the naive method"); out.println(" 4: find the five smallest elements using a better method"); out.print("Command: "); // And read the selection. algorithm = in.readInt(); // Run the algorithm and print the results switch (algorithm) { case 1: // smallest element out.println("The smallest element is " + numbers.smallest()); break; case 2: // two smallest elements numbers.twoSmallest(); out.println("The smallest element is " + numbers.get(0)); out.println("The next smallest element is " + numbers.get(1)); break; case 3: // five smallest elements, naive method numbers.fiveSmallest(); for (i = 0; i < 5; ++i) { out.println("The " + i + "th smallest element is " + numbers.get(i)); } // for break; case 4: // five smallest elements, better method numbers.newFiveSmallest(); for (i = 0; i < 5; ++i) { out.println("The " + i + "th smallest element is " + numbers.get(i)); } // for break; default: /// Unknown algorithm out.print("Sorry, I only know four algorithms."); break; } // switch } // Small // Hmmm ... an unknown command. else { out.println("Sorry, don't understand '" + command + "'"); } // Unknown command } // while } // main(String[]) } // class SortTester