CS 342 Additional Homework for Chapter 7 Due: April 10, 1992 General directions: be sure we have given you some specific help (either in recitation or personally) with each problem before solving these additional problems. 1. (to make up for problem 1 on page 344) Define a class "Counter" that has the following protocol. * initCounter sets the counter to 0 * incr sets the counter to its current value plus one, and returns the new value. * decr, sets the counter to its currrent value minus one, and returns the new value. * display, prints the counter's value Your code should be able to handle test cases such as the following: -> (set myCounter (mkCounter)) -> (display myCounter) 0 -> (incr myCounter) 1 -> (set myc2 (mkCounter)) (display myc2) 0 -> (decr myc2) -1 2. (to make up for reverse) a. Suppose we execute the following, after loading in the smalltalk library, -> (set lst (mkList)) -> (add: lst 1) -> (add: lst 2) What is printed by the following? What does each result mean to you? (i) -> (isEmpty lst) (ii) -> (set n (mkList)) (iii) -> (first lst) (iv) -> (add: n (first lst)) (v) -> (next lst) (vi) -> (car #nil) b. Define a global function (not in any class), (subst gnu old ls), that builds a list obtained by replacing each (top-level) occurrence of the item old in the list ls with the item gnu. You may assume that an ``item'' is either a number or a symbol. For example, supposing that we have -> (printColl abaca) a b a c a 0 so that the list abaca contains first the symbol a, then b, then a, then c, and then a, and supposing that we have -> (printColl lst2323) 2 3 2 3 0 -> (printColl empty) 0 then your program should work as follows -> (printColl (subst #z #a abaca)) z b z c z 0 -> (printColl (subst 3 2 lst2323)) 3 3 3 3 0 -> (printColl #dog #cat empty) 0 3. (To make up for the interval problem.) Do problem 3 on page 345. 4. (To make up for the bags problem.) a. For this problem you will use an extension to the smalltalk interpreter, called smalltalk-extended-with-load. It has two new primitive operations: printChar and printNoNewline. When using it you can do things like the following: -> (begin (printChar 33) (printChar 34) (print 0)) !"0 0 -> (begin (printNoNewline #hi) (printChar 32) (print #there) 0) hi there 0 -> (printChar #hi) ; an error Can only print an integer as a character That is, printChar prints the character whose ASCII code is its argument. And PrintNoNewline prints its argument in the same way that print does, but without a newline folllowing it. To use printChar you have to know the ASCII character codes in decimal, (e.g., 32=space). The ASCII codes in ocatal and hexadecimal are found in /usr/pub/ascii; see the staff if you don't know how to translate those into decimal. Source for this version of the interpreter is in /home/cs342/public/src/chap.7/smalltalk-extended-with-load.p Your task is to make a copy of the smalltalk library collection classes, taking the code from /home/cs342/public/lib/libraray.smt; and to add the minimum amount of code needed to allow colections to print themselves out sensibly. For example: -> (set lst (mkList)) -> (add: lst 1) -> (add: lst 2) -> (print lst) (2 1) ; or some other value printed here that we don't care about Note that the "(2 1)" is printed under control of the print method of List or some superclass of List; the is printed by the read-eval-print loop for the value returned by the print method. You should return the object itself as the result of the print method, and that you consider it's type to be T -> Void for each type of collection, T. Here are some more examples, continuing from the above. -> (set s (mkSet)) -> (add: s 5) -> (add: s 6) -> (print s) (6 5) -> (add: lst s) -> (print lst) ((6 5) 2 1) -> (set a (mkArray 1 10)) -> (print a) (0 0 0 0 0 0 0 0 0 0) -> (add: lst a) -> (print lst) ((0 0 0 0 0 0 0 0 0 0) (6 5) 2 1) Remember that you'll have to use smalltalk-extended-with-load to do this problem. b. (extra credit only) Modify the smalltalk interepreter's read-eval print loop so that after evaluating an expression and getting an object, it should send the object the message "print", and otherwise use the built-in method. (Hint, bind the object to a variable, say "it" and evaluate the expression (print it) afterwards.) c. (extra credit only) Try to get sets to print as {6 5} arrays to print as [1: 0 0 3 4] where 1 is the low bound, and the elements follow, and dictionaries to print as {key |-> value, 3 |-> 5} Do this with the minimum amount of code and code changes.