hw7 - CS 61A HOMEWORK: 1. Week 7 solutions random number...

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

View Full Document Right Arrow Icon
CS 61A Week 7 solutions HOMEWORK: 1. random number generator object (define-class (random-generator range) (instance-vars (count 0)) (method (number) (set! count (1+ count)) (random range) )) ; We don't need to say (method (count) . ..) because we automatically get a ; method to examine each instance variable (and instantiate variable as well). 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?)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

hw7 - CS 61A HOMEWORK: 1. Week 7 solutions random number...

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

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