6.001 Recitation # 13 – March
21, 2003
RI: Konrad
Tollmar
•Generic OOP
•Parsing object messages
1. A Employee Class
Create a OO model of people employed in a company
(employers). In this company have different employers different roles such as;
sales personnel, marketing personnel, and administrative personnel. Each role
has some unique properties but they also share some properties, e.g., name of
the person. Create appropriate classes and use inheritance and aggregates to
make the right associations between these classes.
(define (get-method message object)
(object message))
(define method? Procedure?)
(define (ask object message . args)
(let ((method (get-method
message object )))
(if (method? method)
(apply method object args) ;object becomes self
(error "No method for message" message))))
Make a Scheme implementation of the classes Employee
(define eecs-emp (make-emp 'konrad))
(ask eecs-emp 'getName)
;Value: konrad
(ask eecs-emp 'setRole '6001-recitator)
;Value: #f
(ask eecs-emp 'getRole)
konrad is our:
6001-recitator
;Value: 6001-recitator
3. Object Oriented Stacks
Using this object oriented style, write the function create-stack that will create a stack object. Recall that stacks are data structures that include the operations push, pop, peek, and clear. Objects get pushed on and popped off the stack in a last-in-first-out manner. Complete the function create-stack.
(define s (create-stack)
(ask s ’push 5)
(ask s ’push 3)
(ask s ’pop) ==>
3
(ask s ’push 1)
(ask s ’pop) ==>
1
(ask s ’pop) ==>
5
4. Object Oriented Variables
Next, let's write an abstraction for variables in the object oriented style. We do this so that in addition to the
get (lookup) and set! operations that we have in scheme, we also want to implement an undo! method that un-does the last set!ing of the variable. We want to store an arbitrary number of undos.
Hint: How can we use stacks to help us with this
undo!?
(define a (create-var 1)
(ask a ’get) ==>
1
(ask a ’set! 2)
(ask a ’get) ==>
2
(ask a ’undo)
(ask a ’get) ==> 1