In lecture, two algorithmic solutions to the 2-Color Partitioning Problem were presented. In this lab, you are given the task of solving the same problem in two more ways.
In each of your two solutions —embodied in the methods partition1() and partition2()— you will use Java's assert statement for the purpose of verifying that the method's intended loop invariant is true immediately before and immediately after each iteration of its loop.
The functionality that an instance of the RedBluePartitioner class is intended to provide is basically this:
To support these observer methods, instance variables are declared.
Before embarking upon the first activity, you should carefully examine the source code of the RedBluePartitioner class so that you come to an understanding of, generally, how it is organized and, specifically, the purpose of each instance variable and method.
0 blueStart N +---------------------------+--------------------------------+ | all RED | all BLUE | +---------------------------+--------------------------------+ |
That is, all elements in locations [0..blueStart) are RED, and all elements in locations [blueStart..N) are BLUE (where N is the length of the array).
As the comments in the partition1() method indicate, the code you supply must be consistent with the loop invariant depicted by this figure:
0 qStart blueStart N +-------------------+---------------+----------------------+ | all RED | ? | all BLUE | +-------------------+---------------+----------------------+ |
That is, all elements in locations [0..qStart) are RED, and all elements in locations [blueStart..N) are BLUE. The invariant says nothing about the colors of the elements in locations [qStart..blueStart).
Note that blueStart is an instance variable, so it is not declared as a local variable in the partition1() method. On the other hand, qStart is declared inside partition1(), as its value is meaningful only during execution of that method.
You will notice that there are two assert statements in the partition1() method; their purpose is to verify that the loop invariant is true immediately before and immediately after each iteration of the loop. They do so by calling the loopInvariant1() method, which, as its name suggests, evaluates the loop invariant. If the condition specified in an assert statement evaluates to false, an exception is thrown.
Before you run the "tester" program to test your work, make sure that you enable assertions (or else they will be ignored during execution). See the appendix for how to do that.
0 redStart blueStart N +---------------+-------------------+----------------------+ | ? | all RED | all BLUE | +---------------+-------------------+----------------------+ |
Remember that blueStart is an instance variable. However, redStart is not, as its value is relevant only during execution of partition2().
To verify that your loop is consistent with the loop invariant described here, you should introduce and make use of a new loopInvariant2() method to check that the loop invariant holds before and after each loop iteration. (An incomplete version of the method has been provided.)
To enable assertions in jGrasp:
Note: In the less likely event that you are running a Java program from the command line (using the java command), to enable assertions you need to include the -ea (or the more verbose -enableassertions) option. For example, you would enter
End of note.
To verify that assertion testing is turned on, try running the AssertTest program. It should abort due to an AssertionError. If it does not, it means that assertion testing has not been turned on.