proj3b - CS 61A Project 3 solutions(Part II A6 First we...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 CS 61A Project 3 solutions (Part II) A6. First we define the FOOD class: (define-class (food name calories) (parent (thing name)) (initialize (ask self 'put 'edible? #t))) (define-class (food name calories) (parent (thing name)) (method (edible?) #t)) (define-class (bagel) (parent (food 'bagel 300)) (class-vars (name 'bagel))) (define (edible? thing) (ask thing 'edible?)) (method (eat) (for-each (lambda (food) (ask self 'put 'strength (+ (ask self 'strength) (ask food 'calories) )) (ask self 'lose food) (ask place 'gone food)) (filter edible? possessions))) A7a. Give people money. (define-class (person name place) (instance-vars (possessions '()) (saying "") (MONEY 100)) ... (METHOD (GET-MONEY AMT) (SET! MONEY (+ MONEY AMT)) ) (METHOD (PAY-MONEY AMT) (COND ((>= MONEY AMT) (SET! MONEY (- MONEY AMT)) #T) (ELSE #F) )) ...)
Background image of page 1

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

View Full Document Right Arrow Icon
Sheet1 Page 2 A7b. Create restaurant class. This is all new so I won't bother with capital letters! (define-class (restaurant name food-class food-price) (parent (place name)) (method (menu) (list (ask food-class 'name) food-price)) (method (sell buyer order) (if (eq? order (ask food-class 'name)) (if (ask buyer 'pay-money food-price) (let ((food (instantiate food-class))) (ask self 'appear food) food) #f) #f)) ) A8. Modification to PERSON class: (define-class (person name place) ... (method (buy food-name) (let ((food (ask place 'sell self food-name))) (if food (begin (set! possessions (cons food possessions)) (ask food 'change-possessor self)) (error "could not buy" food-name) ))) ...) B6a. First the easy parts: (define jail (instantiate place 'sing-sing)) (method (go-directly-to new-place)
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.

{[ snackBarMessage ]}

Page1 / 20

proj3b - CS 61A Project 3 solutions(Part II A6 First we...

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