With respect to the class RedBluePartitionerOfInt, the method partition1() accepts (as an argument) an array of int values and rearranges them so that the array ends up with two homogeneous segments, one containing RED values followed by one containing BLUE values. (Values are classified as being either RED or BLUE by an instance of some class that implements the RedBlueClassifierOfInt interface. Such an object is passed to the constructor of RedBluePartitionerOfInt and becomes part of the state of the object thereby initialized.)
Using N as an abbreviation for b.length, the loop invariant that guided the development of the code in partition1() was
As a picture, we could depict the (conjunction of the) 2nd and 3rd conjuncts by
0 k m N
+---------------+----------------+-------------------+
b | all RED | all BLUE | ? |
+---------------+----------------+-------------------+
The method partition2(), which you are to supply, should be modeled, generally, after partition1(). (The two methods have exactly the same purpose in that their their pre- and post-conditions are identical.) The difference between them is that the development of the code in partition2() should be guided by the loop invariant given by the following picture:
0 k m N
+---------------+-------------------+----------------+
b | all RED | ? | all BLUE |
+---------------+-------------------+----------------+
In words, this says that all values in b[0..k-1]
are RED and all values in b[m..N-1] are BLUE.
In Java, execution of a command of the form
where B is a boolean expression, has no effect if B is true, but throws an AssertionError exception if B is false.
Notice that, in partition1(), there are two assert commands, each of which invokes the method loopInvariant1(). Notice also that these commands are placed in such a way that the first will be executed immediately before each iteration of the loop and the second will be executed immediately after the last iteration of the loop. As you may have guessed, loopInvariant1() returns the value true if and only if partition1()'s loop invariant is true. Hence, the purpose of the assert commands is to verify, with respect to whatever test cases we may feed to partition1(), that its loop invariant is true at those points during execution where we expect it to be true.
In analogy to loopInvariant1(), you are to supply the method loopInvariant2(), which should return the value true if and only if partition2()'s loop invariant is true. And, of course, the method partition2() should include assert commands to verify that its loop invariant is true immediately before each loop iteration and after the last one.
For the purpose of testing your work, the class RedBluePartTester is provided.
Finally, you should be aware that, by default, assert commands are ignored by Java. In order to make such a command operative, you must invoke the -source 1.4 option when compiling the class in which the command is found and you must invoke the -enableassertions (which can be abbreviated -ea) option when running an application that executes the command. Details can be found here. (This link also appears on the course web page.)