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):
- NegVsPosInt: A class that implements
RedBlueClassifier<Integer>,
classifying as RED any integer that is negative and as
BLUE any integer that is nonnegative.
- SmallVsBigInt:
A class that implements RedBlueClassifier<Integer>,
classifying as RED any integer that is less than a boundary value
(which is supplied to the class's constructor) and as BLUE any
integer that is not less than that boundary value.
- WordVsNonWord:
A class that implements RedBlueClassifier<String>,
classifying as RED any String that is composed entirely of letters
(which we define to be characters that satisfy the
Character.isLetter() method).
All other strings are classified as BLUE.
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
- RedBluePartitioner:
Generic Java class, instances of which can perform 2-color
partitioning upon arrays containing elements of any reference type.
The constructor receives a "RED/BLUE classifier object"
(i.e., an instance of a class that implements the
RedBlueClassifier interface (see below)),
which is used for classifying each array element.
The student must make a couple of minor adjustments to the
partition() method of this class to correct syntax errors.
- RedBlueClassifier:
Generic Java interface; an implementing class can classify values
(of a specified reference type) as being either RED or BLUE.
- PrimeVsComposite:
A class that implements RedBlueClassifier<Integer>,
classifying as RED any integer whose absolute value is prime.
All other integers are classified as BLUE.
- NegVsPosInt:
A class that implements RedBlueClassifier<Integer>,
classifying as RED any integer that is negative.
All other integers (i.e., nonnegative ones) are classified as BLUE.
The student must supply this class.
- SmallVsBigInt:
A class that implements RedBlueClassifier<Integer>,
classifying as RED any integer that is less than a boundary value
that is supplied to its constructor.
All other integers (i.e., ones that are greater than or equal to
that boundary value) are classified as BLUE.
The student must supply this class.
- WordVsNonWord:
A class that implements RedBlueClassifier<String>,
classifying as RED any String that is composed entirely of characters
that satisfy the Character.isLetter() method.
All other strings are classified as BLUE.
The student must supply this class.
- RB_PartTester:
Java application that is for the purpose of testing all the
classes listed above.
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.