import java.io.IOException; import java.lang.ArrayIndexOutOfBoundsException; import java.lang.NumberFormatException; import java.util.Vector; import rebelsky.io.SimpleReader; import rebelsky.io.SimpleOutput; import rebelsky.util.NumberRangeException; /** * Allow interactive testing of the java.util.Vector. * Used to extend understanding of what that class does in various * situations. * * @author Samuel A. Rebelsky * @author YOUR NAME HERE * @version 1.0 of February 1998 */ public class VectorTester { // +------------+--------------------------------------------------------- // | Attributes | // +------------+ /** * The object used for output. */ protected SimpleOutput out; /** * The object used for input. */ protected SimpleReader in; /** * The vector we're testing. */ protected Vector vec; // +--------------+------------------------------------------------------- // | Constructors | // +--------------+ /** * Build a new tester. */ public VectorTester() { in = new SimpleReader(); out = new SimpleOutput(); vec = new Vector(); } // VectorTester() // +---------+------------------------------------------------------------ // | Methods | // +---------+ /** * Append an element to the current vector. */ public void append(String val) { vec.addElement(val); } // append(String) /** * Look up a value. */ public void lookup(int pos) { try { out.println(pos + ": " + vec.elementAt(pos)); } catch (ArrayIndexOutOfBoundsException e) { out.println(pos + " is an invalid position"); } } // lookup(int) /** * Give instructions for using this program. */ public void giveHelp() { out.println("a val append a value"); out.println("h get this help message"); out.println("l pos look up the value at position pos"); out.println("p print the current vector"); out.println("q quit"); } // giveHelp() /** * Print the current vector. */ public void print() { out.println(vec.toString()); } // print() // +------------+--------------------------------------------------------- // | Attributes | // +------------+ /** * Repeatedly prompt for what to do and then do it. */ public static void main(String[] args) { // The object used to drive the testing VectorTester tester = new VectorTester(); // The base part of the user's response String resp; // The value the user wants to add or retrieve String val; // The index the user wants to use int index; // Provide some basic instructions tester.giveHelp(); // Keep going until we hit the end of file or the user quits. // One exit is done using "break", and the other is done using // a catch of the end-of-file exception, so it looks as if our // loop continues indefinitely. try { LOOP: while (true) { // Print a prompt tester.out.println(); tester.out.print("> "); // Get the first response resp = tester.in.readString(); // Make sure the response is non-empty if (resp.length() == 0) { tester.out.println("You must give a non-empty response."); tester.giveHelp(); continue; } // Determine what to do based on the first character switch (resp.charAt(0)) { case 'a': // append // Get the value and append it val = tester.in.readString(); tester.append(val); break; case 'h': // help tester.giveHelp(); break; case 'l': // lookup // Get the position and look up try { index = tester.in.readInt(); tester.lookup(index); } catch (NumberFormatException e) { tester.out.println("Invalid position"); } catch (NumberRangeException e) { tester.out.println("Invalid position"); } break; case 'p': // print tester.print(); break; case 'q': // quit break LOOP; } // switch // Skip anything else on the line tester.in.readLine(); } //while } // try doing the input catch (IOException e) { // We're done } // catch(IOException) } // main } // VectorTester