CS 132
Computer Science II
List of Words (and Odwsr)

David B. Levine
Computer Science Department
St. Bonaventure University
Copyright, 2002

Objective

            In this lab we will continue our work from last week; we will build our first anagram-solving tool as well as our first collection class.

 

General Idea:

           If we are going to try to find all valid anagrams of a given word, then we will need some sort of dictionary of all of the possible words.  This week's lab involves creating such a list of words and manipulating it.

 

Instructions:

  1. We begin by modifying the Word class slightly.  We wish to create a new method, "equals".  We want to do this because equals is a standard Java name and we wish to conform.  In all ways except name, the equals method should be identical to the isAnagram method.  Implement this method by adding a new public method to the Word class that simply calls the isAnagram method.  Make the appropriate changes in your main method and check that everything still works.


  2. Copy the file AnagramsDriver.java into your folder.  Add it to your project.  


  3. Copy the file Anagrams.java into your folder.  Add it to your project.  Examine it carefully.  Note that it makes use of a third class, WordList, that we have not yet dealt with.  For now, simply work with an intuitive understanding of a WordList.

    An Anagrams object will be constructed out of a file (containing the dictionary) and will perform only a single task for us.  That task will be to generate anagrams in response to user suggestions of jumbled words.  The constructor is completely written for you, but the generateAnagrams method is incomplete.  Don't worry about completing it for now.  Instead, draw (on paper), an object diagram for the Anagrams class.  Be sure to include both data and methods.  (Hint: there won't be too much of either.)


  4. Now we will turn our attention to the WordList class.  It should contain a single instance variable which we will call allWords.  This variable will be an ArrayList.  Keeping in mind that a constructor must initialize each instance variable, create a small class named WordList that contains such a variable and a constructor.
  5.  

  6. Next, we need to be able to add words to our WordList.  Create a new (public) method named addWord that takes a String as a parameter.  Its body should consist of a line or two that simply adds the Word associated with the String to the ArrayList.  One adds any object to the ArrayList using the add method.  (Note:  Whether this uses one line or two depends upon to what degree you combine lines - as discussed in class last week.  The two-line solution first creates a temporary Word out of the String and then adds it to the ArrayList whose name is allWords; the one-line solution combines this, creating an anonymous Word as it goes.)


  7. Next, create a method printMatches that prints all of the matches for a given String.  The String should be passed as a parameter.  Your algorithm should be to turn the String into a Word and then go through the ArrayList and simply print out any word that "equals" the Word just created.  Note that the expression

    (Word) allWords.get(i)

    will get the Word from position i out of the ArrayList named allWords.


  8. Now add code to the generateAnagrams method in the Anagrams class that asks the WordList to printMatches appropriately.  (Once again, this will probably be only a line or two.)


  9. Copy the file small-words.txt and place it in your folder.  Test all of your code.  To know what words are in the dictionary, simply double-click on the file.


  10. One flaw with your code is that words that do not have anagrams generate no output, but also no message.  Modify your printMatches method so that if no anagrams are found, then the message "No matches were found." is printed.


  11. Now test the program under "real" conditions.  Copy the file words.txt to your folder.  Modify AnagramDriver.java to use this file instead of small-words.txt.  Solve for each of the words in the jumble.


  12. After verifying that they are properly documented, print each of the files, Word.java, WordList.java, and Anagrams.java.


  13. Finally, create an object diagram for the WordList class.

 

Hand in:

        Hand in a cover page that includes the four solved words from Step 10, the listings from Step 11, and the object diagrams from Steps 3 and 12.

 

Assignment Type (see Academic Practices and Policies Document):

            Individual assignment, limited collaboration.