CMPS 144L Spring 2019
Lab #4: Red-Blue Partitioning using Generics and Subclassing; Add/Subtract
Week of February 18

Activity #1

Motivation

In last week's lab, you worked on Java classes that could be used for partitioning an array of integers (i.e., an object of type int[]) into three segments (containing elements classified as "red", "white", and "blue" respectively), based upon various ways of classifying numbers (e.g., negative vs. zero vs. positive). Specifically, the abstract class RWB_PartitionerOfInt included a partition() method but left the responsibility of providing concrete isRed(), isWhite(), and isBlue() methods to each of its child classes. Each child class thus defined its own way of classifying integers in addition to having inherited the partition() method from its parent. As a reslt, an instance of any such child class was capable of partitioning an array of type int[] in accord with that class's criteria for deciding whether a given value of type int was red, white, or blue.

This week, we revert to two-color partitioning (red vs. blue), but we employ generics so that we can develop Java classes whose instances are capable of partitioning not just arrays containing values of type int but, more generally, arrays containing elements of any type.1


Your Tasks

Provided to you are these Java classes, some of which need work.

(1) Classify Integers by RED:even/BLUE:odd
(2) Classify Integers by RED:negative/BLUE:nonnegative
(3) Classify Strings by RED:is a palindrome/BLUE:is not
(4) Classify Strings by RED:has required # of vowels/BLUE:does not

> 1
Partitioning based on odd vs. even...

Array elements:
3 -4 7 13 -2 -5 64 13 0 41 -22 23 44

After partitioning,
  Red segment: -4 -2 64 0 -22 44
  Blue segment: 7 13 13 41 3 23 -5

Goodbye.
(1) Classify Integers by RED:even/BLUE:odd
(2) Classify Integers by RED:negative/BLUE:nonnegative
(3) Classify Strings by RED:is a palindrome/BLUE:is not
(4) Classify Strings by RED:has required # of vowels/BLUE:does not

> 2
Partitioning based on negative vs. nonnegative...

Array elements:
3 -4 7 13 -2 -5 64 13 0 41 -22 23 44

After partitioning,
  Red segment: -4 -2 -5 -22
  Blue segment: 3 7 64 13 0 41 13 23 44

Goodbye.
(1) Classify Integers by RED:even/BLUE:odd
(2) Classify Integers by RED:negative/BLUE:nonnegative
(3) Classify Strings by RED:is a palindrome/BLUE:is not
(4) Classify Strings by RED:has required # of vowels/BLUE:does not

> 3
Partitioning based on palindrome vs. non-palindrome...

Array elements:
radar elephant Glork mom bcdfg noon cat racecar I dog 1234321 Object-oriented

After partitioning,
  Red segment: radar mom noon racecar I 1234321
  Blue segment: cat elephant bcdfg dog Glork Object-oriented

Goodbye.
(1) Classify Integers by RED:even/BLUE:odd
(2) Classify Integers by RED:negative/BLUE:nonnegative
(3) Classify Strings by RED:is a palindrome/BLUE:is not
(4) Classify Strings by RED:has required # of vowels/BLUE:does not

> 4
Partitioning based on number of vowels...
  Enter min # of vowels for a string to qualify as Red: 3

Array elements:
radar elephant Glork mom bcdfg noon cat racecar I dog 1234321 Object-oriented

After partitioning,
  Red segment: elephant racecar Object-oriented
  Blue segment: mom bcdfg noon cat radar I dog 1234321 Glork

Goodbye.
(1) Classify Integers by RED:even/BLUE:odd
(2) Classify Integers by RED:negative/BLUE:nonnegative
(3) Classify Strings by RED:is a palindrome/BLUE:is not
(4) Classify Strings by RED:has required # of vowels/BLUE:does not

> 4
Partitioning based on number of vowels...
  Enter min # of vowels for a string to qualify as Red: 5

Array elements:
radar elephant Glork mom bcdfg noon cat racecar I dog 1234321 Object-oriented

After partitioning,
  Red segment: Object-oriented
  Blue segment: elephant Glork mom bcdfg noon cat racecar I dog 1234321 radar
Goodbye.


Submission of Work

Following the usual procedure, submit to the lab4_dir folder your work, which should include the source code (i.e., .java) files for (at least some of) the classes RedBluePartitioner, NegPosPartitioner, PalindromePartitioner, and VowelCountPartitioner.

Footnotes (for Activity #1)

[1] Actually, only reference types, and not any of Java's eight primitive types. But this is not a serious restriction, as each primitive type has a corresponding reference type (e.g., int has Integer) that can be used in its place without much inconvenience.



Activity #2

In the incomplete Java class AddSubtract are two methods to be completed, sum() and difference(). Their comments should be sufficient to describe their intended behavior. The class's main() method can be used for testing purposes. It expects the user to enter expressions such as 5023 + 4178 or 987543 - 123456, i.e., two decimal integer numerals of the same length separated by either the addition or subtraction operator (with spaces separating them).