CMPS 144L
Lab Activity: 2-Color Partitioning

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.

Resources

The following Java classes are provided, which you should load into jGrasp.

The functionality that an instance of the RedBluePartitioner class is intended to provide is basically this:

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.


Part #1: Complete the partition1() method

The partition1() method has some missing pieces that you are to provide. Each such missing piece is clearly pointed to by a comment. The method's purpose is to partition the array provided to it via its parameter so that, upon completion, the array elements (and the instance variable blueStart is consistent with this picture:

 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.


Part #2: Complete the partition2() and loopInvariant2() Methods

Having completed the partition1() method, now you are to complete partition2(), which has the same purpose. It will look much like partition1(), but it should be based upon the loop invariant depicted here:

 0               redStart            blueStart              N
+---------------+-------------------+----------------------+
|       ?       |      all RED      |       all BLUE       |
+---------------+-------------------+----------------------+
That is, all elements in locations [redStart .. blueStart) are RED, and all elements in locations [blueStart..N) are BLUE. The invariant says nothing about the colors of the elements in locations [0..redStart).

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.)


Submitting Your Work

Submit your RedBluePartitioner.java file to the appropriate folder using the Student File Submission/Retrieval Utility (to which there is a link on the CMPS 144L course web page). Make sure that you include comments indicating the names of all the members of your team. If your class does not meet all the specifications described here, include a comment that indicates its flaws.



Appendix: Enabling Assertions:

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

java -ea RWB_PartTester

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.