Exercise index of this lecture   Alphabetic index   Course home   

Exercises and solutions
Object-oriented programming in Scheme


8.1   Points and Rectangle *** 

The purpose of this exercise is to strengthen your understanding of functions used as classes in Scheme.

First, play with the existing Point class defined on this page available from the on-line version of this material.

As an example, construct two points and add them together. Also, construct two lists of each four points, and add them together pair by pair.

Define a new method in the Point class called (move dx dy) , which displaces a point with dx units in the x direction and dy units in the y direction. We encourage you to make a functional solution in which move creates a new displaced point. After that you can make an imperative solution in which the state of the receiving point is changed.

Finally, define a new class, Rectangle , which aggregates two points to a representation of a rectangle. Define move and area methods in the new class.

As a practical remark to the 'class Point ' and the send primitive, be sure to define send before you define Point . (This is done to redefine an existing send procedure in MzScheme).

Solution

The solution is on a separate solution file


8.2   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

The solution is on a separate solution file


8.3   Representing HTML with objects in Scheme **** 

This is an open exercise - maybe the start of a minor project.

In the original mirror of HTML in Scheme, the HTML mirror functions, return strings. In the current version, the mirror functions return an internal syntax tree representation of the web documents. With this, it is realistic to validate a document against a grammar while it is constructed. In this exercise we will experiment with an object representation of a web document. We will use the class and object representation which we have introduced in this lecture.

Construct a general class html-element which implement the general properties of a HTML element object. These include:

  1. A method that generates a rendering of the element
  2. A method that returns the list of constituents
  3. An abstract method that performs context free validation of the element

In addition, construct one or more examples of specific subclasses of html-element , such as html , head , or body. These subclasses should have methods to access particular, required constituents of an element instance, such as the head and the body of a HTML element, and title of a head element. Also, the concrete validation predicate must be redefined for each specific element.


Generated: Friday January 3, 2014, 09:49:34