Unformatted text preview:

401 28 1 COMP 401 Spring 2008 Program 3 Word class and dictionary using recursion Due March 27 2008 4 30 pm est 1 Objectives Write recursive methods Use inheritance Read a text file Use a Vector or ArrayList Perform some simple JUnit testing In this program you will create a word class a word list class and some recursive methods that act on words I The Word class Write a parent abstract Word class with two children GoodWord and BadWord The data component is a word a String object Good words are words that contain only letters and are no more than 25 characters long bad words are strings that contain any character other than letters or are more than 25 letters long For example Good words hello TarHeel cannot nontrivial twenty asdf abcdefghijklmnopqrstuvwxy Bad words R2D2 Blue Devil space between words and just plain bad can t non trivial 20 abcdefghijklmnopqrstuvwxyz The Word class has the following methods some of which may need to be abstract some can actually be implemented in the parent Public methods2 See restrictions below good Returns true for GoodWord objects returns false for BadWord objects boolean Returns the word String Returns a string of length n containing the word left justified padded if necessary with spaces If the word is longer than n letters return the whole word Sets the word to s void Returns the word length int For good words return true if the word is exactly equal to s For bad words return false boolean For good words return true if the parameter s and the word are permutations of each other For bad words return false boolean toString getWordPad int n setWord String s length equals String s isPerm String s 1 2 This may seem like a long way off but don t forget spring break Types in parentheses are the type of the returned value 401 28 2 For good words return true if the parameter s is a prefix of word For bad words return false boolean isPrePerm String s For good words return true if the parameter s is a prefix of a permutation of the word For bad words return false boolean isPrefix String s Private methods needed for good words only The real work of some of the public methods should be done by recursive private methods All return a boolean Returns true if a and b are exactly equal 10 Returns true if a is a permutation of b 4 Returns true if a is a prefix of b The empty string is a prefix of any string 10 isPrePermR String a String b Returns true if a is a prefix of a permutation of b Note that isPermR and isPrePerm are closely related One can be used to implement the other 11 equalsR String a String b isPermR String a String b isPrefixR String a String b Constructors Do nothing Create a new GoodWord object with s as the word Create a new BadWord object with s as the word Word GoodWord String s BadWord String s Notice that there s nothing preventing good words from being put into BadWord objects and vice versa This is deliberate and allows us to decide or change what constitutes good and bad words This also means that the good method should return a stored boolean and not recompute the goodness or badness of a word II The WordList class Write a WordList class that maintains a Vector or ArrayList containing good and bad word objects It should include the following public methods Constructors WordList Create a WordList object with default size and increment Methods addWord Word w getWord int i size setPad int n search Word w toString Add Word w to the list void 4 Return the Word object in the ith position of the list Return null if i is out of bounds less than zero or beyond the list size Word 7 Return the current size of the WordList int 4 Set the padding amount to n void 4 Return the position of the first occurrence of w in the list If w is not in the list return 1 int 7 Return a String containing the first 10 words in this word list each padded to the padding amount If there are fewer than 10 words return all of them String Put a new line character n after the fifth word and after the last word 6 401 28 3 III The program 1 Testing the WordList class Create a JUnit test class that tests your WordList class It should create one or more WordList objects and perform at least the following tests You can add these tests incrementally to your test class as you implement your WordList class An empty WordList should have size 0 After adding some words the list should have the appropriate size Searching for the words that you added should result in successful searches Searching for words that you didn t add should result in unsuccessful searches If a search for w returns an integer i then w should be found in position i and not before 2 Phase 1 reading the dictionary The dictionary file is available on the web Copy it and paste it into your project folder An example of reading a text file can be found in handout 401 26 Read the dictionary file into a WordList of the appropriate GoodWord or BadWord objects Convert everything to lower case before inserting it into the dictionary When the dictionary has been read in display the following statistics The number of lines read from the dictionary file The number of good words the number of bad words and the total number of words The number of good words of each length e g how many good one letter words how many good two letter words etc 3 Phase 2 user interface Enter a user interface loop that does the following Prompt the user to enter a word Convert all upper case letters to lower case If the word is empty or not good issue an appropriate error message If the word is good do the following a Indicate whether the word is in the dictionary b Create a WordList containing all the good words in the dictionary for which the input word is a prefix Display the number of words found and the first ten words in the list appropriately labeled c Create a WordList containing all the good words in the dictionary that are permutations of the input word Display the number of permutations found and the first ten words in the list appropriately labeled d Create a WordList containing all the good words in the dictionary for which the input word is a prefix of a permutation Display the number of words found and the first ten words in the list appropriately labeled This list could be quite large For example if the user enters e then the list is made up of all words that contain an e Hint you should create all three WordLists with a single pass through the dictionary Continue the user interface loop until the user …


View Full Document

UNC-Chapel Hill COMP 401 - Program 3- Word class and dictionary – using recursion

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view Program 3- Word class and dictionary – using recursion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Program 3- Word class and dictionary – using recursion and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?