|
Comp201: Principles of Object-Oriented Programming I
|
Note: some problems may require that you write "helper" visitors.
Be sure to write your test class/method first! (See the homework assignment page) The test class with the first test method can be downloaded in Prob. 1 below.
Put all your visitors in the listFW.visitor package.
Write a visitor to copy a list called CopyFac that takes an IListFactory in its constructor (and this has a corresponding private field for such) and uses that factory to instantiate any needed lists.
Test your visitor using both the CompositeListFactory and the InnerCompListFact factory classes. Download Test_HW07.java into your default package directory to see how one can properly test for all the possibilities of the two different factory classes. Why was it important to write the test code in this manner, i.e. with two methods?
For your emptyCase(), should you return the host or _factory.makeEmptyList()? Think about what you want to mean by having the copy algorithm use a factory in the first place. Make your choice and add a comment to your code defending your choice.
Write a visitor to a list of Integers called SumNested that uses forward accumulation and a private static nested helper class (singleton!) to sum all the elements of the list. SumNested should be a singleton.
Your test code should demonstrate that SumNested works on lists made using CompositeListFactory or InnerCompListFact.
Write a visitor to a list of Integers called SumInner that uses forward accumulation and a private static final variable referencing an anonymous inner class to sum all the elements of the list. SumInner should be a singleton.
Hint: Just copy the code from SumNested and modify it.
Other than the difference in the name of visitor class under test, should the test method for Prob. 3 be any different than that for Prob. 2?
Your test code should demonstrate that SumInner works on lists made using CompositeListFactory or InnerCompListFact.
Write a visitor called RemoveLast to remove the last element of a list. As before, this visitor should take an IListFactory in its constructor. Express the required helper visitor as a private final variable referencing an anonymous inner class.
Note a couple of items that will be discussed further in class:
- The anonymous inner class can access the field referencing the IListFactory directly as any other code inside the RemoveLast class.
- The helper variable cannot be declared static. This is a result of using the first note above. Therefore use the convention of a lower-case name, rather than the upper-case name reserved for static variables.
- As in Prob. 1, make a choice as to what you will do in the empty case and add a comment defending your choice.
Test your code using both CompositeListFactory and InnerCompListFact.
For the following problems, you need only test using a single
IListFactory
implementation.
--Note: If one visitor uses the other, the visitor being used must be instantiated
right when it is used. It cannot be "pre-instantiated" as a field.
Why?
--Hint: If first is the first
even-indexed element of a list, what are the rest of the even-indexed elements
with respect to the rest of the list? Similarly, how can you describe the
odd-indexed elements of a list?
When you have completed your homework, zip up the entire HW07 directory and submit the zipped file using the file upload link on the Assignments Page.
Last Revised Thursday, 03-Jun-2010 09:50:16 CDT
©2007 Stephen Wong and Dung Nguyen