CS212 Exams
Spring 1997 - Prelim
2

Object-oriented Programming


Consider the following definitions:

(defclass <thing> ()
  (size :type <number>
        :initarg :size
        :initvalue 100)
  (location :type <symbol>
            :initarg :location
            :initvalue 'nowhere))

(defclass <animal> (<thing>)
  (name :type <top>
        :initarg :name
        :initvalue 'anonymous)
  (hunger :type <number>
          :initarg :hunger
          :initvalue -1))

(defclass <vegetable> (<thing>)
  (nutritional-value :type <number>
                     :initvalue 0))

(defclass <mineral> (<thing>)
  (worth :type <number>
         :initvalue 0))

(defclass <venus-flytrap> (<animal> <vegetable>)
  (favorite-meals :type <list>
                  :initvalue '(flies)))

(defclass <fly> (<animal>))

(define popeye (make <animal> :size 150 :name 'popeye))

(define spinach (make <vegetable> :location 'grocery-store))

(define milo (make <venus-flytrap> :name 'milo))
  1. List the slots of popeye, spinach and milo, together with their values.

    popeye spinach milo



     

  2. Write a generic function edible?, which takes two arguments, an eater and an eaten. The generic function will return #t if the eater can eat the eaten. In general, an arbitrary <thing> cannot be eaten. An <animal> can eat a <vegetable> (but not the reverse). However, a <venus-flytrap> can eat a <fly>.





Solution

Return to CS 212 Prelim 2 - Spring 1997