Classes and objects in DrScheme

Course links

Exercise 1

Create an object of the tally% class and send it some messages. Confirm that it works like the tallies in the lab on object-oriented programming.

Exercise 2

Create a queue, enqueue the symbols 'foo, 'bar, and 'baz, dequeue the front element, and check the size of the queue.

Exercise 3

Add a :print message to the queue class.

Exercise 4

Translate our implementation of stacks (from the reading on stacks) into a DrScheme class definition.

Exercise 5

Define a traffic-light% class that is always in one of four states, represented by the symbols 'red, 'yellow, 'green, and 'flashing-red. A traffic light can respond to four messages:

Give the class a initialization parameter specifying its initial state.

Exercise 6

From the tally% class defined in the reading, derive a clicker% class, containing objects that receive the same messages as tally% objects, and in addition a message called :advance!, which takes one argument, a natural number, and increases the internal counter by the specified amount:

> (define my-clicker (make-object clicker%))
> (send my-clicker :show-contents)
0
> (send my-clicker :bump!)
> (send my-clicker :bump!)
> (send my-clicker :show-contents)
2
> (send my-clicker :advance! 7)
> (send my-clicker :show-contents)
9
> (send my-clicker :set-contents-to-zero!)
> (send my-clicker :show-contents)
0
> (send my-clicker :advance! 5)
> (send my-clicker :bump!)
> (send my-clicker :advance! 78)
> (send my-clicker :show-contents)
84