TArray0.java
Select all
/* TArray0.java David Klick CIS 260 3/23/09 This program demonstrates a number of Java features including keyboard input, string tokenization, the use of multiple classes, customized exceptions, exception handling, and a Hashtable. A simple parser controls access to an unordered array, which the user can manipulate interactively. */ import java.util.*; import java.io.*; public class TArray0 { 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, INSERT, 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); hash.put("INSERT_AT", Command.INSERT); hash.put("INSERTAT", Command.INSERT); hash.put("INS", Command.INSERT); hash.put("I", Command.INSERT); 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 UnorderedArray a = new UnorderedArray(arrayLength); // intialize command lookup table Hashtable
commands = initCommandTable(); // process commands displayHelp(); System.out.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 INSERT: if (cmdLength != 3) error("INSERT command takes two arguments"); else { if (!isInteger(cmd[1])) { error("INSERT command position argument must be an integer"); continue; } if (!isInteger(cmd[2])) { error("INSERT command value argument must be an integer"); continue; } position = Integer.parseInt(cmd[1]); value = Integer.parseInt(cmd[2]); try { a.insertElement(position, value); println("Element with value " + value + " inserted at " + position); } 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) { System.out.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) { System.out.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) { System.out.println(e); } 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("Index out of bounds"); if (pos > numElements) throw new MyArrayException("Index not in populated part of array"); if (numElements < size) { for (int i=size-1; i>pos; i--) arr[i] = arr[i-1]; arr[pos] = val; numElements++; } else throw new MyArrayException("Array full"); } public int findElement(int val) { for (int i=0; i