CS 275 - Programming Assignment 3

This assignment is based on the material from Section 2.5.2 (pages 89-90) from the course textbook.

You are to implement a generic Pair class similar to, but not exactly like, Code Fragment 2.15 on page 89 of the course textbook.

Your Pair class should be defined as Pair<T1, T2> and it should contain two (private) fields called first (of type T1) and second (of type T2). Your Pair class should contain three constructors. There should be a default constructor that sets both fields to null. There should be a two item constructor that takes references to objects of type T1 and T2 and points the two fields at these two objects. And there should be a one item constructor that takes a reference to a Pair object of the same type as the one being constructed and points the fields of the item being constructed at the two objects contained in the given Pair object.

Your Pair class should contain appropriate getFirst, setFirst, getSecond and setSecond methods. And there should also be a method called transpose that returns a reference to a new Pair object of type Pair<T2,T1> where the objects in the new Pair are the same as the objects in the current pair, but in the opposite order.

You Pair class should also contain an appropriate toString method.

In the zip file along with this file there is a program TestPairs.java that tests your implementation of the Pair class. Do not make any changes to the TestPairs.java file. The TestPairs.java program will compile correctly when you have completed your implementation of the Pair class. 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.

When you have finished implementing the Pair class, in a text file write an explanation of exactly what the statement on line number 31 from TestPairs.java does. Also write an explanation of what the statement on lines numbered 35-36 does. Your two explanations should explain in detail what every part of each statement does.

Turn in a zip file called CS275Hw3Surname.zip containing your Java source file, Pair.java, a text file containing your explanations of the two statements from TestPair.java, and also the original files contained in h3.zip. This assignment is due Friday, September 21.

Here are a few references to Java generics that may help you.


Return to the CS 275 home page.


compliments and criticisms