QUIZ #2 SOLUTIONS 1. post-fix calculator class Here is the answer I was expecting: (define-class (PFCalc) (instance-vars (st (instantiate stack)) ) (method (push item) (ask st 'push item) ) (method (add) (let ((result (+ (ask st 'pop) ;; could we do this if + (ask st 'pop)) ) ) ;; weren't commutative? (ask st 'push result) result)) ) Note that the Stack class should NOT be a parent of PFCalc! We use inheritance when two classes have an IS-A relationship. In this case, however, a calculator is NOT a type of stack. (A stack is like a pile of plates or a pile of papers; a calculator is not.) Instead, the calculator HAS-A stack. (This was stated in the question.) Another clue that there is no inheritance here is that PFCalc doesn't accept methods like "pop" or "empty?", whereas Stack does. When we use inheritance, a child class inherits ALL of the parent classes' methods and variables. So when we say a child class is-a type of a parent class, the child can do EVERYTHING the parent can do. If this isn't clear, let me know. A number of people used an instance-var to store the result of the addition. This works, but it's not good style. Temporary variables should be temporary. You shouldn't be able to access them when they're no longer necessary. In general, use LET for temporary variables unless you have a really good reason not to. 2. inheritance and variables A bunch of these were from old exams. (Probably Professor Harvey's, I'm not sure.) For inheritance, again, we look for IS-A relationships. Is class A a type of class B? Is class B a type of class A? The only two cases that have an is-a relationship are: A drummer is a type of musician. A freeway is a type of road. All of the other cases have HAS-A relationships: A professor has teaching assistants. A computer has a keyboard. A web server has web pages. An automobile has wheels. For the instance versus class variable part: In a file cabinet class, the number of files in the file cabinet should be an INSTANCE variable. If we have multiple file cabinet objects, they can each have a different number of files inside of them. In an AC Transit local bus class, the price of a bus ticket should be a CLASS variable. Each bus charges the same amount for a ticket. If the fares were to change, ALL buses should be affected.