6.001 Recitation 18Structure and Interpretation of Computer Programs April 13, 2005More OOPing1. OOP code templateWith our fu ll-blown obj ect system, here’s what the code for a class looks like:; ; D e f ine t h e c l a s s c o n s t r u c t o r( d e f i n e ( c r e at e− ty p e ar g1 ar g 2 . . . )( c r e a t e − i n s t a n c e t y p e ar g 1 ar g2 . . . ) ); ; D e f i n e t h e c l a s s( d e f i n e ( t yp e s e l f a r g 1 a rg 2 . . . ); ; C a p t u r e i n t e r n a l s t a t e , i n c l u d i n g i n s t a n c e s o f s u p e r c l a s s e s t o; ; d e l e g a t e t o . I f t h e r e a r e no b e t t e r s u p e r c l a s s e s , don ’ t f o r g e t; ; t o i n h e r i t from r o o t − o b j e c t( l e t ( ( s u p er 1− p a r t ( s u p e r 1 s e l f a r g s . . . ) )( su p e r 2 − p ar t ( s u p e r 2 s e l f a r g s . . . ) ); ; O t h e r s u p e r c l a s s e s and l o c a l s t a t e); ; T h i s f u n c t i o n h a s t o r e t u r n t h e h a n d l e r f o r c l a s s o f t y p e , s o; ; c r e a t e t h a t h a n d l e r( make− handler; ; The h a n d l e r n e e d s t o know t h e name o f t h i s o b j e c t ’ s t y p e’ t y p e; ; make− methods b u n d l e s t o g e t h e r t h e m e t h o d s f o r t h e h a n d l e r( make− methods’METHOD1 (lambda ( a r g s . . . ) . . . )’METHOD2 (lambda ( a r g s . . . ) . . . ) ); ; F i n a l l y , t h e h a n d l e r n e e d s t o know w h e r e t o go f o r m e s s a g e s; ; t h a t w e r e n ’ t d e f i n e d h e r esu p e r 1 − p ar tsu p e r 2 − p ar t ) ) ); ; I n s t a n t i a t e t h e c l a s s( d e f i n e t y p e − i n s t a n c e ( cr e at e − ty p e arg 1 ar g 2 . . . ) ); ; Ask t h e i n s t a n c e t o do s o m e t h i n g ( i e , p a s s i t a m es sa ge )( a sk t y p e − i n s t a n c e ’MESSAGE a r g 1 ar g 2 . . . ); ; Or you can g e t t h e met h o d d i r e c t l y as a p r o c e d u r e ( u s e f u l i n; ; c o n j u n c t i o n w i t h h i g h e r o r d e r p r o c e d u r e s , s i n c e t h i s a l l o w s you; ; t o p a s s m e ss a g e h a n d l e r s / m et ho ds a r o u n d l i k e r e g u l a r p r o c e d u r e s )( d e f i n e m ethod− of− typ e − i nstance ( get− method t y p e − i n s t a n c e ’MESSAGE) )( m ethod− of− typ e − i nstance arg 1 a r g2 . . . )2. OOPFirst, draw an object diagram for the two objects described below.Write a food class. Its constructor should take its name, nutrition value, and good−for duration.It should have accessors for its name and age. It should also have a sit−there method thattakes on e argument and causes it to age by that much. Finally, it should have an eat methodthat returns the current nutrition value of the food.16.001 Structure and Interpretation of Computer Programs More OOPingNow write an aged−food class. Its constructor should take an additional argum ent good−afterthat specifies when the f ood is good after. It should have an additional method sniff th atreturns #t if the food has aged long enough. eat should return 0 if the foo d is not good yet,and should otherwise behave like food’s eat m ethod.3. Asking and delegatingSuppose aged−fo od was started with( d e f i n e ( aged− food . . . )( l e t ( food− part ( c rea t e − fo o d . . . ) )What might go wrong? (Your implementation of food probably won’t trigger this problem,but aged−food can’t gaurantee that)4. Brainteaser: Classless object systemsConsider an object system in wh ich everything’s an object (ie, there’s no huge distinctionbetween classes and instances). For example, if we ask an object in our object system for itstype, it’ll return a symbol (well, a list of symbols), …
View Full Document