package username.dict; import java.io.PrintWriter; /** * A simple, generic, test of dictionaries. * * @author Samuel A. Rebelsky * @version 1.0 of April 2005 */ public class NewDictTester { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The dictionary we're testing. */ NewDict dict; /** The place to print output. */ PrintWriter pen; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Build a tester for _dict that sends its output to _pen. */ public NewDictTester(NewDict _dict, PrintWriter _pen) { this.dict = _dict; this.pen = _pen; } // NewDictTester(NewDict, PrintWriter) // +----------------+------------------------------------------ // | Public Methods | // +----------------+ /** * A simple series of tests, based on an association list * written by Benjamin Gum. */ public void test() { this.printStars(); pen.println("Original Dictionary: "); dict.dump(pen); this.printStars(); pen.println("Searching in original dictionary: "); this.sampleSearch(); this.printStars(); pen.println("Filling with Rocky and friends"); this.addJayWard(); dict.dump(pen); this.printStars(); pen.println("Searching in revised dictionary: "); this.sampleSearch(); this.printStars(); pen.println("Filling with other characters"); this.addOthers(); dict.dump(pen); this.printStars(); pen.println("Searching in revised dictionary: "); this.sampleSearch(); this.printStars(); pen.println("Filling with other characters"); this.addOthers(); dict.dump(pen); this.printStars(); pen.println("Searching in revised dictionary: "); this.sampleSearch(); this.printStars(); pen.println("Replacing Batman's sidekick"); this.dict.put("Batman","Batgirl"); dict.dump(pen); this.printStars(); pen.println("Searching in revised dictionary: "); this.sampleSearch(); } // test() // +-----------------+----------------------------------------- // | Private Methods | // +-----------------+ /** * Add the characters of Jay Ward. */ void addJayWard() { this.dict.put("Rocky", "Bullwinkle"); this.dict.put("Mr. Peabody", "Sherman"); this.dict.put("Bullwinkle", "Rocky"); } // addJayWard() /** * Add some miscellaneous other characters. */ void addOthers() { this.dict.put("Batman", "Robin"); this.dict.put("Asterix", "Obelix"); this.dict.put("Dick Dastardly", "Muttley"); this.dict.put("Secret Squirrel", "Morocco Mole"); this.dict.put("Quick Draw McGraw", "Bab Looey"); this.dict.put("Yogi", "Booboo"); this.dict.put("Bart Simpson", "Milhouse Van Houten"); } // addOthers() /** * Print a row of stars as a separator. */ void printStars() { pen.println("**************************************************"); } // printStars() /** * Do a sample search */ void sampleSearch() { // Prepare an array of sample strings for searching String[] samples = new String[]{ "Rocky","Bullwinkle","Batman","SamR" }; for (int i = 0; i < samples.length; i++) { String result = dict.get(samples[i]); if (result == null) { pen.println(samples[i] + " has no sidekick."); } else { pen.println(samples[i] + "'s sidekick is " + result); } // else } // for } // sampleSearch() } // class NewDictTester