Lecture 7: In Class Activity

Solution Key


Problem 1

(defclass Boat1 () (position heading speed))


Problem 2

(defclass Boat () ((position :initform nil
                             :initarg :pos
                             :accessor boat-pos
                   )
                   (heading :initform nil
                            :initarg :heading
                            :reader boat-heading
                   )
                   (speed :initform 0
                          :initarg :speed
                          :accessor boat-speed
                   )
                   (windspeed :initarg :windspeed
                              :accessor boat-windspeed
                              :allocation :class
                   )
                  )
)


Problem 3

(defmethod change-heading (c new-heading)
 (format t "You can't change the heading of this!")
)

(defmethod change-heading ((c Boat) new-heading)
 (cond
  ((numberp new-heading)
   (setf (slot-value c 'windspeed) (mod new-heading 360))
  )
  ((equalp new-heading 'Dry-Dock)
   (setf (slot-value c 'windspeed) nil)
  )
  (t (call-next-method))
 )
)


Problem 4

(defclass aircraft-carrier (Boat)
 ((num-planes :initarg :num-planes
              :initform 16
              :reader Boat-planes
 ))
)

(defmethod launch ((b aircraft-carrier) on-orders-from)
 (if (> (Boat-planes b) 0)
  (setf (slot-value b 'num-planes) (- (Boat-planes b) 1))
 )
)

(defmethod land ((b aircraft-carrier))
 (setf (slot-value b 'num-planes) (+ (Boat-planes b) 1))
)


Problem 5

(defclass military-aircraft-carrier (aircraft-carrier)())

(defmethod launch ((b military-aircraft-carrier) on-orders-from)
 (if (equalp on-orders-from 'Admiral)
  (call-next-method)
 )
)

(defmethod launch :before ((b aircraft-carrier) on-orders-from)
 (do ()
     ((>= (Boat-planes b) 5))
     (land b)
 )
)

(defmethod launch :after ((b aircraft-carrier) on-orders-from)
 (format t "~%Planes launched, sir.~%")
)

Back to main page...