Versions Compared

Key

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

...

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.

...

Apply the interpreter design pattern to IntList and its subclasses to write the following methods. Also write a JUnit test class to test all methods in EmptyIntList and a different JUnit test class to test all methods in ConsIntList . We strongly recommend that you write Template Instantiations as an intermediate step in developing your code BUT DO NOT submit these Template Instantiations (or corresponding Templates) as part of your code documentation. Confine your documentation to writing contracts (purpose statements in HTDP terminology) using javadoc notation. The javadoc documentation style will be discussed in the upcoming lab.

...

...

<ol>
HTML
<li>
  • boolean contains(int key) : returns true if key is in the list, false otherwise.

...

...

<li>
  • int length() : computes the length of the list.
HTML
<li>
  • int sum() : computes the sum of the elements in the list.

...

  • double average() : computes the average of the elements in the list; returns 0 if the list is empty.

    Hint: at the Intermediate level you can cast an int to double by using the prefix operator (double). At the Elementary level, casts are illegal, but you can use the workaround of adding 0. to an int to convert it to double.

...

...

<li>
  • IntList notGreaterThan(int bound) : returns a list of elements in this list that are less or equal to bound .

...

  • IntList remove(int key) : returns a list of all elements in this list that are not equal to key .

...

  • IntList subst(int oldN, int newN) : returns a list of all elements in this list with oldN replaced by newN .

...

  • IntList merge(IntList other) merges this list with the input list other, assuming that this list and other are sorted in ascending order. Note that the lists need not have the same length.

    Hint: add a method mergeHelp(ConsIntList other) that does all of the work if one

...

  • list is non-empty (a ConsIntList

...

  • ). Only

...

  • mergeHelp is recursive. Use dynamic dispatch on the list that may be empty. Recall that a.merge(b) is equivalent to b.merge(a) . This approach is the Java analog of the extra credit option in HTDP Problem 17.6.1 in HW 3.

...