This lab was written by Prof. Nathan Sprague.
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.
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
mainmethod that will probably cause the driver to crash when
largestStadiumis passed the
stadiumsarray. Find the problem and fix it.
- Complete the
largestStadiummethod 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 = new ObjectPair("Bridgeforth Stadium", 25000);
Notice that the formal and actual parameter types don’t match. The expected type of the second parameter is
Object(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”.
- Create a copy of the
Pair. 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
PairDriver. Refactor this driver so that it uses your
Pairclass. 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
Pairclass might be preferable to the
ObjectPairclass. Can you think of any situations where the
ObjectPairclass might be preferable?
Wildcards and Subclasses
Open up the following file in a simple text editor like
xed. (Don’t open it in Eclipse!)
Comment each assignment statement with either:
// C (For "Will compile.")
// 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.
CompileDriver.java through Canvas.