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
Provided to you are these Java classes, some of which need work.
As given, only the class's heading reflects the fact that this is supposed to be a generic class, as it refers to the generic type parameter, T. In the remainder of the class's source code are several occurrences of int that need to be changed to T. Indeed, except for the comments and the class heading having been changed, what has been given to you is a RedBluePartitionerOfInt class. Your task is to make all the appropriate substitutions of T for int.
You can use this (correct, as given) class as a model for developing the other child classes of RedBluePartitioner mentioned below.
As given, this class needs work. Use EvenOddPartitioner as a model.
As given, this class needs work. Use EvenOddPartitioner as a model. But keep in mind that this class is for partitioning arrays containing String objects, not Integer objects.
Note that, until you appropriately modify the three child classes of RedBluePartitioner, compiling the given application program will likely result in errors being reported. To "fix" that, simply "comment out" any branches of the if-else statement that refer to child classes that you haven't worked on yet. For example, if you start by working on the NegPosPartitioner class and you want to test it —while leaving the PalindromePartitioner and VowelCountPartitioner classes to work on later— comment out the 3rd and 4th branches of the if-else statement, as all references to those two classes are found there. Later, when you are working on PalindromePartitioner and want to test it, restore the 3rd if-else branch!
(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. |
[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.
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).