Objects

Course links

Exercise 1

Define a one-field object tally that responds to exactly three messages: :show-contents and :set-contents-to-zero!, as in zeroable-box, and :bump!, which has the effect of increasing the number stored in the contents field by 1. The initial value of that field should be 0.

> (tally ':show-contents)
0
> (tally ':bump!)
> (tally ':show-contents)
1
> (tally ':bump!)
> (tally ':bump!)
> (tally ':bump!)
> (tally ':show-contents)
4
> (tally ':set-contents-to-zero!)
> (tally ':show-contents)
0
> (tally ':bump!)
> (tally ':bump!)
> (tally ':show-contents)
2

Exercise 2

Define a make-tally procedure that constructs and returns objects similar to the tally object you defined in exercise 1.

Exercise 3

Create two tally objects and demonstrate that they can be incremented and reset independently.

Exercise 4 (optional)

Define a constructor procedure, make-monitored-tally, for objects similar to the tally objects from exercise 2 above, except that each such object keeps track (in a separate field) of the total number of messages that it has received. (The initial value for the new field should be 0, and you should add 1 to this field as a side effect every time the object is invoked.) A monitored-tally object should also respond to a fourth message, :report, by returning the count of messages received.

I am indebted to Ben Gum for his contributions to the development of this lab.