DOC PREVIEW
Yale CPSC 427 - Problem Set 4
School name Yale University
Pages 4

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:

Assignment GoalsWord TrianglesAssignmentCode ExplorationExtensions: Count TrianglesExtension: Find All TrianglesDeliverablesYALE UNIVERSITYDEPARTMENT OF COMPUTER SCIENCECPSC 427a: Object-Oriented Programming Handout #7Professor M. J. Fischer October 25, 2010Problem Set 4Due before midnight on Friday, November 5, 2010.1 Assignment Goals1. Explore a significant code base.2. Learn about iterators.3. Learn more about operator extensions, including casts.4. Learn how various C++ constructs interact.5. Analyze the class structure of a program.6. Extend an existing program in non-trivial ways.2 Word TrianglesA word triangle is a sequence of words, each of length one less than the preceding word inthe sequence, and ending with a minimal-length word. Moreover, each word is an anagram(letter rearrangement) of a subword of the previous word. An example should make thisclear:trilinearairlinerairlinealineralienileaailOf course, one can debate what is a “word”. For the above example, I used the dictionaryfile /usr/share/dict/words found on the Zoo.You will find a complete word triangle program on the Zoo under the path/c/cs427/assignments/ps4/PS4-triangle that implements the following algorithm:1. Obtain four parameters: dictionary file name, filter string, minimum length word inthe triangle, and maximum length word in the triangle.2. Read the entire dictionary file into memory, discarding words that are too long orthat contain letters not in the filter string.3. Arrange the dictionary into lists of words of the same length.2 Problem Set 44. Copy each word of the dictionary that can be the head of a triangle into a new triangledictionary. A word is a triangle head if either its length is the minimum length allowedor it has a parent triangle word. A parent is a word that is one letter shorter than theoriginal word and is an anagram of a subword of the original word. That is, it can beobtained from the original word by deleting one letter and rearranging the remainingletters. For each word copied, set a link to its first parent. (There can be several.)5. Print each triangle head of maximum length followed by successively shorter triangleheads comprising the triangle.3 AssignmentThis assignment has two parts. The first part is to explore the given code and answerquestions about it. The second part is to modify the code in a couple of directions.3.1 Code Exploration1. Draw a UML diagram of the class structure of this program.2. Find each friend declaration. For each, find the lines that cause privacy violationsif the declaration is removed.3. Class Word contains a declaration operator<=(const Word& w2). It is used in theline (*p <= word) in WordList::findParent(). What is the type of *p? What isthe type of word? Explain why the aforementioned operator extension is used eventhough the argument types are not the same as the corresponding declared parametertypes.4. Class WordEntry contains a set-function. Where is it needed? What are the prosand cons of using a set-function rather than making the parent data member public?What are the pros and cons of instead adding another friend declaration?5. In Dictionary::readDict(), there is a call add(cword). Why does this work eventhough Dictionary::add() has only been defined for parameter type const Word&?6. Triangle::build() contains the statement: add(*p)->setParent(parent). Ex-plain in detail how this works. For each operator or function in the expression, saywhat the types of the parameters and results are, which methods are actually called,and why.7. main() initializes static variables declared in word.hpp. Which principle of OO-programming does this violate? What would be a better way to handle this initial-ization? Write the code to do it.Iterators Class WordList contains an embedded class iterator. Iterators are general-izations of pointers. The intention is that an iterator can be used to iterate over a lineardata structure in the same way that a pointer can be used to scan an array. For example, ifint a[100]; is an array of integers, then the following loop can be used to print the array:Handout #7—October 25, 2010 3int* p;int* begin=&a[0];int* end=&a[100];for (p=begin; p!=end; p++) cout << *p;We use iterators to scan over a WordList. See WordList::print() for an example of theiruse, and compare it with the above pointer code.8. Explain what each of the operator definitions in class iterator does.9. What happens if you comment out the definition for WordEntry& operator*()const? Explain.10. What happens if you comment out the definition for WordEntry* operator->()const? Explain.3.2 Extensions: Count TrianglesA word may, in general, have several parents. If it does, it may head several word triangles.Since each parent may itself have several parents, the number of triangles for a given wordis potentially quite large.The first extension is to modify the given code to count the number of word trianglesfor each word. It should appear to the user just like the present code, except that insteadof printing out each head of a word triangle followed by the triangle, it will print out justthe head word followed by a count of the number of triangles that it heads.3.3 Extension: Find All TrianglesThe second extension is to modify the given code not only to count but also to print outall triangles headed by a given word. This will require that you modify the data structuresused to keep track of the words in a word triangle. You might find it useful to define one ormore new classes for this purpose. You will be graded not only on the correctness of yourcode but also on its efficiency and how closely it follows object-oriented design principles.You should document your code with a UML diagram as well as with a report describingyour data structures and why you believe your design follows good OO practices.4 DeliverablesYou should submit this assignment using the submit script that you will find in/c/cs427/bin/ on the Zoo. The three parts should be submitted separately using the-V option.1. To submit the answers to the code exploration questions, use/c/cs427/bin/submit -V1 4 files...2. To submit the count-triangles extension, use/c/cs427/bin/submit -V2 4 files...4 Problem Set 43. To submit the find-all-triangles extension, use/c/cs427/bin/submit -V3 4 files...Remember to include the following items with each code submission:1. Source code and header files.2. A makefile and any other files necessary to build your project. (If you’re usingEclipse with the


View Full Document

Yale CPSC 427 - Problem Set 4

Download Problem Set 4
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 Problem Set 4 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 Problem Set 4 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?