CMPS 144L
Lab Activity: Partitioning using Red/Blue Classifiers

The goal of this activity is to give students practice in developing classes that implement a generic interface and in using instances of those classes.

The generic interface is RedBlueClassifier. As its name suggests, an instance of an implementing class is for the purpose of classifying objects (of some specified type) as being in either of two categories, which we arbitrarily refer to as RED and BLUE.

Provided is the class PrimeVsComposite, which implements RedBlueClassifier<Integer>. As its name suggests, it distinguishes between numbers whose absolute values are prime (which it classifies as RED) and those whose absolute values are non-prime (BLUE). The student should use this class as a model when developing each of the following (from scratch):

Also provided is the generic Java class RedBluePartitioner, instances of which perform 2-color partitioning of arrays (such has been studied earlier in the course). However, the partition() method has a couple of syntax errors that need to be corrected.

The RedBluePartitioner class is designed to be used for partitioning arrays containing elements of any reference type[1] and based upon any criteria by which to distinguish between RED and BLUE elements of that type. But of course those criteria must be explicitly specified in some fashion! How? By passing a "RED/BLUE classifier" (i.e., an instance of a class that implements the RedBlueClassifier interface) to the constructor of RedBluePartitioner.

For example, suppose that part had been declared to be a variable of type RedBluePartitioner and then this assignment statement were executed:

part = new RedBluePartitioner<Integer>(new PrimeVsComposite());

That would establish part as (pointing/referring to) an instance of the RedBluePartitioner class capable of partitioning an array of integers so that all prime (i.e., RED) elements of the array end up in lower-numbered locations than the non-prime (i.e., BLUE) elements. To partition array A[] in this way, you would make the call

part.partition(A);

For the purposes of testing your work, provided is the Java application RB_PartTester. Below are dialogs corresponding to two runs of this program. In the one on the left, the user chose to partition an array according to how numbers are classified by the PrimeVsComposite class. In the one on the right, the user chose to partition an array according to how numbers are classified by the NegVsPosInt class.

Partitioning based on PrimeVsComposite
Choose among these:
  (1) Partition int's by prime vs. nonprime
  (2) Partition int's by negative vs. nonnegative
  (3) Partition int's by small vs. big
  (4) Partition Strings by word vs. nonword

> 1

Array elements:
3 -4 77 0 95 -2 -5 64 13 -1 41 -22 0 27 23

After partitioning,
  RED segment: 3 23 41 13 -5 -2
  BLUE segment: 95 64 0 -1 77 -22 0 27 -4
Partitioning based on NegVsPosInt
Choose among these:
  (1) Partition int's by prime vs. nonprime
  (2) Partition int's by negative vs. nonnegative
  (3) Partition int's by small vs. big
  (4) Partition Strings by word vs. nonword

> 2

Array elements:
3 -4 77 0 95 -2 -5 64 13 -1 41 -22 0 27 23

After partitioning,
  RED segment: -22 -4 -1 -5 -2
  BLUE segment: 95 0 64 13 77 41 3 0 27 23

The arrays tested by the program are "hard-coded" (i.e., described within the source code using literals), but you are free to modify that source code to test other arrays.


Summary of Relevant Java artifacts


Footnote

[1] Recall that Java has eight primitive data types (including int, double, boolean, and char). Data types that arise from classes are reference types, called that because the value of a variable of a reference type is a reference, or pointer, to an object.