import java.util.*; import java.io.*; public class ThesaurusTester { protected Thesaurus thesaurus; protected BufferedReader keyboardReader; /** * Initializes this ThesaurusTester object. * */ public ThesaurusTester() { thesaurus = new Thesaurus(); keyboardReader = new BufferedReader (new InputStreamReader (System.in)); } // default constructor /** * Constructs this ThesaurusTester from a path read in from the keyboard. * The worstTime(n) is O(n log n). * */ public void constructThesaurus() { final String FILE_PROMPT = "\nPlease enter the path for the thesaurus file: "; final String NO_INPUT_FILE_FOUND_MESSAGE = "Error: there is no file with that path.\n\n"; BufferedReader fileReader; String inFilePath, line; boolean pathOK = false; while (!pathOK) { try { System.out.print (FILE_PROMPT); inFilePath = keyboardReader.readLine(); fileReader = new BufferedReader (new FileReader (inFilePath)); pathOK = true; while (true) { line = fileReader.readLine(); if (line == null) break; thesaurus.add (line); } // while not at end of file } // try catch (IOException e) { System.out.println (e); } // catch } // while !pathOK } // method constructThesaurus /** * Prints the synonyms of each word entered from the keyboard, or a * no-synonyms-found message. * The worstTime(n, m) is O(m log n), where n is the number of lines in * the thesaurus file, and m is the number of words entered from the * keyboard. * */ public void printSynonyms() { final String SENTINEL = "***"; final String WORD_PROMPT = "\n\nPlease enter the sentinel (" + SENTINEL + ") or a word: "; final String WORD_NOT_FOUND_MESSAGE = "That word does not appear in the thesaurus."; final String SYNONYM_MESSAGE = "The synonyms of that word are "; String word; LinkedList synonymList; while (true) { try { System.out.print (WORD_PROMPT); word = keyboardReader.readLine(); if (word.equals (SENTINEL)) break; synonymList = thesaurus.getSynonyms (word); if (synonymList == null) System.out.println (WORD_NOT_FOUND_MESSAGE); else System.out.println (SYNONYM_MESSAGE + synonymList); } // try catch (IOException e) { System.out.println (e); } // catch } // while } // printSynonyms } // class ThesaurusTester