TCSS 143, Autumn 2004
Homework 4: Anagrams (Exceptions, I/O, HashMap/Set)
Assigned: Mon 2004/11/08
Due: Tue 2004/11/16, 11:59pm
This assignment is designed to test your understanding of exception handling, input and output (I/O), and the HashMap and HashSet collections.
By popular request, this assignment may be submitted either alone or with a partner. See the Submission section for details.
Background Information:
This program focuses on spell-checking and anagrams. Spell-checking consists of examining words to see if they are contained in a dictionary collection. If the word is contained in the dictionary, the word is considered to be spelled correctly. For example, "inheritance" is spelled correctly and "ithenirance" is not.
Anagrams are words that contain the same letters, not necessarily in the same order. For example, "loop", "pool", and "polo" are all anagrams of each other, because each contains one "l", two "o"s, and one "p". Any word is considered to be an anagram of itself.
For the purposes of this program, we will also consider two phrases to be anagrams of each other if they have the same number of words, and each word is an anagram of the corresponding word in the other phrase. For example, "marginal manila" and "alarming animal" are anagrams of each other because each phrase has two words, and "marginal" and "alarming" are anagrams of each other, and "manila" and "animal" are anagrams of each other.
You will write a class that performs spell-checking and anagram-checking on words and phrases. Your code will read a spell-checking dictionary of words from a file, and a list of anagram sets from a file. The spell-checking dictionary file contains one word per line, like this:
aardvark
abacus
abalone
abandon
The anagram set file contains one set of anagrams per line, separated by whitespace, like this:
not ton
note tone
noted toned
noter toner tenor notre
After reading the spell-checking and anagram files, your class must be able to examine arbitrary strings representing single words or long phrases, and determine whether they are spelled correctly, and/or whether two strings are anagrams of each other as defined above.
Implementation:
You will write a class Css143Dictionary that implements two interfaces, provided to you by the instructor: SpellChecker and AnagramChecker. Download these interfaces plus a testing program, TestChecker, and the spell-checking dictionary file (words.txt) and anagram list file (anagrams.txt) from the course web site.
The following are the methods in the each interface that your class must implement. For all methods, you may assume that the argument(s) passed are not null.
SpellChecker methods:
public void readDictionary(String filename)
Reads the file with the given file name, which is assumed to contain the spell-checking dictionary. Stores each word from the file into a collection in your object, so that words and phrases can be checked for spelling. You should catch any FileNotFoundException when the file name is invalid and handle it as though the file were empty, by using an empty spell-checking dictionary.
public boolean wordSpellCheck(String word)
Returns whether the given string represents a word in the spell-checking dictionary. If the spell-checking dictionary has not been read yet, this method should throw an IllegalStateException.
public boolean phraseSpellCheck(String phrase)
Returns whether the given string represents a phrase where each word in the phrase is in the spell-checking dictionary. If the spell-checking dictionary has not been read yet, this method should throw an IllegalStateException.
|
AnagramChecker methods:
public void readAnagrams(String filename)
Reads the file with the given file name, which is assumed to contain the anagram list. Stores each set of anagrams from the file into a collection in your object, so that words and phrases can be checked to see whether they are anagrams. You should catch any FileNotFoundException when the file name is invalid and handle it as though the file were empty, by using an empty anagram-checking dictionary.
public boolean wordAnagramCheck(String word1, String word2)
Returns whether the two strings represent words that are anagrams of each other; that is, words that appear together on some line in the anagram file. If the anagram file has not been read yet, this method should throw an IllegalStateException.
public boolean phraseAnagramCheck(String phrase1, String phrase2)
Returns whether the given strings represent phrases that are anagrams of each other; that is, where the two phrases have the same number of words, and each word in phrase1 is an anagram of the corresponding word in phrase2. If the spell-checking dictionary has not been read yet, this method should throw an IllegalStateException.
|
Since your dictionary class will primarily be used for lookups, you should store your spelling and anagram data in HashMap and HashSet collections as appropriate. Storing your data in other collections such as ArrayList or LinkedList will perform much more poorly and will not receive full credit.
Hints and Suggestions:
Here are some helpful pointers about what collections you should use to implement your class:
-
The spell-checking dictionary is a group of words, one per line. You will want to be able to search this group of words quickly to see whether a word is in the group.
Given word w, is w in the dictionary?
-
The anagram-checking file contains many lines. On each line is a group of words that are all anagrams of each other. You will want to be able to find the anagram group for a given word quickly, so that you can see if the other word is also in that group.
Given words w1 and w2, do both of them belong to the same anagram group?
Or: Given word w1, what anagram group is it in? Is w2 in the same anagram group?
Remember that a collection can contain other collections as elements. For example, it is legal to have an ArrayList of HashSets or a HashMap of LinkedLists. Be creative!
Submission and Grading:
Submit this assignment online, as with all programming assignments. Turn in your Css143Dictionary code only.
You may work individually or with one partner. If you work with a partner, please email the instructor to tell who is in your group, and put both partners' names in the comments in each file you turn in. Teammates should work together with each other, but this program is still subject to the plagiarism and conduct rules specified in the course syllabus; this means that one team should not share code with another team.
A sample solution will be posted to the course web site. You can run this sample solution to test how your program should behave. When in doubt or when behavior is otherwise unspecified, match the behavior of the sample solution exactly.
The correctness of your program will be graded by calling the methods of your dictionary in various combinations and examining the results. Exceptions should not occur under normal usage except as specified previously. Your code should not produce any console output, such as System.out.println statements in your code that were not specified.
The design of your program will be graded on whether you follow the program specification above, whether you implement the interface given, whether you use appropriate hash collections to implement your dictionary, and the reasonableness of your code and algorithms.
Your style will be graded on the presence of reasonable brief comments in your source code (a short header with your name and course on each class, a header on each method, and a comment on particularly complex code sections), the use of meaningful identifier names, appropriate use of modifiers such as public, private, protected, and static; avoiding redundancy, and general spacing and indentation of your code.
of
Share with your friends: |