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

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


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.