CS61A_fa00_mt2_sol - CS 61A Fall 2000 Midterm 2 solutions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 61A Fall 2000 Midterm 2 solutions 1. Skittles in OOP. (a) Since methods to read local state variables are automatically provided, all we need is (define-class (skittle color)) We didn't take off for explicitly providing a COLOR method, unless the method was so convoluted that it actually wouldn't work. But learn to do things simply instead of making your programs unnecessarily complicated. Scoring: One point if correct. (b) There were two basic approaches, one keeping minimal state information and one keeping extra redundant information to avoid writing some methods. I prefer the first approach: (define-class (skittle-bag) (instance-vars (bag '())) (method (add skit) (set! bag (cons skit bag))) (method (count) (length bag)) (method (colors) (map (lambda (s) (ask s 'color)) bag)) (method (remove) (let ((result (car bag))) (set! bag (cdr bag)) result))) Note that the REMOVE method must first remember the to-be-returned skittle, then remove it from the list, and then--last of all--return the result. This can't be done without LET or some equivalent mechanism to hold the removed skittle. (method (remove) ;; wrong!! (car bag) (set! bag (cdr bag))) The second approach remembers the colors and count in state variables as well as the actual skittles: (define-class (skittle-bag) (instance-vars (bag '()) (count 0) (colors '())) (method (add skit) (set! bag (cons skit bag)) (set! count (+ count 1)) (set! colors (cons (ask skit 'color) bag))) (method (remove) (let ((result (car bag))) (set! bag (cdr bag)) (set! count (- count 1)) (set! colors (cdr colors)) result))) This is okay, and got full credit. And it has the virtue that its
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(implicit) COLOR method is Theta(1) time instead of Theta(N) time because it doesn't have to count the length of the BAG list, although there is a slight slowdown of the ADD and REMOVE methods. But it has the disadvantage that keeping redundant information increases the possibility of program bugs. In this simple problem it's unlikely to happen, but what if your ADD or REMOVE method updated some of the three state variables but not others? This could be a difficult bug to find. (But don't make a religion of this; I'd use redundant information if there were a huge difference in efficiency.) We didn't give full credit to solutions that kept *only* a list of colors and not a list of skittle objects. Because skittles have such simple behavior, and no changing local state, such programs could have the correct apparent results, if the REMOVE method instantiates a new skittle of the removed color. (In this simple program, two yellow skittles can't really be distinguished, so making a new one isn't visibly different from returning the same one originally added.) But these solutions violate the spirit of simulations with objects, and we'll see that two same-color skittles actually *can* be distinguished if we try hard enough. Scoring: 3 if correct, 2 for minor errors, 1 for major errors, 0 for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

CS61A_fa00_mt2_sol - CS 61A Fall 2000 Midterm 2 solutions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online