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.
Labwork
finish this during section
Template
Type the following command at the terminal to copy the template
file to the current directory:
cp ~cs61as/autograder/templates/hw7.scm .
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))
brian
> (ask brian 'repeat)
()
> (ask brian 'say '(hello))
(hello)
> (ask brian 'repeat)
(hello)
> (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))
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.
Homework
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 prints a Not enough money or Machine empty error
message or returns the amount of change you get. These error
messages should be printed using display
, for
example, (display "Machine empty")
(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)
NOT ENOUGH MONEY
(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)
BRIAN MOVED FROM BH-OFFICE TO SODA
> (define fussy-BH (instantiate miss-manners BH))
> (ask fussy-BH 'go 'east)
ERROR: NO METHOD GO
> (ask fussy-BH 'please 'go 'east)
BRIAN MOVED FROM SODA TO PSL
Exercise 7.
Do the following reading: