Programming Assignment 2
CS 27500
Data Structures
Fall, 2021

This assignment is due Tuesday, November 9.

This assignment makes use of the files contained in this zip file.

This assignment is based on the content of the file Generic_Classes.pdf from the zip file.

You are to implement a generic Pair class. Your Pair class should be defined as Pair<T1, T2> and it should contain two (private) instance 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 instance 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 instance fields of the item being constructed at the two objects contained in the given Pair object (this is a "shallow" copy constructor).

Pages 5 - 7 of the file Generic_Classes.pdf show you how to get started with writing Pair.java. Also in the zip file is a Javadoc file, Pair.html, that can help you to understand the description of Pair.java given in this document.

Your Pair class should contain appropriate getFirst(), getSecond(), setFirst(), and setSecond() methods. The setFirst() and setSecond() methods should not be void methods, they should return a reference to the current object (so that other method calls can be chained).

There should 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.

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" (pages 8 - 9 in Generic_Classes.pdf) 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). The reason for the new type parameter is that the first element of the new Pair need not be of the same type as the first element of this pair.

Write a similarly defined replaceSecond() method.

Your Pair class should also contain an appropriate toString() method.

In the zip file there is a program TestPairs_v1.java that tests your implementation of the Pair class. Do not make any changes to the TestPairs_v1.java file. The TestPairs_v1.java program will compile correctly when you have completed your implementation of the Pair class. If the TestPairs_v1.java program does not compile correctly, then you need to fix something in your implementation of Pair. When the TestPairs_v1.java program runs, it prints out information about what is wrong if a test fails.

In the zip file there is another version of the test program, TestPairs_v2.java. Do not make any changes to this file either. The TestPairs_v2.java program runs tests similar to TestPairs_v1.java. When the TestPairs_v2.java program runs, it should produce output exactly like that contained in the file TestPairs_v2_output.txt.

Don't try to write the whole Pair class and then test it using TestPairs_v1.java. You should start to develop incremental strategies for writing and testing code. Writing all of the code and then testing it is not a good strategy. As you are implementing the Pair class, you should test each completed method before you go on to another method. You should write your own test class to which you can incrementally add test code as you implement more methods from the Pair class. For example, you should implement the Pair constructors first and make sure they compile and can be used. Then implement the toString() method so that you can "print out" the Pair objects you just constructed. Then start implementing other methods. Use the toString() method to print out Pair objects to see if each method does the correct thing to them.

When you have finished implementing the Pair class, in a text file called Explanation.txt write an explanation of exactly what the statement on line number 32 from TestPairs_v2.java does. In particular, explain how many objects are created by that line of code and what happens to each one. Be sure to read the line of code carefully and explain all of the steps that it executes.

Also write an explanation of what the statement on line number 36 from TestPairs_v2.java does.

Your two explanations should explain in detail what every part of each statement does. Do not write brief, one or two sentence, explanations.

Turn in a zip file called CS275Hw2Surname.zip (where Surname is your last name) containing your version of Pair.java and your text file Explanation.txt containing explanations of the two statements from TestPair_v2.java.

This assignment is due Tuesday, November 9.