// This program tests stage 3 of the LetterInventory class. // The add and subtract methods import java.util.*; import java.io.*; public class Test3 { public static String[] LETTERS1 = new String[] {"aaggghhhh", "hello", "Four score and seven years ago", "Mitt Romney", "Cellar Door", "one day at a time"}; public static String[] LETTERS2 = new String[] {"ggg", "", "See your greed!", "inert", "Old Car Lore", "a stich in time"}; public static final int GETS_PER_LINE = 5; public static void main(String[] args) { for (int i = 0; i < LETTERS1.length; i++) { String s1 = LETTERS1[i]; String s2 = LETTERS2[i]; System.out.println("Testing these two strings:"); System.out.println(" i1: \"" + s1 + "\""); System.out.println(" i2: \"" + s2 + "\""); System.out.println("constructing i1 and i2"); System.out.println(); LetterInventory i1 = null; LetterInventory i2 = null; try { i1 = new LetterInventory(s1); i2 = new LetterInventory(s2); } catch (Exception e) { System.out.println("failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("testing i1.add(i2)"); System.out.println("\ti1: \"" + s1 + "\""); System.out.println("\ti2: \"" + s2 + "\""); System.out.println("\tli = i1.add(i2)"); try { LetterInventory test = i1.add(i2); check(test); } catch (Exception e) { System.out.println("add failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("testing i2.add(i1)"); System.out.println("\ti1: \"" + s1 + "\""); System.out.println("\ti2: \"" + s2 + "\""); System.out.println("\tli = i2.add(i1)"); try { LetterInventory test = i2.add(i1); check(test); } catch (Exception e) { System.out.println("add failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("testing i1.subtract(i2)"); System.out.println("\ti1: \"" + s1 + "\""); System.out.println("\ti2: \"" + s2 + "\""); System.out.println("\tli = i1.subtract(i2)"); try { LetterInventory test = i1.subtract(i2); check(test); } catch (Exception e) { System.out.println("subtract failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("testing i2.subtract(i1)"); System.out.println("\ti1: \"" + s1 + "\""); System.out.println("\ti2: \"" + s2 + "\""); System.out.println("\tli = i2.subtract(i1)"); try { LetterInventory test = i2.subtract(i1); check(test); } catch (Exception e) { System.out.println("subtract failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println(); } } // post: prints out the provided letter inventories // toString, size, isEmpty, and get calls public static void check(LetterInventory tester) { if (tester == null) { System.out.println("\tli = null"); } else { testToString(tester); testSize(tester); testIsEmpty(tester); testGet(tester); System.out.println(""); System.out.println(""); } } // post: prints out the result of calling .toString() public static void testToString(LetterInventory tester) { String test = ""; try { test = tester.toString(); } catch (Exception e) { System.out.println("toString failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("\tli.toString() = " + test); } // post: prints to the screen the result of calling // the provided LetterInventorie's .size() method public static void testSize(LetterInventory tester) { int test = 0; try { test = tester.size(); } catch (Exception e) { System.out.println("size failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("\tli.size()=" + test); } // post: prints to the screen the result of calling // the provided LetterInventory's .isEmpty() method public static void testIsEmpty(LetterInventory tester) { boolean test = false; try { test = tester.isEmpty(); } catch (Exception e) { System.out.println("isEmpty failed"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } System.out.println("\tli.isEmpty()=" + test); } // post: prints out the result of calling the provided // LetterInventory's .get methods public static void testGet(LetterInventory tester) { System.out.print("\t"); for (char ch = 'a'; ch <= 'z'; ch++) { // int correct = input.nextInt(); int test = 0; try { test = tester.get(ch); } catch (Exception e) { System.out.println("get failed for '" + ch + "'"); System.out.println(" threw exception: " + e); int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" in LetterInventory line#" + line); System.exit(1); } if ((ch - 'a') % GETS_PER_LINE == 0 && ch != 'a') { System.out.println(""); System.out.print("\t"); } System.out.print("li.get('" + ch + "')=" + test + " "); } } }