Exercises in this lecture   previous -- Keyboard shortcut: 'p'        Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 8.3
Color Point Extension ***


On this page we have introduced the class ColorPoint, which inherits from Color .

In the sample dialogue with a couple of color points we have identified the problem that the sum of two color points is not a color point. Why is it so?

You are now supposed to make a few changes in the classes Point and ColorPoint. In order to make it realistic for you to play with the classes, your starting point is supposed to be a pre-existing file, with all the useful stuff (available from the on-line version of this material).

When you experiment with points and color-points, use M-x run-laml-interactively from Emacs.

  1. First take a look at the existing stuff, and make sure you understand it. Be aware that both of the classes Point and ColorPointuse virtual methods, as explained below.

  2. Add a method class-of to both Point and ColorPoint that returns the class of an instance. Underlying, the method class-of is supposed to return a function.

  3. Now repair the method add in Point, such that it always instantiate a class corresponding to the class of the receiver. In other words, if the receiver of add is a Point, instantiate a Point. If the receiver of add is a ColorPoint, instantiate a ColorPoint. You can probably use the method class-of from above. (If you run into a problem of a missing parameter in the instantiation of the 'sum point' - you are invited to take a look at my solution).


Solution