DOC PREVIEW
MIT 6 001 - Object-Oriented Programming

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

STUDENT NAME: 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.001—Structure and Interpretation of Computer ProgramsFall 2004Recitation 19Object-Oriented ProgrammingScheme1. Syntax(a) . args - In order to implement variable-number-of-arguments procedures (like +,list,append,or map). End the p arameter list of a lambda with . args. The variable args will bebound to a list of all the remaining arguments.((lambda (x . args) (append x y)) ’(1 2) 3 4 5)(define (do-stuff x y . rest) ...)(define (add . args) (fold-right + 0 args))((lambda args ( cons ’yay args)) 3 4)2. Procedures(a) (apply proc args)Applies proc to args. It’s like having written (proc arg0 arg1 arg2 ...).Object System(define (make-type self arg1 arg2 ... a rgn)(let ((super1 -part (make-super1 self args))(super2-part (make-super2 self args))other superclassesother local state)(make-handler’type(make-methods’METHOD-NAMEmethod-lambda...)super1-part super2-part ...)))(define (create-type arg1 arg2 ... argn)(create-instance make-type arg1 arg2 ... argn))STUDENT NAME: 2Object ProceduresThese are defined in objsys.scm.1. ask - (ask obj msg [args...])Calls the method msg on the object obj with the (optional) extra arguments. It may be usedwith either an instance or a h an dler for the obj.2. make-handler - (make-ha ndler type method-list [parts ...])Make a handler (a message-passing procedure) that hand les an object of the given type,methods, and parents.3. make-methods - (make-me thods name method name method ...)Builds an association-list out of the method names and l ambdas. Used to build the method-list for make-handler.Conventions1. All objects follow the above obj ect skeleton. It’s a make-class procedure that produces ahandler for the particular class.2. Every class must inherit from some other object (have at least one superpart). If the classdoesn’t have an obvious superclass, it should probab ly inherit from the ro ot-object.3. Use ask to call methods on an object.4. When calling other methods on the same object (or it’s superclasses), you should(ask self ...)5. The exception to the above rule is w hen the call is in method M and is calling method M onthe superclass:((M)(lambda ()...(ask super-pa rt ’M)...))By rule 4, using the required (ask self ’M) instead of (ask super-part ’M), will infiniteloop.STUDENT NAME: 3Problems1. Write a food class• Input state is the name,nutriti on value, and good-until time.• Additional state is the ag e of the food, initially 0.• Methods are:– NAME - returns the name of the food– NUTRITION - returns the nutrition of the food– AGE - return s the age of the food– SIT-THERE - takes an amount of time, and increases the age of the food by theamount.– EAT - return the nutrition if the food is still good; 0 otherwise.STUDENT NAME: 42. Write an aged-fo od class• Input state is the same as the food class, with an additional parameter, which is thegood-after time.• Should inherit from the foo d class.• Methods are:– SNIFF - r etur ns #t if it has aged en ough to be good.– EAT - return s 0 if the food is not good yet; otherwise behaves like normal food.3. Write a decay ing-food class• Input state is the same as the aged-food class.• Should inherit from the age d-food class.• Methods are:– NUTRITION - nutrition is inversely proportional to age, after the good-after time.STUDENT NAME: 54. Write a vendi ng-machine class• Input state is the same as the food class.• Additional state is age of the vending-mach ine, initially 0.• Methods are:– SIT-THERE - takes an amount of time, and increases the age of the vending-machineby half that amount (it’s refridgerated!).– SELL-FOOD - returns a new food instance with the appropriate name, nutrition andgo


View Full Document

MIT 6 001 - Object-Oriented Programming

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Object-Oriented Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?