Versions Compared

Key

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

Homework 07 (Due 11:59pm Tuesday, October 27, 2020)

Submit via Owl-Space

Preliminaries

This homework can be done using the Functional language level of DrJava, which autogenerates the constructors, accessors, equals, and toString() methods on the assumption that the class narrowed to its fields constitutes a free algebraic type.

Composite Design Pattern for List

The following is an object-oriented formulation of lists of integers.

...

This coding pattern is called the interpreter design pattern: it interprets the abstract behavior of a class in each of its concrete subclasses. The composite pattern refers to the the structure of the composite type hierarchy, while the interpreter pattern refers to how the behavior of the variants of the type are defined uniformly via polymorphism.

Interpreter Design Pattern for List

The interpreter design pattern applied to the above composite list structure prescribes a coding pattern for list operations that is analogous to Racket 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 Racket function template while the concrete method in ConstIntList corresponds to the recursive case by calling the same method on its rest.

...

Code Block
abstract class IntList {
    abstract returnType methodName(parameter_list);
}

class EmptyIntList extends IntList {
    returnType methodName(parameter_list) {
        // base case code
    }
}

class ConsIntList extends IntList {
    int first;
    IntList rest;
    returnType methodName(parameter_list) {
        // ... first ...
        // ... rest.methodName(parameter_list) ...
    }
}

Problems

Apply the interpreter design pattern to IntList and its subclasses given above to write all of the following methods as augmentations (added code) of the IntList class. Also write a JUnit test class, IntListTest to test all of your new methods in the IntList class.  We strongly recommend that you write Template Instantiations for all of these new methods as an intermediate step in developing your code BUT DO NOT submit these Template Instantiations (or corresponding Templates) as part of your code documentation.  The structure of your program implicitly provides this information.  Confine your documentation to writing contracts (purpose statements in HTDP terminology) using javadoc notation (opening the purpose statement (preceding the corresponding definition)with /** and closing it with */.

...