Code /* TArray1.java David Klick CIS 260 3/23/09 This program is essentially the same as TArray0 with the exception that an ordered array is used instead of an unordered array. Due to the array being ordered, there is no insert instruction available. */ import java.util.*; import java.io.*; public class TArray1 { private static final int DEFAULT_ARRAY_LENGTH = 10; private static BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); public enum Command { QUIT, PRINT, STATUS, HELP, ADD, DELETE_AT, DELETE, FIND } private static Hashtable initCommandTable() { Hashtable hash = new Hashtable(); hash.put("QUIT", Command.QUIT); hash.put("Q", Command.QUIT); hash.put("EXIT", Command.QUIT); hash.put("X", Command.QUIT); hash.put("PRINT", Command.PRINT); hash.put("P", Command.PRINT); hash.put("STATUS", Command.STATUS); hash.put("STAT", Command.STATUS); hash.put("S", Command.STATUS); hash.put("HELP", Command.HELP); hash.put("H", Command.HELP); hash.put("FIND", Command.FIND); hash.put("F", Command.FIND); hash.put("?", Command.FIND); hash.put("ADD", Command.ADD); hash.put("A", Command.ADD); hash.put("+", Command.ADD); hash.put("DELETE", Command.DELETE); hash.put("DEL", Command.DELETE); hash.put("D", Command.DELETE); hash.put("-", Command.DELETE); hash.put("DELETE_AT", Command.DELETE_AT); hash.put("DELETEAT", Command.DELETE_AT); return hash; } private static void error(String msg) { System.out.println(msg); } private static boolean isInteger(String s) { return s.matches("[+-]?[0-9]+"); } private static void println(String msg) { System.out.println(msg); } public static void main(String[] args) throws IOException { boolean done = false; int arrayLength = DEFAULT_ARRAY_LENGTH; // process command line parameters (array length) if (args.length == 1) { try { arrayLength = Integer.parseInt(args[0]); if ((arrayLength > 20) || (arrayLength < 5)) { println("Illegal array length specified"); arrayLength = DEFAULT_ARRAY_LENGTH; } } catch (NumberFormatException e) { error("Unrecognized command line argument"); System.exit(0); } } else if (args.length > 1) { error("Only one command line parameter allowed. " + args.length + " found."); System.exit(0); } else { error("No command line parameters found. Using default" + " array size of " + DEFAULT_ARRAY_LENGTH + "."); } // create array to be used for application OrderedArray a = new OrderedArray(arrayLength); // intialize command lookup table Hashtable commands = initCommandTable(); // process commands displayHelp(); println("\nProgram ready for processing"); while (!done) { int position, value; String[] cmd = getCommand(); int cmdLength = cmd.length; Command command = commands.get(cmd[0].toUpperCase()); if (command == null) error("Unrecognized command"); else { switch (command) { case QUIT: if (cmdLength > 1) error("QUIT command takes no arguments"); else done = true; break; case PRINT: if (cmdLength > 1) error("PRINT command takes no arguments"); else a.print(); break; case STATUS: if (cmdLength > 1) error("STATUS command takes no arguments"); else displayStatus(a); break; case HELP: if (cmdLength > 1) error("HELP command takes no arguments"); else displayHelp(); break; case FIND: if (cmdLength != 2) error("FIND command takes one argument"); else { if (!isInteger(cmd[1])) error("FIND command argument must be an integer"); else { position = a.findElement(Integer.parseInt(cmd[1])); if (position == -1) println(cmd[1] + " not found in array"); else println(cmd[1] + " found at position " + position); } } break; case ADD: if (cmdLength != 2) error("ADD command takes one argument"); else if (!isInteger(cmd[1])) error("ADD command argument must be an integer"); else { try { value = Integer.parseInt(cmd[1]); a.addElement(value); println("Element with value " + value + " added"); } catch (MyArrayException e) { error(e.getMessage()); } } break; case DELETE: if (cmdLength != 2) error("DELETE command takes one argument"); else if (!isInteger(cmd[1])) error("DELETE argument must be an integer"); else { value = Integer.parseInt(cmd[1]); if (a.deleteElement(value)) println("Element with value " + value + " deleted"); else println("Element with value " + value + " not found"); } break; case DELETE_AT: if (cmdLength != 2) error("DELETE_AT command takes one argument"); else if (!isInteger(cmd[1])) error("DELETE_AT argument must be an integer"); else { position = Integer.parseInt(cmd[1]); try { a.deleteElementAt(position); } catch (MyArrayException e) { println(e.getMessage()); } } break; default: error("Unrecognized command"); } } } // end of command processing loop println("End of processing"); } public static String[] getCommand() { String s = new String(); System.out.print("\n> "); try { while ((s = in.readLine()).length() == 0); } catch (IOException e) { println("I/O Exception"); } s = s.toUpperCase(); StringTokenizer p = new StringTokenizer(s); String[] cmds = new String[p.countTokens()]; try { int i = 0; while (p.hasMoreTokens()) cmds[i++] = p.nextToken(); } catch (NoSuchElementException e) { println(e.getMessage()); } if (cmds.length > 0) { // This handles the special case where the first character // is a plus (insert) or minus (delete) and is followed by // a number without any delimiter in between. This code // breaks the single "word" into two separate "words" char c = cmds[0].charAt(0); if ((cmds[0].length() > 1) && ((c == '+') || (c == '-'))) { String[] newCmds = new String[cmds.length + 1]; for (int i=1; i= size) throw new MyArrayException("Array full"); else { arr[numElements++] = val; Arrays.sort(arr, 0, numElements); } } public int findElement(int val) { for (int i=0; i