DOC PREVIEW
UW CSE 341 - Study Guide

This preview shows page 1 out of 2 pages.

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

Unformatted text preview:

CSE 341, Fall 2004, Assignment 6 (version 1)Due: Tuesday 30 November, 9:00AMOverview: You will define 4 Smalltalk classes (all subclasses of Object) and add methods to them. Putyour classes in a category lastname_hw6 where lastname is your last name. Do not change any code inother categories.1. Define a class Leaf with an instance variable str, which for now we assume will hold a String. Definea setter method string:, but do not provide a getter method.2. Define a class BinaryNode with instance variables left and right, which for now we assume will holdeither a Leaf or a BinaryNode. Define a setter method left:right:, but not getter methods.3. Add concatAll methods to Leaf and BinaryNode. The return value should be a String which is allthe strings in the tree concatenated together left-to-right. Do not worry about efficiency. Smalltalkalready has a method for concatenating two strings.4. Add a class method to BinaryNode called firstAlphabetical:and:. This method takes two stringsand returns the one that comes first alphab etically. The case of letters does not matter; so BinaryNodefirstAlphabetical: ’Hi’ and: ’hI’ can return either argument and be correct. Do not useexisting String methods for comparing strings. More specifically send only the messages size andat: to the arguments. You can send other messages to characters in the arguments. (Use a “whileloop”.) Sample solution 10 lines.5. Add firstAlphabetical methods to Leaf and BinaryNode. The return value should be the Stringthat is the first string alphabetically in the tree. Use your solution to the previous problem.6. Define a class NaryNode with an instance variable arr, which for now we assume will hold an arrayof strings. Define a setter method array:, but do not provide a getter method. Define methodsconcatAll and firstAlphabetical as in previous problems. For a size-zero array, concatAll shouldreturn ’’ and firstAlphabetical should send the error message ’firstAlphabetical on emptyNaryNode’.7. Add iterate:onEmpty: methods to Leaf and BinaryNode. (Note NaryNode is a little trickier, so it isEC1.) The first argument is a block taking one argument and the second argument is a block takingzero arguments. The return value is an iterator implemented as follows:• It is an array with two elements.• The first element is the result of sending value: str to the first argument of iterate:onEmpty:where str is a String in the tree.• The second element is a block taking no arguments. If there are no more strings in the tree,this block is the second argument to iterate:onEmpty:. Else it is a block that evaluates to aniterator for the remaining strings in the tree.Also, the iterator should work “left to right” so the first element in the iterator is the first block appliedto the leftmost string in the tree, and so on. Hint: We did something similar in class in Scheme, butin class the interior nodes of the tree also had elements on them.8. Add a class method to BinaryNode called concatAll:after:. The result of BinaryNode concatAll:t after: s where t is a tree and s is a string is a string that is s followed by all the strings in tconcatenated together. However, the only message you may send to t is iterate:onEmpty:. Hint:Make the second argument to iterate:onEmpty: be [{}]. That way you can test if the iterator hasmore strings by seeing if the size of the array is 0 or 2. Sample solution: 6 lines.19. Define a class FunnyNumber with an instance field num (for holding a number), a getter method (num),and a setter method (num:). Define one other method (quite possibly an infix method) that takesanother FunnyNumber and returns a new FunnyNumber holding the sum of self’s num and the argument’snum. This other method should have a name such that you can build a tree t holding FunnyNumberobjects (instead of strings) and (BinaryNode concatAll: t after: 0) returns the sum of the tree’selements.Examples: The file hw6.text on the website has a couple examples that should help you understand themethods you need to write.Extra Credit:EC 1 Add an appropriate iterate:onEmpty: method to NaryNode.Hint: Use a helper method iterate:from:onEmpty:.EC 2 Add a class method concatAll2:after: to BinaryNode that is just like concatAll:after: exceptexecuting BinaryNode concatAll2: t after: s creates only one String object and no other col-lections, arrays, trees, lists, etc.Hint: Iterate through t twice.Turn-in Instructions• “File out” the category you created and name the resulting file lastname_hw6.st.• Email your solution to [email protected].• The subject of your email should be exactly [cse341-hw6].• Your .st file should be an


View Full Document

UW CSE 341 - Study Guide

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Study Guide
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 Study Guide 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 Study Guide 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?