Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- HW07   


See Assignments Page for due date! Don't forget your new Honor Pledge file!

Save all of your work in a directory called HW07on your U: drive (Owlnet account) inside a directory called Comp201 .

For all the problems below, you must write a JUnit test class called Test_HW07. Individual problems should be tested using separate methods of that class. Be sure to test completely!

Note: some problems may require that you write "helper" visitors.

 

  1. (40 pts total)   Extract the list framework code from the inner class lecture into a HW07 directory.

    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.

    (10 pts) Problem 1:

    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.

    (10 pts) Problem 2:

    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.

    (10 pts) Problem 3:

    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.

    (10 pts) Problem 4:

    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.


  2. (20 pts total) Write the following two IListAlgos, which (hint!) depend on each other. The visitors should use an IListFactory to construct their results, either as a field or as an input parameter (your choice).
    1. (10 pts) EvenIndexed which returns an IList consisting of the even-indexed elements of the host IList. For example, the even-indexed elements of (a b c d e) is (a c e).
    2. (10 pts) OddIndexed which returns an IList consisting of the odd-indexed elements of the host IList. For example, the odd-indexed elements of (a b c d e) is (b d).

    --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?

  3. (20 pts) Replicate the same functionality as previous problem but instead, write a factory class, called EvenOddFactory, that has two methods, makeEven() and makeOdd() that return IListAlgos corresponding to EvenIndexed and OddIndexed above. Internally, EvenOddFactory should use two private final IListAlgo fields initialized with anonymous inner classes (the fields cannot be static if the IListFactory is passed to the constructor of EvenOddFactory -- why?).
    --Remember that an inner class can access any field in its enclosing class.
    --Modify your test code to prove that the visitors from this formulation will pass exactly the same test code as the the previous problem's visitors.
    --Why doesn't this formulation have the same instantiation problems that the previous problem did?

  4. (15 pts) Write a visitor to an IList filled with Doubles called DivideByFirst that will return an IList where every element is the corresponding original list's element divided by the first element of the original list. That is the visitor run on (2.0, 4.0, 5.0, 7.0) should return (1.0, 2.0, 2.5, 3.5).
    -- The visitor and any helpers should be written in such a way that the Object input parameter are never used!
    -- Be careful of name conflicts. Be sure to name similar things differently so that you can be sure of which one is being referenced.

  5. (15 pts) Write a visitor to an IList called FirstToLast that will return an IList where the first element of the original list has been moved to being the last element. No other elements have been deleted or moved.
    -- Use an anonymous inner class for the helper visitor
    -- Use the technique of passing the IListFactory as an input parameter to FirstToLast.
    -- The helper visitor must NOT use its Object input parameter however!
    -- Be careful of name conflicts. Be sure to name similar things differently so that you can be sure of which on is being referenced.

110 points total.

 

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:22 CDT

©2008 Stephen Wong and Dung Nguyen