CSG 107: Problem Set 8

Due: Wednesday, March 25, 2009 at 5:00 pm.

Purpose:

The goal of this problem set is to help you learn to design more complex class hierarchies and to use abstraction in an object-oriented language.

As always, you must follow the design recipe. The deliverables for the design recipe are as in the preceding problem set.

Drill:

HtDC: 18.7-18.13, 18.16-18.18, 19.4

Required Problems:

Note: You must use DrScheme's ProfessorJ Intermediate Student Language.

  1. Extend homework problem 7.3 (the Matryoshka dolls in ProfessorJ) by creating a DollsWorld animation, as in homework problem 2.4 . Since you can't display an arbitrary image in ProfessorJ, draw the doll as a tower of strings. For example, imagine that the original doll had 6 layers, with layer 1 being the innermost and layer 6 the outermost. If we then did 3 "d" commands, the result might look like:
    Layer 3
    Layer 2
    Layer 1     Layer 6    Layer 5   Layer 4
    ----------------------------------------  
    
    where the dotted line represents the bottom of the canvas. Your animation should support the "d", "a", and "q" commands, as in problem 2.4.
  2. Consider the following variation on homework problems 2.2, 3.3, and 7.2:
      A Enumeration is either an OpenDotList or a ClosedDotList
    
      An OpenDotList is a list of Items
    
      A ClosedDotList is also a list of Items
    
      An Item is either a String or an Enumeration
    

    Enumerations support the following operations:

    Item get(int n) -- returns the n-th item in the enumeration (counting from 1). If there is no n-th item (either because n is greater than the length of the enumeration or because n is <= 0), returns the empty string.

    Enumeration remove(int n) -- returns an enumeration like the original, except that the n-th item has been removed. If there is no n-th item, returns an enumeration just like the original.

    Enumeration replace(int n, Item item) -- returns an enumeration like the this one, except that the n-th item (if any) is replaced by the given item.

    Enumeration swap(int n, int m) -- returns an enumeration like the original, except that the n-th and m-th items are swapped. If there is no n-th or m-th item, returns an enumeration just like the original.

    IListOfString render() -- as in homework problem 7.2, except that it does not take a style argument: the style is determined by whether the Enumeration is an OpenDotList or a ClosedDotList . Returns a list of strings, as in 7.2. For example, the result of render might be an object representing the following list of strings:

    "* Here is the first item in a ClosedDotList, which we'll call list A"
    "* Here is the second item in the list."
    "* o The third item in the list is an OpenDotList, which we'll call list B"
    "  o Here is the second item in the OpenDotList."
    "  o o The third item in the OpenDotList is another OpenDotList, which we'll call list C"
    "    o Here is the second item in list C."
    "    o List C could have been a ClosedDotList, of course, but it wasn't."
    "  o Here is the fourth, and last, item in list B."
    "* This is the fourth item in list A."  
    

    Every item that is a string should have exactly one open or closed dot in front of it, unless it occurs as the first item of a sublist, as in the case of lines 3 and 5 in the example above. [Puzzle: how many dots can a string have in front of it?]

    Implement the class of Enumerations. Use abstraction to reduce the amount of duplicated code wherever feasible.


Last modified: Mon Mar 16 22:22:00 2009