# More Object-Oriented Programming

## Exercise 0: Preparation

a. Start DrScheme.

b. Make a copy of `statistician.scm`

## Exercises

### Exercise 1: A Simple Statistician

The `(make-statistician)` procedure creates an object that supports two operations, `:observe! val`, and `:average`. The first adds a value to those the statistician has already observed. The second computes the average of all observed values.

a. Create a new statistician named `tom`.

b. Tell `tom` to average the numbers 1, 4, and 9.

### Exercise 2: Adding Statistics

a. Extend `make-statistician` so that it also supports a `:max` operation that reports on the largest value observed so far. In addition to adding that operation, you will also need to add another entry to the vector and update it each time a value is observed.

b. Use the improved version of `make-statistician` to compute the largest of -4, -1, -9.

### Exercise 3: A Variant of `map`

Once we start to use side-effecting procedures like `display` and `observe!`, it becomes less appropriate to use `map` to apply a procedure to each element in a list. Why? Because we don't really care about the result list.

a. Use `map` to display each element in the list `(1 2 3)`, one per line. What do you observe about the result?

b. I've created `apply-to-each` as an alternative. Rewrite the expression for part a to use `apply-to-each` rather than `map`. What do you observe about the result?

### Exercise 4: Repeated Observations

a. Use `apply-to-each` to observe each value in the list `(4 1 2 6 3 1 5)`.

b. Verify that the statistician computes the correct average of those values.

c. Verify that the statistician computes the correct maximum of those values.

### Exercise 5: An Alternate Statistician

The `(make-new-statistician)` procedure also returns a statistician object, albeit a slightly different one. The objects returned by `make-new-statistician` respond to two messages, `:observer!` and `:average`. The first returns a procedure that can be used to observe valeus. The second computes the average of the returned values.

a. Create a new statistician named `heidi` using `make-new-statistician`.

b. Tell Heidi to compute the average of the values 1, 4, and 9.

c. Use `apply-to-each` to tell Heidi to observe each element of the list `(4 1 2 6 3 1 5)`.

d. Which kind of statistician do you prefer? Why?

## Notes

### A Note on Problem 3: Side Effects

A side-effecting procedure is a procedure that is called for its effects, rather than its result. Many such procedures return no value (or, more precisely, the special value `#<void>`).

## History

Thursday, 5 December 2002 [Samuel A. Rebelsky]

Friday, 6 December 2002 [Samuel A. Rebelsky]

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Dec 6 09:30:19 2002.
The source to the document was last modified on Fri Dec 6 09:30:17 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/more-oop.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu