CS 394P: Object-Oriented Programming Exercise

Due: March 28, 2005.

The goals of this exercise are to gain experience in using object-oriented programming to write generic programs, and to investigate performance of object-oriented code.

The graphical part of this assignment should be done using xgcl (Gnu Common Lisp, GCL, with an interface to X windows), /p/bin/xgcl on a Linux or Sun workstation. Enter (load "/projects/cs394p/dw.lsp") as the first command to Lisp to load all the files needed. (File names shown below are in the directory /projects/cs394p/.)

You can use xdvi /projects/cs394p/dwdoc.dvi to view documentation of the drawing primitives.

  1. Examine the code in the file oops.lsp and understand how it works. Example files for use with this code are oopexa.lsp, oopexa.tst, planet.lsp, and drawobj.lsp.
  2. Add to the file oopexa.lsp a method move-by with arguments deltax and deltay that will modify a vector by incrementing its x and y values by the specified amounts. This method should work for any kind of vector. Add methods to make the examples in the file oopexa.tst work.
  3. Examine the execution overhead of this object-oriented system experimentally as follows.

    For this part of the exercise, you should use plain GCL, /p/bin/gcl. Enter (load "/projects/cs394p/dwb.lsp") to load files for use with this Lisp.

    Consider the functions t1 through t4 in the file ooptime.lsp ; time these functions using (time code) and using a suitable number of repetitions n as the argument to the functions.. Compile all functions and methods; the function (compile-methods) will compile all methods. These functions should allow you to compare:

    1. the amount of time required to execute the loop and perform the additions
    2. the overhead of a function call
    3. the overhead of a message send
    4. the overhead of creating a new object.
  4. Add to the file drawobj.lsp a method to draw some other kind of object. Verify that the slide message works for your object.
  5. Add a composite-object that is composed of multiple drawable objects, each of which has an offset from the offset of the composite object as a whole. Implement a method add-object to add a new object to the composite object at specified offsets, and a draw method. Verify that the slide message works for a composite object and that a composite object can have composite components.
  6. Replace the + method for vector with a method that is more general. Assume that the definition of vector addition is to make a new instance of the same type as the first vector, and to set each of its slots to the value obtained by adding the slot value of the first vector to the corresponding message value of the second vector; of course, the addition should also be done by a message. You can use class-of and slot-names to find out the class of the first arguemnt and the names of its slots. Demonstrate that your single generic function for vector addition works for xyvector, xyzvector, rthvector, sivector, vectorvector, and appropriate combinations of these (e.g., xyvector combined with rthvector). Investigate the time required for this generic function and compare it with the time required for the + method given in oopexa.lsp.