// This program tests stage 2 of the LetterInventory class. import java.util.*; import java.io.*; public class Test2 { public static final char[] LETTERS = new char[] {'g', 'a', 'h', 'm', 'g', 'b', 'z', 'p', 'z', 'b', 'a', 'h', 'p', 'm', 'G', 'A', 'H', 'M'}; public static final int[] COUNTS = new int[] { 3, 2, 4, 1, 0, 2, 1, 4, 0, 0, 0, 0, 0, 0, 3, 2, 4, 1 }; public static final int GETS_PER_LINE = 5; public static void main(String[] args) { LetterInventory tester = new LetterInventory(""); System.out.println("Starting with empty inventory"); check(tester); System.out.println(); for (int i = 0; i < LETTERS.length; i++) { char ch = LETTERS[i]; int count = COUNTS[i]; System.out.println("setting count for " + ch + " to " + count); try { tester.set(ch, count); } 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); } check(tester); System.out.println(); } } // pre : // post: prints out the provided LetterInventory's size, toString, and // isEmpty() methods public static void check(LetterInventory tester) { System.out.print(".size()="); testSize(tester); System.out.print(".toString()="); testToString(tester); System.out.print(".isEmpty()="); testIsEmpty(tester); testGet(tester); System.out.println(); } // post: prints out result of calling toString() on // the provided LetterInventory 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(test); } // post: prints out the result of calling // the provided LetterInventory'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(test); } // post: prints out 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(test); } // post: prints out the result of calling the provided // LetterInventory's .get method on every character a-z public static void testGet(LetterInventory tester) { for (char ch = 'a'; ch <= 'z'; ch++) { 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(".get('" + ch + "')=" + test + " "); } } }