|
This assignment extends what you wrote for Assignment 3.
First of all, make your Pair<T1,T2> class implement the parameterized Comparable<T> interface. That way, every Pair<T1,T2> object will be able to compare itself to another Pair<T1, T2> object of the exact same type (but not to a Pair object of a different type, say Pair<T3,T4> ). Notice that, in order for a Pair<T1,T2> object to be able to compare itself to another Pair<T1,T2> object, the objects contained in the Pairs must themselves be comparable. So you need to express the fact that types T1 and T2 must themselves implement the Comparable interface. Here is how two Pair objects should be compared. I will use the example of pairs of numbers, but the idea is the same for any other type of pairs. If one pair contains [a,b] and the other pair contains [c,d], the we say that [a,b] equals [c,d] if a=c and b=d. We say that [a,b] < [c,d] if a<c or b<d. Notice that this means that [a,b] > [c,d] if a >= c and b >= d and [a,b] is not equal to [c,d].
Add a method with the following signature to your Pair<T1,T2> class.
public Pair<T1,T2> maxPair( Pair<T1,T2> p )
What this method should do is return a new Pair object where the first entry in the new Pair is the maximum of the two original first items (that is, the first item in the this object and the first item in the p object). And the second item in the new Pair object should be the maximum of the two original second items.
Have your Pair<T1,T2> class override the
public boolean equals(Object o)
method from the Object class. Your version of equals() should return true when the argument o is not null and the argument o is a Pair object and the items in o are equal to the items in the this pair.
Write a method called replaceFirst that takes a single argument that is a reference to an object and returns a new Pair object where the first item in the new pair is the argument object and the second item in the new pair is the second item from the this object. This method will be a "generic method" in that it will need its own "local" type parameter (a type parameter different from the T1 and T2 type parameters at the beginning of the Pair class). Write a similarly define replaceSecond method. (Note: The "local type" needed by this method must be a Comparable type since it describes an object that will be put in a Pair object. This means that the local type must be a "bounded type".)
Finally, write a static method called addPairOfNumbers that takes two arguments that are each Pairs with both entries being subclasses of the Number class. The method should return a Pair<Double,Double> object where each entry is the sum of the corresponding entries from the two arguments. This method will need its own "local" type parameters in order to specify that the arguments must be Pairs of subclasses of Number .
In the zip file along with this file there is a program TestPairs.java that tests your new implementation of the Pair class. Do not make any changes to the TestPairs.java file. If the TestPairs.java program does not compile correctly, then you need to fix something in your implementation of Pair . When the TestPairs.java program runs, it should produce output like that contained in the file SampleOutput.txt .
Turn in a zip file called CS275Hw4Surname.zip containing your Java source file, Pair.java , and also the original files contained in h4.zip . This assignment is due Wednesday, October 3.
Here are a few references to Java generics that may help you.
|
|