Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This homework can be done using either the Elementary or Intermediate language level of DrJava. If you are comfortable with casting between primitive types, the Intermediate level is probably a better choice because it supports such casting operations. You can test these operations in the Interactions pane, which supports the full Java language regardless of what language level is selected.

Download and extract the attached file shapes.zip (Intermediate version: shapes1.zip) into the same subdirectory. This .zip file contains the following files:

  • Shape.dj0/dj1
  • Rectangle.dj0/dj1
  • Circle.dj0/dj1
  • TestRectangle.dj0/dj1
  • TestCircle.dj0/dj1

Use DrJava to open all these files and perform a Compile All (by clicking the Compile button). Click the Test button to run the two JUnit test files. Everything should compile and pass all the JUnit tests. Use these files as examples for documentation, coding style and JUnit testing.

Composite Design Pattern for List

...

Code Block
/** Abstract list structure.  IntList := EmptyIntList + ConsIntList(int, IntList) */
abstract class IntList { }
 
/** Concrete empty list structure containing nothing. */
class EmptyIntList extends IntList { }
 
/** Concrete non-empty list structure containing an int, called first,
 and *an andIntList acalled rest, which is a list structure.
  */
class ConsIntList extends IntList {
    int first;
    IntList rest;
}

The above implementation is an example of what is called the Composite Design Pattern. The composite design pattern is a structural pattern that prescribes how to build a container object that is composed of other objects whose structure is isomorphic to that of the container itself. In this pattern, the container is called a composite. Here the container object is the list and ConsIntList is said to be a composite: it is a list and is composed of a substructure that is itself a list.

...

The interpreter design pattern applied to the above composite list structure prescribes a coding pattern for list operations that is analogous to Scheme function template. It entails declaring an abstract method for each list operation in the abstract list class, IntList, and defining corresponding concrete methods in the concrete list subclasses: the empty list class, EmptyIntList}, and the non-empty class, {{ConsIntList. The concrete method for EmptyIntList corresponds to the base case in the Scheme function template while the concrete method in ConstIntList corresponds to the recursive case by calling the same method on its rest.
The following is the coding template for the interpreter design pattern for IntList and its subclasses.

...