Lab 7:

Object Oriented Programming

One of the main advantages of using generic operators is that new modules can be designed and added to pre-existing modules without modifying the pre-existing code. Object oriented programming is another technique that has this advantage. This is the second major programming paradigm that we are studying, after functional programming.

The Big Idea of object oriented programming is to have data that knows how to perform computations on itself. For example, a number could be represented as an object that knows how to be added to, subtracted from, multiplied with, or divided by another number. This allows programmers to build modules independently - to create a new data representation, a programmer creates a class, which is like a blueprint for objects, that specifies the data to be stored in an object of that class, and what computations can be performed on such objects. The three main ideas that make object-oriented programming possible are message passing, local state and inheritance.

Note: To use the OOP language you must first (load "~cs61as/lib/obj.scm") before using define-class, etc.


finish this during section

Exercise 1.

Modify the person class given in the lecture notes for week 7 (it's in the file ~cs61as/lectures/3.0/demo2.scm) to add a repeat method, which repeats the last thing said.

Here's an example of responses to the repeat message.

> (define brian (instantiate person 'brian))
> (ask brian 'repeat)
> (ask brian 'say '(hello))
> (ask brian 'repeat)
> (ask brian 'greet)
(hello my name is brian)
> (ask brian 'repeat)
(hello my name is brian)
> (ask brian 'ask '(close the door))
(would you please close the door)
> (ask brian 'repeat)
(would you please close the door)

Exercise 2.

This exercise introduces you to the usual procedure described on page 9 of "Object-oriented Programming - Above-the-line View". Read about usual there to prepare for lab. Suppose that we want to define a class called double-talker to represent people that always say things twice, for example as in the following dialog.

> (define mike (instantiate double-talker 'mike))
> (ask mike 'say '(hello))
(hello hello)
> (ask mike 'say '(the sky is falling))
(the sky is falling the sky is falling)

Consider the following three definitions for the double-talker class. (They can be found online in the file ~cs61a/lib/double-talker.scm.)

(define-class (double-talker name)
  (parent (person name))
  (method (say stuff) (se (usual 'say stuff) (ask self 'repeat))) )
(define-class (double-talker name)
  (parent (person name))
  (method (say stuff) (se stuff stuff)) )
(define-class (double-talker name)
  (parent (person name))
  (method (say stuff) (usual 'say (se stuff stuff))) )

Determine which of these definitions work as intended. Determine also for which messages the three versions would respond differently.


do this in section if possible; finish the rest at home

Exercise 3.

For a statistical project you need to compute lots of random numbers in various ranges. (Recall that (random 10) returns a random number between 0 and 9.) Also, you need to keep track of how many random numbers are computed in each range. You decide to use object-oriented programming. Objects of the class random-generator will accept two messages. The message number means "give me a random number in your range" while count means "how many number requests have you had?" The class has an instantiation argument that specifies the range of random numbers for this object, so

(define r10 (instantiate random-generator 10))

will create an object such that (ask r10 'number) will return a random number between 0 and 9, while (ask r10 'count) will return the number of random numbers r10 has created.

Exercise 4.

Define the class coke-machine. The instantiation arguments for a coke-machine are the number of Cokes that can fit in the machine and the price (in cents) of a Coke:

(define my-machine (instantiate coke-machine 80 70))

creates a machine that can hold 80 Cokes and will sell them for 70 cents each. The machine is initially empty. Coke-machine objects must accept the following messages:

(ask my-machine 'deposit 25) means deposit 25 cents. You can deposit several coins and the machine should remember the total.

(ask my-machine 'coke) means push the button for a Coke. This either gives a Not enough money or Machine empty error message or returns the amount of change you get.

(ask my-machine 'fill 60) means add 60 Cokes to the machine.

Here's an example:

(ask my-machine 'fill 60)
(ask my-machine 'deposit 25)
(ask my-machine 'coke)
(ask my-machine 'deposit 25) ;; Now there's 50 cents in there.
(ask my-machine 'deposit 25) ;; Now there's 75 cents.
(ask my-machine 'coke)
5 ;; return val is 5 cents change.

You may assume that the machine has an infinite supply of change.

Exercise 5.

We are going to use objects to represent decks of cards. You are given the list ordered-deck containing 52 cards in standard order:

(define ordered-deck '(AH 2H 3H ... QH KH AS 2S ... QC KC))

You are also given a function to shuffle the elements of a list:

(define (shuffle deck)
  (if (null? deck)
      (let ((card (nth (random (length deck)) deck)))
	(cons card (shuffle (remove card deck))) )))

A deck object responds to two messages: deal and empty?. It responds to deal by returning the top card of the deck, after removing that card from the deck; if the deck is empty, it responds to deal by returning (). It responds to empty? by returning #t or #f, according to whether all cards have been dealt.

Write a class definition for deck. When instantiated, a deck object should contain a shuffled deck of 52 cards.

Exercise 6.

We want to promote politeness among our objects. Write a class miss-manners that takes an object as its instantiation argument. The new miss-manners object should accept only one message, namely please. The arguments to the please message should be, first, a message understood by the original object, and second, an argument to that message. (Assume that all messages to the original object require exactly one additional argument.) Here is an example using the person class from the upcoming adventure game project:

> (define BH (instantiate person 'Brian BH-office))

> (ask BH 'go 'down)

> (define fussy-BH (instantiate miss-manners BH))

> (ask fussy-BH 'go 'east)

> (ask fussy-BH 'please 'go 'east)

Exercise 7.

Do the following reading:

Extra for Experts

Do this if you want to. This is NOT for credit.

Exercise 8.

The technique of multiple inheritance is described on pages 9 and 10 of "Object-Oriented Programming - Above-the-line view". That section discusses the problem of resolving ambiguous patterns of inheritance, and mentions in particular that it might be better to choose a m thod inherited directly from a second-choice parent over one inherited from a first-choice grandparent.

Devise an example of such a situation. Describe the inheritance hierarchy of your example, listing the methods that each class provides. Also describe why it would be more appropriate in this example for an object to inherit a given method from its second-choice parent rather than its first-choice grandparent.