Versions Compared

Key

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

...

Preliminaries

This homework can should be done using either the Elementary or Intermediate Functional 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.

Composite Design Pattern for List

...

  • (10 pts.) boolean contains(int key) : returns true if key is in the list, false otherwise.
  • (10 pts.) int length() : computes the length of the list.
  • (10 pts.) int sum() : computes the sum of the elements in the list.
  • (10 pts.) 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.
  • (10 pts.) IntList notGreaterThan(int bound) : returns a list of elements in this list that are less or equal to bound .
  • (10 pts.) IntList remove(int key) : returns a list of all elements in this list that are not equal to key .
  • (10 pts.) IntList subst(int oldN, int newN) : returns a list of all elements in this list with oldN replaced by newN .
  • (30 pts.) 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.