Compsci 100, Anagrams, Spring 2006


See the FAQ


Anagrams

Two words are anagrams if they contain the same letters. For example, bagel and gable are anagrams as are glare, lager, large, and regal.

Word puzzles like the Jumble shown below, and found in many newspapers are based on anagrams too. You can also play jumbles online at jumble.com.

For example, here's a list of words such that the words on each line are anagrams of each other:


 bared beard bread debar debra
 abel able bale bela elba
 bares baser bears braes saber
 abets baste bates beast beats
 caster caters crates reacts recast traces
 caret cater crate react recta trace

Here's another list such that the words on each line are texting-equivalent anagrams, that is you'd press the same sequence of keys on a cell-phone keypad to text the words on each line (the key-sequence is shown with the words):

11626	 acres bards barer bares baser bases caper capes cards cares cases
1166	 barr bars bass caps carp carr cars
116726	 barter bastes carter carver carves caster castes
3552	 gone good goof home hone hood hoof
35526	 goner goods goofs homer homes honer hones hoods hoofs inner

In this program you'll be working to understand tradeoffs in determining how words are anagrams and in how object-oriented programs are constructed using what's called a model view pattern (sometimes called model, view, controller).

The program has two parts, you should complete Part I before moving onto Part II, and you should submit your code/solutions for each part separately as indicated below.


Part I

To get the code to start this project, use the Ambient/snarf plugin (alternatively you can browse the code directory). The site for Compsci 100, Spring 2006, for your snarf browser is www.cs.duke.edu/courses/spring06/cps100/snarf which you can enter by right-clicking in the snarf-site browser window of Eclipse.

The program SimpleAnagram.java launches the model and view that together comprise the program you'll be running. Initially the program reads a dictionary of words (you can read your own dictionary too) and prints the anagrams of the word typed into the textbox, which is pots in the screenshot below.

gui

There are other classes and interfaces that you'll need to understand, but for Part I you'll be creating a new subclass of AnagramModel.java and then using that from the SimpleAnagram.java launch/run code (you'll need to modify the launch/run code).

The goals of part I of this assignment are

  1. Learn about and understand inheritance and interfaces

  2. Learn about and understand the model-view architecture

  3. Design an algorithm for finding anagrams that is reasonably efficient.

Changes to the Code for Part I

The model for this program is the class AnagramModel.java whose process method finds all strings in a file that are anagrams of the String object passed to the process method. The GUI class SimpleViewer is the view for this program --- it interacts with the user, but the intelligence and state of the program are part of the model, not part of the view. The user enters a word in the GUI, which is then passed to the model. The model finds the anagrams and displays them in the view.

For Part I you'll change the launching/main program SimpleAnagram.java so that the GUI has no text box for a word. You'll also create a new model class with a different functionality as part of its process method --- it results in all anagrams in a file of words being displayed in the GUI. In summary, for the new program

  1. No input/text box should appear in the GUI -- see the screen shot below.

  2. Running the program should display in the view a list of all anagrams in the file loaded in the model. The user will not be prompted for a word as in the current version. Instead, after reading a file, the anagrams in the file are to be displayed in the GUI/view.

    The program reads one file, and finds the words in that file that are anagrams of each other.

Here's a screen shot showing the last few anagrams printed when the extra-credit option is used (see below) from the file words-lower.txt.

gui

In the view, each collection or sequence of anagrams is displayed on one line, and every line displayed should have at least two words that are anagrams. For example, the output of the program might be as follows (depending on the file read). Do not print lines with only one words, e.g., for the word "zoo" which doesn't have any anagrams.

Be sure that every line printed consists of words that are anagrams of each other and that every line printed has at least two words on it.

Implementation Details

Create AllAnagramModel as a subclass of AnagramModel. You'll need to implement the following methods in the subclass you create, and perhaps any helper methods these require.

  1. setWords which in the code you write will process all the words read and then notify the views appropriately so that the views display all the anagrams.

    When setWords is called, the views will display all anagrams (call notifyViews appropriately) and a message about the number of anagrams should be displayed (call messageViews appropriately). Note: setWords is called from initialize --- your view should call initialize.

  2. process should do nothing, except perhaps send a message to the views indicating that this version doesn't process single words.

  3. constructor for the subclass may need to initialize state --- note that parent-class consructor will be called automatically if your constructor has no parameters (default constructor).

  4. The simplest method for finding all anagrams involves sorting an array/ArrayList of Anaword objects. Then all anagrams will be contiguous in the sorted list.

You'll need to modify the code in SimpleAnagram so that an AllAnagramModel object is created. You'll need to modify the call that constructs the SimpleViewer object so that the title of the GUI changes and so that an empty string "" is passed as the label for the input box which results in no input box.

Note: you'll run SimpleAnagram, but your modified code is the new model used by that code.

Implementation Hint

To find the anagrams, sort the collection of Anaword objects, then run through looking for equal elements, these are anagrams. Construct a new ArrayList of strings that will be passed to the view, each string is one sequence of anagrams, formed by concatenating the string representations of words that are anagrams.

Efficiency

Correctness should be your first priority in writing your program. However, it is important that you write a reasonably efficient implementation of AllAnagramModel. Specifically, your program should not take more than around a minute to display process a 40,000 word file like lowerwords.txt.

Full/Extra Credit

Without doing this option the maximal grade on the assignment is the equivalent of an A-.

For A/A+/Extra credit the anagrams should be displayed in a particular order.

  1. Words in each line/sequence should appear in alphabetical order.

  2. The longest line/sequence (number of anagram-words) of anagrams should be printed last, the shortest --- length two --- sequences should be printed first.

  3. In sequences that have the same number of anagrams print sequences in order by word length: two-letter word sequences appear before three-letter word sequences.

  4. In sequences of the same length with the same number of letters in each anagram the sequences are ordered alphabetically by the first word in the sequence.

You should create a private/nested Comparator class and use this to sort the sequence of anagrams that are printed.

Grading

Part I is worth 25 points.

Criteria Points
Implemented as specified in general 10
Robust for all inputs/special cases 6
Documentation, style of code 5
README 4

In your README, which you must submit, you should include

The extra credit is worth 12 points, basically three points for each of the four parts.

Submit

To submit use Eclipse and the name anagrampartI.
Owen L. Astrachan
Last modified: Mon Jan 23 09:10:44 EST 2006