hw8 - CS 61A Week 8 solutions HOMEWORK: 1. random number...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 CS 61AWeek 8 solutions HOMEWORK: 1. random number generator object (define-class (random-generator range) (instance-vars (count 0)) (method (number) (set! count (1+ count)) (random range) )) 2. Coke machine. (define-class (coke-machine size price) (instance-vars (cokes 0) (money 0)) (method (deposit coin) (set! money (+ money coin)) ) (method (coke) (cond ((= cokes 0) (error "Machine empty")) ((< money price) (error "Not enough money")) (else (let ((change (- money price))) (set! money 0) (set! cokes (-1+ cokes)) change )) )) (method (fill number) (let ((new (+ cokes number))) (set! cokes (if (> new size) size new))))) 3. Deck of cards (define-class (deck) (instance-vars (the-deck (shuffle ordered-deck))) (method (deal) (if (null? the-deck) '() (let ((top-card (car the-deck))) (set! the-deck (cdr the-deck)) top-card))) (method (empty?) (null? the-deck)) )
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sheet1 Page 2 4. polite objects (define-class (miss-manners object) (method (please message arg) (ask object message arg)))
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.

This note was uploaded on 09/14/2009 for the course PEIS 100 taught by Professor Mckenzie during the Spring '08 term at University of California, Berkeley.

Page1 / 6

hw8 - CS 61A Week 8 solutions HOMEWORK: 1. random number...

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