CMPS 144L
Lab Activity: Filtering 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 whimsically
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
RedBlueFilter,
instances of which have a method, called filter(),
that prints either the RED elements of a given array or the
array's BLUE elements, according to which color is asked for
(using a second parameter).
However, that method is in need of completion.
The RedBlueFilter class is designed to be used for
filtering 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
RedBlueFilter.
For example, suppose that rbf had been declared to be a variable
of type RedBlueFilter and then this assignment statement
were executed:
rbf = new RedBlueFilter<Integer>(new PrimeVsComposite());
That would establish rbf as being bound to an instance of
the RedBlueFilter class capable of "filtering" an array A[]
of integers so that the call rbf.filter(A,true) would print the
elements of array A[] that are prime (RED).
Meanwhile, the call rbf.filter(A,false) would print the elements
of A[] that are composite (BLUE).
For the purposes of testing your work, provided is the Java application
Tester.
Below are dialogs corresponding to two runs of this program.
In the one on the left, the user chose to filter an array according
to how numbers are classified by the PrimeVsComposite class.
In the one on the right, the user chose to filter an array according
to how numbers are classified by the NegVsPosInt class.
Filtering based on PrimeVsComposite
Choose among these:
(1) Classify integers by prime vs. nonprime
(2) Classify integers by negative vs. nonnegative
(3) Classify integers by small vs. big
(4) Classify Strings by word vs. nonword
> 1
Array elements:
3 -4 77 0 95 -2 -5 64 13 -1 41 -22 0 27 23
Elements that are RED:
3 -2 -5 13 41 23
Elements that are BLUE:
-4 77 0 95 64 -1 -22 0 27
|
|
Filtering based on NegVsPosInt
Choose among these:
(1) Classify integers by prime vs. nonprime
(2) Classify integers by negative vs. nonnegative
(3) Classify integers by small vs. big
(4) Classify Strings by word vs. nonword
> 2
Array elements:
3 -4 77 0 95 -2 -5 64 13 -1 41 -22 0 27 23
Elements that are RED:
-4 -2 -5 -1 -22
Elements that are BLUE:
3 77 0 95 64 13 41 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
- RedBlueFilter:
Generic Java class, instances of which can perform 2-color
filtering 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 adjustments to the filter()
method of this class to make it work.
- 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.
- Tester:
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.