CMPS 144L
Spring 2019 Lab #3 (and Intersession 2020 Lab #2):
Red-White-Blue Partitioning of int[]

Resources

You are given the following Java classes, which you should load into jGrasp.

These classes are analogous to ones that were discussed in lecture (and to which you can find links starting at the course web page).


Major Task: Completing the partition() Method

Your major task for this lab is to fill in the missing pieces of code in the partition() method of the RWB_PartitionerOfInt abstract class. This class is analogous to the RedBluePartitionerOfInt class that was discussed in lecture, but there are a few important differences, the most obvious being that RWB_PartitionerOfInt's partition() method is concerned with three categories (RED, WHITE, and BLUE) rather than only two (RED and BLUE). Not surprisingly, partitioning an array with respect to three categories of values is a little bit more complicated than partitioning with respect to only two categories.

An annoying byproduct of having three categories rather than only two is that, after an array has been partitioned, there are two boundaries between adjacent segments (the RED-WHITE and WHITE-BLUE boundaries) rather than only one (RED-BLUE). For the partition() method to return the locations of both boundaries would require that it return some kind of composite object (e.g., an array of two int's, or an OrderedPairOfInt object, perhaps) rather than a value of type int, as was the case with the partition() method discussed in lecture. Rather than having the method return a composite object, your instructor decided to introduce two new methods by which a client could obtain this information, one of which returns the location of the RED-WHITE boundary and the other of which returns the location of the WHITE-BLUE boundary.

To support this approach, it was necessary to introduce new instance variables, the names of which are rw and wb, intended to suggest "red-white" and "white-blue", respectively.

As the comments in the partition() method indicate, the code you supply must be consistent with the loop invariant depicted by this figure:

   0             rw            q              wb             N
  +-------------+-------------+--------------+--------------+
b |   all RED   |  all WHITE  |      ?       |   all BLUE   |
  +-------------+-------------+--------------+--------------+

Remember that rw and wb are instance variables, so they are not declared as local variables in the partition() method. On the other hand, q is declared inside partition(), as its value is meaningful only during execution of that method. (Its name is intended to suggest that it marks the starting location of the ?-segment of the array.)

You will notice that there are two assert statements in the partition() method; their purpose is to verify that the loop invariant is true immediately before and immediately after each iteration of the loop.

Before you run the "tester" program to test your work, make sure that you enable assertions (or else they will be ignored during execution).

Enabling Assertions

To enable assertions when running a program from the command line (using the java command), you need to include the -ea (or the more verbose -enableassertions) option. For example, you would enter

java -ea RWB_PartTester

To enable assertions in jGrasp:

To verify that assertion testing is turned on, try running the AssertTest program. It should abort due to an AssertionError.


Secondary Task: Even_3Mult_Partitioner

Having gotten the partition() method to work as desired, the next thing you should do is to devise correct bodies for the three methods in the Even_3Mult_Partitioner class, isRed(), isWhite(), and isBlue(). The comments in that class make it clear what those methods should do.

Tertiary Task: partition2()

If you have time after completing the first two tasks, tackle this one, which is to develop a second version of the partition() method. Call it, say, partition2().

It should look much like the first version of the method, but it should be based upon the loop invariant depicted here:

   0             rw            wb             q           N
  +-------------+-------------+--------------+-----------+
b |   all RED   |  all WHITE  |   all BLUE   |     ?     |
  +-------------+-------------+--------------+-----------+

You should find that the loop body necessary to maintain this loop invariant is a little bit more complicated than that needed in your first version of partition(). Indeed, you will probably find that, during certain loop iterations, two swaps of array elements are called for rather than only one.

Of course, to test your work you should also introduce and make use of a new loopInvariant2() method to check that the loop invariant holds before and after each loop iteration.


Submitting Your Work

Submit the .java files that you worked on to the "lab2_dir" folder using the Student File Submission System (to which there is a link on the CMPS 144 course web page).