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

Info icon This 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) )) ...)
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) (announce-move name place new-place) (for-each (lambda (p) (ask place 'gone p)
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern