CS212 Exams
Spring 1999 -
Final

Object Oriented Programming


The code below defines a class of containers and contained objects, similar to the ones we provided in problem set 5. In addition, the code defines a generic transfer function for transferring a contained object from one container to another with a default method:

(defclass <container> ()
   (contents :type <list>
             :accessor contents
             :initarg :contents
             :initvalue '()))

(defclass <contained> ()
   (location :type <container>
             :accessor location
             :initarg :location))

(defgeneric (transfer (obj <contained>) (source <container>)
                                        (target <container>)))

(defmethod (transfer (obj <contained>) (source <container>)
                                       (target <container>))
   (unless (eq? source target)
      (set! (contents source) (remq obj (contents source)))
      (set! (location obj) target)
      (set! (contents target) (cons obj (contents target)))))

Define a new class <counted-container> that keeps track of how many contained objects are in the container. You will need to define several new methods for transfer to keep the count accurate. Your code should be as small and simple as possible.


Solution

Return to CS 212 Final - Spring 1999