This lab was written by Prof. Nathan Sprague.
Introduction
The goal of this lab is to explore the use of Java Generics by developing a Pair
class that represents a 2-tuple. (Recall that a tuple is a finite ordered sequence of elements. A 2-tuple contains two elements, a 3-tuple contains three elements, an n-tuple contains n elements.)
A Pair class could be useful any time we need to store data that naturally occurs as an ordered pair: first and last name, two-dimensional coordinates, etc.
The challenge in designing this class is that we want to avoid specifying the element types in advance. The goal is to develop a single Pair
class that may be used to store pairs of objects of any type.
Exercises
Object
elements
One possible design involves programming the Pair
class so that the types of the two elements are declared as Object
. Since every reference type in Java inherits from Object
, this approach will give us the flexibility we want.
Open the following two classes in your favorite IDE:
Take a few minutes to read the code, then complete the steps below.
- The provided code compiles, but there is a logic error in the
main
method that will probably cause the driver to crash whenlargestStadium
is passed thestadiums
array. Find the problem and fix it. - Complete the
largestStadium
method so that it conforms to the Javadoc comments. Test to make sure that it works as expected. (HINT: You will need to perform some casts when you retrieve the items from the tuple.) - BONUS QUESTION: Why does this line of code compile?
stadiums[0] = new ObjectPair("Bridgeforth Stadium", 25000);
Notice that the formal and actual parameter types don’t match. The expected type of the second parameter isObject
(a reference type) and the provided argument is 1 (a primitive value). If you don’t know the answer to this question Google the term “Autoboxing”.
Generics
- Create a copy of the
ObjectPair
class namedPair
. Refactor this class to use Java generics. Your updated class should make it possible to independently specify the types of the first and second elements. - Create a copy of the
ObjectPairDriver
class namedPairDriver
. Refactor this driver so that it uses yourPair
class. The functionality should be unchanged. The resulting code should not include any cast operations. - BONUS QUESTION: What happens if you re-introduce the problem that you fixed in step 1 from the previous section? Will the resulting code compile? Why do you think generic collection classes are sometimes called “type-safe” collections?
- BONUS QUESTION: List some reasons that the
Pair
class might be preferable to theObjectPair
class. Can you think of any situations where theObjectPair
class might be preferable?
Wildcards and Subclasses
Open up the following file in a simple text editor like vim
, gedit
or xed
. (Don’t open it in Eclipse!)
Comment each assignment statement with either:
// C (For "Will compile.")
or
// N (For "Will not compile.")
For those lines that will not compile, include an explanation of the problem. Once you have finished all of the statements, check your answers by attempting to compile the file.
Submitting
Submit Pair.java
, PairDriver.java
and CompileDriver.java
through Canvas.