DOC PREVIEW
MIT 6 001 - Object-Oriented Systems

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 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 12 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 12 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 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

13 April 2005By Gerald Dalley6.001 Recitation: Object-Oriented SystemsUML(-ish) Diagram+INSTALL()+LOCATION() : PLACE+DESTROY()+EMIT(in text)-self : ?-name : SYMBOL-location : PLACE-named-part : ?thing+LOCATION() : place+CHANGE-LOCATION(in new-location : PLACE)+ENTER-ROOM()+LEAVE-ROOM()+CREATION-SITE() : place-self : ?-name : SYMBOL-location : PLACE-thing-part : ?mobile-thing+INCANT()+ACTION: nil -> unspecified()+USE(in caster:PERSON, in target:THING)-self : ?-name : SYMBOL-location : PLACE-incant-action-mobile-part : ?spellAbstract View of ObjectsQ: What is the type of self?A: an instanceQ: What is the type of named-part? thing-part? mobile-part?A: They are all message handler proceduresQ: What happens when we:((((askaskaskask mymymymy----spell spell spell spell ‘‘‘‘CHANGECHANGECHANGECHANGE----LOCATIONLOCATIONLOCATIONLOCATION somesomesomesome----location)location)location)location)A: 1) we look in spell for CHANGE-LOCATION2) we look in mobile-thing for CHANGE-LOCATION3) We retrieve mobile-thing’s locationQ: When is spell’s location variable used?A: only in spell’s maker (when it’s passed to the mobile-thing maker)Anatomy of a Class;;;; spell;;(define (create-spell name location incant action)(create-instance spell name location incant action))(define (spell selfselfselfself name location incant action)(let ((mobile-part (mobile-thing self name location)))(make-handler'spell(make-methods'INCANT(lambda () incant)'ACTION(lambda () action)'USE(lambda (caster target) (action caster target)))mobile-part)))ConstructorMakerMake super parts & internal stateMethod definitionsLink up super partsMake handlerNote: chest anatomy image borrowed from http://www.usctransplant.org/lung/aboutourprogram.htmlPractice: Diagram Given Code(define (create-place name) ; symbol -> place(create-instance place name))(define (place self name)(let ((named-part (named-object self name))(container-part (container self))(exits '()))(make-handler'place(make-methods'EXITS (lambda () exits)'EXIT-TOWARDS(lambda (direction) (find-exit-in-direction exits direction))'ADD-EXIT(lambda (exit)(let ((direction (ask exit 'DIRECTION)))(if (ask self 'EXIT-TOWARDS direction)(error (list name "already has exit" direction))(set! exits (cons exit exits)))'DONE)))container-part named-part)))Practice: Code Given Diagram• Sketch out the constructor and maker code• For methods, just write the shell of it, e.g. for DO-FOO(victim : PERSON)put the following in the make-methods portion:‘DO-FOO (lambda (victim) ...)(define (create-hippogryph name birthplace activity friendliness)(create-instance hippogryph name birthplace activity friendliness))(define (hippogryph self name birthplace activity friendliness)(let* ((person-part (person self name birthplace))...(mount (create-place ...)))(make-handler'hippogryph(make-methods 'INSTALL (lambda () ...)'MOVE-ABOUT (lambda () ...)'MOVE-SOMEWHERE (lambda () ...)'EAT-SOMEONE (lambda () ...)'PICKUP-RIDER (lambda () ...)'DIE (lambda (perp) ...))person-part)))Practice: Code Given DiagramDesign & Implementation Practice• We are going to implement a few new classes in the project 4 world. As you go, ask yourself these questions:– What should class or classes should it inherit from? – What new fields does it need? – What new methods does it need? – What methods from its superclass(es) should it override? Should the behavior of its overridden methods completely replace what the superclass does, or just augment it? How should superclass methods be called from an overriding method? Hints are provided so you don't have to hunt around too much.• A grumpy troll attacks someone whenever it has a fit. (Hint: a person can HAVE-FIT, a troll can EAT-PEOPLE)• A bomb, when triggered, destroys everything around it. (Hint: a thing has a LOCATION, which is a container with a list of THINGS.)• A recorder remembers everything it ever said and can replay it all on command. (Hint: a person can SAY something.)• An enchanted person is a person that wanders around and IS-A spell. Whenever the enchanted person has a fit, it should cast itself on everyone else in the room. (Hint: think about multiple-inheritance issues)Note: grumpy-troll, bomb, and recorder were adapted from Rob Miller’s “Inheritance and Delegation” recitation http://people.csail.mit.edu/people/rcm/6001/grumpy-troll• A grumpy troll eats someone whenever it has a fit. (Hint: a person can HAVE-FIT, a troll can EAT-PEOPLE)(define (grumpy-troll self name birthplace speed hunger)(let ((troll-part (troll self name birthplace speed hunger)))(make-handler‘grumpy-troll(make-methods‘HAVE-FIT (lambda ()(ask trolltrolltrolltroll----part part part part 'HAVE-FIT)(ask self 'EAT-PEOPLE)))troll-part))) • Why did we have to use (ask trolltrolltrolltroll----partpartpartpart 'HAVE-FIT), instead of just (ask selfselfselfself 'HAVE-FIT)? Note: troll image borrowed from http://www.nofrag.com/2003/aou/18/7138/bomb• A bomb, when triggered, destroys everything around it. (Hint: a thing has a LOCATION, which is a container with a list of THINGS.)(define (bomb self name birthplace)(let ((mobile-thing-part (mobile-thingself name birthplace)))(make-handler‘bomb(make-methods‘EXPLODE(lambda ()(for-each (lambda (thing)(if (ask thing 'IS-A 'person) (ask thing 'DIE self)(ask thing 'DESTROY)))(ask (ask self 'LOCATION) 'THINGS))))mobile-thing-part))) • In the for-each expression, why didn't we just write (ask self 'THINGS) to find out what things are around us, since we already have a local variable with our location in it?recorder• A recorder remembers everything it ever said and can replay it all on command. (Hint: a person can SAY something.)(define (recorder self name birthplace)(let ((person-part (person self name birthplace))(recording ‘()))(make-handler‘recorder(make-methods‘SAY (lambda (list-of-stuff)(set! recording (cons list-of-stuff recording))(ask person-part 'SAY list-of-stuff))‘REPLAY(lambda ()(ask person-part 'SAY (reverse recording))))person-part))) • Why did we have to use (reverse recording)?• Why did we have to use (ask person-part 'SAY ...), instead of just (askself 'SAY ...) in REPLAY?enchanted-person• An enchanted person is a person that automatically wanders around and IS-A spell. Whenever the enchanted person has a fit, it should cast itself on everyone else in the room. (Hint: think about multiple-inheritance issues)(define (enchanted-person self name birthplace activity miserly incant


View Full Document

MIT 6 001 - Object-Oriented Systems

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 Systems
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 Systems 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 Systems 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?