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

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

COMP 401, Spring 2008I. The Word classPublic methods See restrictions belowPrivate methods (needed for good words only)II. The WordList classConstructorsIII. The program401-28.1COMP 401, Spring 2008Program 3: Word class and dictionary – using recursionDue: 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 testingIn this program you will create a word class, a word list class, and some recursive methods that act on words.I. The Word classWrite 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 Bad wordshello R2D2TarHeel Blue Devil (space between words and just plain bad)cannot can’tnontrivial non-trivialtwenty 20asdf *$&#^$abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxyzThe Word class has the following methods; some of which may need to be abstract; some can actually be implemented in the parent.Public methods2See restrictions belowgood() Returns true for GoodWord objects; returns false for BadWord objects. (boolean)toString() Returns the word. (String).getWordPad(int n) 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.setWord(String s) Sets the word to s. (void).length() Returns the word length (int).equals(String s) For good words, return true if the word is exactly equal to s For bad words, return false. (boolean). isPerm(String s) For good words, return true if the parameter s and the word arepermutations of each other. For bad words, return false. (boolean)1 This may seem like a long way off, but don’t forget spring break.2 Types in parentheses are the type of the returned value.401-28.2isPrefix(String s) 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)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.equalsR(String a, String b) Returns true if a and b are exactly equal. (10)isPermR(String a, String b) Returns true if a is a permutation of b. (4)isPrefixR(String a, String b) 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 andisPrePerm are closely related. One can be used to implement the other. (11)ConstructorsWord() Do nothing.GoodWord(String s) Create a new GoodWord object with s as the word.BadWord(String s) Create a new BadWord object with s as the word.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 classWrite a WordList class that maintains a Vector or ArrayList containing good and bad word objects. It should include the following public methods.ConstructorsWordList() Create a WordList object with default size and increment. MethodsaddWord(Word w) Add Word w to the list (void). (4)getWord(int i) 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)size() Return the current size of the WordList (int). (4)setPad(int n) Set the padding amount to n (void). (4)search(Word w) Return the position of the first occurrence of w in the list. If w is not in the list, return -1 (int). (7)toString() 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.3III. The program1. Testing the WordList classCreate 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 dictionaryThe dictionary file is available on the web. Copy it and paste it into your project folder. An exampleof reading a text file can be found in handout 401-26. Read the dictionary file into a WordList of theappropriate 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 interfaceEnter a user interface loop that does the followingPrompt 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 theinput 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 arepermutations 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 inputword is a


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
Download Program 3- Word class and dictionary – using recursion
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?