{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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)) )

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

View Full Document
Sheet1 Page 2 4. polite objects (define-class (miss-manners object) (method (please message arg) (ask object message arg)))
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online