(Generics): Exercises

FIND A SOLUTION AT Academic Writers Bay

Unit 6 (Generics): Exercises
2020-10-08
1 The code bundle
This unit’s code bundle contains quite a few packages, but you do not need to
worry too much about most of them | they are provided as tools that can be
used for testing your implementation(s), but don’t have to be. See the tests in
the code bundle for examples of using some of these classes.
The packages provided are, with the packages that are directly relevant to
the exercises in bold font, and the less relevant packages in italic font:
arrayGenerator Yet another version of array generators, this time defined generically, so able to generate different sorts of array. Most of the array generators
make use of Scopes that define ranges of values (that array generators can
then randomly pick from to populate their arrays).
genericMethods This is where you will be doing most of your work. The
exercise for this unit is to implement (and test) a small generic method.
scope This package defines the Scopes that are used in the arrayGenerator
package.
unaryPredicate This is not in bold font, nor in italic font. It is not necessary
at all for the logbook exercise, but it is required if you wish to attempt the
additional question on predicate (exercise 3).
1
CIS2344 Algorithms, Processes, and Data Hugh Osborne
2 The exercises
Please note: The first two questions in this exercise set ask you to implement
generic methods, not generic classes. See the Unit 6 notes (towards the end)
for pointers to implementing generic methods. A simple example of a generic
method, the method
static <T> boolean equals(T object1, T object2)
is in this exercise set’s code bundle, in the Equals class, in the genericMethods
package.
1. Swap Logbook question
Write a generic method to exchange two elements of an array. You should
implement this method in the Swap class, which is provided, but which does
not contain a swap method. You should add a static swap method to this
class. The method should take an array, and two integer indices into the
array, and swap the two entries in the array at those indices. If you do not
get the signature of the method right in your implementation the SwapTest
tester class will not compile.
For example, if names is the String[] array:
f”Tony”,”Andrew”,”Hugh”,”Diane”,”Simon”,”Gary”g
then, after a call of swap(names,1,4), the array names should contain
f”Tony”,”Simon”,”Hugh”,”Diane”,”Andrew”,”Gary”g
2. Max Model question
Unit 6 (Generics): Exercises page 2
CIS2344 Algorithms, Processes, and Data Hugh Osborne
Write a generic method that will take an array of Comparables1, and two
integer indices into the array, and return the largest element in the array
between the first index (inclusive) and the second index (exclusive). It is
probably best to create a new class (Max, perhaps) to hold this method.
For example, if names is the String[] array:
f”Tony”,”Andrew”,”Hugh”,”Diane”,”Simon”,”Gary”g
a call of max(names,1,4) should return “Hugh”.
3. Satisfy
Write a generic method to count the number of elements in an array that
have a specific property.
Hints:
ˆ This is where the unaryPredicate package is used. This package
includes a generic UnaryPredicatehTi interface definition. This specifies that any implementation of the UnaryPredicate interface must
implement a test method that will take an object of the generic type
and return a boolean.
ˆ The package also contains a class, called IsEven, which is an example
of an implementation of UnaryPredicate, over Integers.
ˆ Write some more implementations of UnaryPredicate. Some possibilities are, e.g.
{ IsOdd: T will, in this case, be Integer and the test method will
tell you whether an Integer parameter is odd;
{ IsPrime: T will again be Integer and the test method will tell
you whether the Integer parameter is prime;
1See the unit 5 exercises for an explanation of Comparables.
Unit 6 (Generics): Exercises page 3
CIS2344 Algorithms, Processes, and Data Hugh Osborne
{ IsPalindrome: T will be String and the test method will tell
you whether the String parameter is a palindrome (reads the same
left to right as right to left)2 ;
{ IsMonotonic: T will be array of Comparables and the test method
will tell you whether the array of Comparables parameter is monotonic | i.e. consistently increasing or consistently decreasing.
ˆ The UnaryPredicateCount interface extends the UnaryPredicate
interface to specify that implementations of this subclass must also
implement a numberSatisfying method that, given an array containing objects of the correct type, will return the number of objects in the
array that satisfy the predicate’s test.
ˆ Define an abstract class, called CountingUnaryPredicate, that is
a partial implementation of UnaryPredicateCount, implementing the
numberSatisfying method specified in UnaryPredicateCount, but
without implementing the test method specified in UnaryPredicate.
ˆ Now change the class signatures of your implementations of UnaryPredicate
from, e.g.
public class IsEven implements UnaryPredicate<Integer>
to
public class IsEven extends CountingUnaryPredicate<Integer>
so that these classes inherit the numberSatisfying method.
2For example: mum”, naan”, radar”, rotator”, Malayalam” (malayalam), detartrated”, God’s dog” (godsdog), Able was I ere I saw Elba” (ablewasiereisawelba), A man,
a plan, a canal | panama!” (amanaplanacanalpanama).
Note: punctuation and capitalisation are usually ignored in determining palindromicity. In the
list above, where relevant, the palindromes are also shown, between brackets, with punctuation
removed, and with upper case letters replaced by lower case.
Unit 6 (Generics): Exercises page 4
CIS2344 Algorithms, Processes, and Data Hugh Osborne
ˆ Implement some test classes for your unary predicate classes.
4. Previous exercise sets
The previous exercise sets only worked with integer values. Rewrite your
solutions for those exercise sets, where possible, to make use of generics.
Unit 6 (Generics): Exercises page 5

READ ALSO...   examine your understanding of concepts
Order from Academic Writers Bay
Best Custom Essay Writing Services

QUALITY: 100% ORIGINAL PAPERNO PLAGIARISM – CUSTOM PAPER