5,6d4 < import java.util.Hashtable; < import java.util.Enumeration; 19,20c17,18 < public class Homework3 extends DefaultDisplay { < /*----------------------------------------------------------------------*/ --- > public class Homework3Base extends DefaultDisplay { > 29d26 < WordCounter the_word_counter = new WordCounter(); 44,45c41 < the_word_counter.add_word(the_word_just_read); < // the_text_output.appendText(the_word_just_read + "\n"); --- > the_text_output.appendText(the_word_just_read + "\n"); 51,58d46 < try { < the_text_output.appendText(" -- The top 10 words are:\n"); < for (int i = 1 ; i <= 10 ; i++) { < the_text_output.appendText(i + ": " + the_word_counter.remove_most_frequent() + "\n"); < } < } catch(WordCounterException the_exception) { < the_text_output.appendText(the_exception.getMessage() + "\n"); < } 62,115d49 < /** < * This exception is thrown if an attempt is made to remove a word from an < * empty WordCounter. < */ < class WordCounterException extends Exception { < /*----------------------------------------------------------------------*/ < public WordCounterException() { super(); } < /*----------------------------------------------------------------------*/ < public WordCounterException(String s) { super(s); } < } < < < /** < * This class is used to count the number of times each token appears in the text. < */ < class WordCounter extends Hashtable { < < /*----------------------------------------------------------------------*/ < /** < * This method adds a new word to the table, or increments the counter on < * an existing word. < */ < public void add_word(String the_word) { < if (containsKey(the_word)) < put(the_word, new Integer(((Integer) get(the_word)).intValue() + 1)); < else < put(the_word, new Integer(1)); < } < < /*----------------------------------------------------------------------*/ < /** < * This method removes the word with the hightest count from the table, and < * returns a string containing the word and its associated count. < */ < public synchronized String remove_most_frequent() throws WordCounterException { < String the_current_key, the_maximum_key = ""; < int the_current_value, the_maximum_value = 0; < Enumeration all_the_keys = keys(); < < if (!all_the_keys.hasMoreElements()) < throw new WordCounterException("Hmm. There are no more words."); < < while (all_the_keys.hasMoreElements()) { < the_current_key = (String) all_the_keys.nextElement(); < the_current_value = ((Integer) get(the_current_key)).intValue(); < if (the_current_value > the_maximum_value) { < the_maximum_value = the_current_value; < the_maximum_key = the_current_key; < } < } < remove(the_maximum_key); < return (the_maximum_key + "[" + the_maximum_value + "]"); < } < }