update 2, 3/5/2011:18:37
update 1.1, 3/11/2011:12:15

Programming Project: The Two-Towers Problem

Two Towers Problem No, not the towers in Middle Earth. Nor the ones in Hanoi. You will find the background and considerably helpful suggestions and hints in your text, Section 8.7 Laboratory: Tho Two-Towers Problem

However, pay attention to the instructions given here as I am superseding a number of the requirements. (Remember my suggestions for success -- "follow instructions".) Even if you perfectly implement the problem as specified in the book, you will lose credit if you do not attend to the modifications to the instructions given below. The SubsetIterator class you write should use generics and generic Vectors. You will actually use a specific type of Vector, perhaps doubles but I'm not saying; however, your SubsetIterator should take a generic Vector and return subsets one at a time.

Requirements

Follow the instructions given in the text on pages 175-177 except as follows:

  1. Write your class, SubsetIterator, to implement the java.util.Iterator interface instead of extending AbstractIterator. You do not need to implement the Enumeration interface. As indicated in the text, your class should have a constructor that consumes a single Vector and the resulting SubsetIterator should return, in turn, different subsets of the elements found in the Vector.
  2. [No changes; follow the instructions. Your reset() method should start your SubsetIterator back at the beginning.]
  3. The hasNext() is required by the Iterator interface. This bit in the text is worded awkwardly. Simply follow the requirements of the Iterator interface found in the Java API documentation. After a SubsetIterator has returned the final subet, this method should return false. Return true if there are more subsets to process.
  4. This get method can be a private method that is called by your next() method. Hint: it might be a good idea to have this method take an argument that represents the particular subset that is desired for this call. Update 2: Note: the get method is introduced in the text as part of the AbstractIterator class. Since we are not extending from that, your get method should is free to serve our own purposes. If you write it to return a subset based on a given index, it may be handy for a later assignment.
  5. [No changes]
  6. Since you are implementing the Iterator interface instead of extending the abstract class mentioned by the author, you will need to implement the remove() method. However, as with the PRNG class that you wrote for assignment 2, this need only throw an UnsupportedOperationException.

With your SubsetIterator class in hand, write a program (another class called TwoTowers) that solves the Two-Towers problem. Note: in order to support the solution of the problem, you will probably want to write additional methods besides the ones prescribed above. For example, you might want a method that computes the sum of the contents of a Vector. It is sufficient for your solution to print out the box sizes in the two partitions, together with their respective sums and a total difference between the two towers.

Update 1.1: Note that the textbook states that units for the fifteen-block example are tenths of an inch. Given that manipulating a block that has an area of one tenth of a square inch would be completely impractical, we will interpret units as inches. Correspondingly, the second best solution -- the one shown above -- has an error of 129 hundred-thousandths of an inch -- NOT millionths. Keep this in mind as you test your program and evaluate the solutions it finds.

Non-optional bonus work

I want all of you to at least consider the "Thought Questions" found at the end of the project in your text. I intend to assign either a lab or a homework that is related to the thought questions of this project so I want you to have thought about the issues.

[optional] If you implement (and test and document) the randomSubset() method and use it to generate an approximate solution, you will get extra credit.

Submission instructions:

Do not forget to use the javadoc constructs for your documentation and include the html file produced by the javadoc tool in your final submission. Make sure that you create a folder named with your Westmont email name followed by "HW7". For example, someone with email address "cjones" would create a folder called "cjonesHW7". Make sure that inside that folder you have: for both parts of the assignment, java source files and html documentation files resulting from javadoc. Finally, either tar or zip the folder so that when we extract it, the folder "<emailname>HW7" will be created. Submit the packaged file for assignment 7 on Eureka.