You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

bHomework 7 (Due 11:59pm Thursday, November 5, 2020)

Submit via SVN

Preliminaries

This homework should be done in Full Java (using DrJava, IntelliJ, Eclipse, or a text editor and command line compilation and execution).  The Functional Java language in DrJava regrettably no longer works for more complex OO code such as the visitor pattern.  In this assignment, you will re-implement each of the functions on IntLists assigned in Homework 7 using the visitor pattern.

As before, your program must support the object-oriented formulation of lists of integers defined the composite class hierarchy where

  • IntList is an abstract list of int.
  • EmptyIntList is an IntList
  • ConsIntList(first, rest), where first is an int and rest is an IntList, is an IntList

The Homework Support files IntList.java, IntListVisitor.java, and IntListTest.java provide a starting point for your code. 

Problems

Apply the visitor design pattern to define visitor classes implementing the IntListVistor interface IntList and its subclasses given above to formulate all of the following methods as  visitorsJUnit test class, IntListTest to test all of your new methods in the IntList class.  Use the LengthVisitor example as a guide for defining your new visitor classes.  Augment the test clas IntListTest.java to include test methods for each of your visitor classes.  Confine your documentation to writing contracts (purpose statements in HTDP terminology) for each visitor using javadoc notation (a comment preceding the corresponding definition) beginning  with /** and closing  with */ for each visitor class.  Use the documentation of LengthVisitor in the Homework Support files as an example.

  • (10 pts.) boolean contains(int key)  returns true if key is in the list, false otherwise.
  • (10 pts.) int reverse() constructs a list that is the reversal of this.
  • (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: you can cast an int to double by using the prefix operator (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)
  • No labels