In: Computer Science
Using your solution or your instructor's solution from the Module 8 ungraded practice exercise, implement a unit test for the Pair class by adding a main method.
Create three small constituent classes (I created PeanutButter, Jelly, and Mustard classes) and insert various combinations of them into an array of Pair objects. Thoroughly test the equals(), hashCode(), and toString() methods from the Pair class with your main method.
Note: override the toString() method in each of your constituent classes so they are properly displayed in the output when printed from the Pair toString() method, otherwise only the hexadecimal Object representation will be displayed).
Sample output:
pair 0: [Peanut Butter, Jelly]
pair 0 hashcode: 2027766238
pair 1: [Peanut Butter, Mustard]
pair 1 hashcode: 1837727328
pair 2: [Peanut Butter, Peanut Butter]
pair 2 hashcode: 366712642
pair 3: [Peanut Butter, Peanut Butter]
pair 3 hashcode: 366712642
[Peanut Butter, Jelly] equals [Peanut Butter, Mustard]?
false
[Peanut Butter, Mustard] equals [Peanut Butter, Peanut Butter]?
false
[Peanut Butter, Peanut Butter] equals [Peanut Butter, Peanut
Butter]? true
Module 8 ungraded practice solution:
// Pair.java
// Aggregate (non-generic) pairs of arbitrary objects
// adapted from Mughal Ch. 8
public final class Pair {
private final Object first, second;
// construct a Pair object
public Pair(Object one, Object two) {
first = one;
second = two;
}
// return the first constituent object
public Object getFirst() { return first; }
// return the second constituent object
public Object getSecond() { return second; }
// return true if the pair of objects are identical
@Override
public boolean equals(Object other) {
boolean result;
if (this == other)
result = true;
else if (! (other instanceof Pair))
result = false;
else {
Pair otherPair = (Pair) other;
result = first.equals(otherPair.first) &&
second.equals(otherPair.second);
}
return result;
}
// return hash code for the aggregate pair
@Override
public int hashCode() {
// XOR hash codes to create a hashcode for the pair
// if the objects are equal, use one hashcode, otherwise
// the XOR result (and subsequent hashCode value) is 0.
return first.equals(second) ? first.hashCode() : first.hashCode() ^
second.hashCode();
}
// return textual representation of aggregated object
@Override
public String toString() {
return "[" + first + ", " + second + "]";
}
}
I am using your class Pair class. I am created three small constituent classes with name PeanutButter.java , Jelly.java , Mustard.java ....And also Created for testing with name Test.java
Note: I am created above 4 classses and your class all are created in Eclipse. and I have created package with name "Chapter_15",(all classes are created in this package)
**************************Java code**********************
package Chapter_15;
/**
* @author ammuarjun
* @version 1.0
* classes PeanutButter,Jelly,Mustard
*
*/
//Create three small constituent classes with name
PeanutButter,Jelly,Mustard
//along with the overridden toString() method
/*******************PeanutButter********************/
class PeanutButter {
@Override
public String toString() {
return "Peanut Butter";
}
}
/*******************Jelly********************/
class Jelly {
@Override
public String toString() {
return "Jelly";
}
}
/*******************Mustard********************/
class Mustard {
@Override
public String toString() {
return "Mustard";
}
}
/*******************Test********************/
public class Test {
public static void main(String[] args) {
// Create objects for those
small constituent classes
PeanutButter peanutButter = new
PeanutButter();
Jelly jelly = new Jelly();
Mustard mustard = new
Mustard();
// Create a pair
Pair pair = new Pair(peanutButter,
peanutButter);
/**
* Create an array of Pair
objects
* and initialize it with various
combinations
* of the objects of those
constituent classes.
* This can also be written in
multiple lines as:
* Pair[] array = new
Pair[11];
* array[0] = new Pair(peanutButter,
peanutButter);
* array[1] = new Pair(peanutButter,
jelly);
* array[5] = pair
* ...... and so on
*/
Pair[] array = new Pair[] {
new Pair(peanutButter, jelly),
new Pair(peanutButter, mustard),
pair,
new Pair(peanutButter, peanutButter),
new Pair(jelly, peanutButter),
new Pair(jelly, jelly),
pair,
pair,
new Pair(jelly, mustard),
new Pair(jelly, mustard),
new Pair(new PeanutButter(), new Mustard()),
new Pair(new PeanutButter(), mustard)
};
// Test toString() and hashCode()
methods
for (int i = 0; i <
array.length; i++) {
System.out.println("pair " + i + ": " + array[i]);
System.out.println("pair " + i + " hashcode: " +
array[i].hashCode());
}
// Test equals() method
for (int i = 1; i <
array.length; i++) {
System.out.println(array[i-1] + " equals " + array[i] + "? " +
array[i-1].equals(array[i]));
}
}
}
Output
pair 0: [Peanut Butter, Jelly]
pair 0 hashcode: 2027766238
pair 1: [Peanut Butter, Mustard]
pair 1 hashcode: 1837727328
pair 2: [Peanut Butter, Peanut Butter]
pair 2 hashcode: 366712642
pair 3: [Peanut Butter, Peanut Butter]
pair 3 hashcode: 366712642
pair 4: [Jelly, Peanut Butter]
pair 4 hashcode: 2027766238
pair 5: [Jelly, Jelly]
pair 5 hashcode: 1829164700
pair 6: [Peanut Butter, Peanut Butter]
pair 6 hashcode: 366712642
pair 7: [Peanut Butter, Peanut Butter]
pair 7 hashcode: 366712642
pair 8: [Jelly, Mustard]
pair 8 hashcode: 357842878
pair 9: [Jelly, Mustard]
pair 9 hashcode: 357842878
pair 10: [Peanut Butter, Mustard]
pair 10 hashcode: 1227423489
pair 11: [Peanut Butter, Mustard]
pair 11 hashcode: 607557415
[Peanut Butter, Jelly] equals [Peanut Butter, Mustard]? false
[Peanut Butter, Mustard] equals [Peanut Butter, Peanut Butter]?
false
[Peanut Butter, Peanut Butter] equals [Peanut Butter, Peanut
Butter]? true
[Peanut Butter, Peanut Butter] equals [Jelly, Peanut Butter]?
false
[Jelly, Peanut Butter] equals [Jelly, Jelly]? false
[Jelly, Jelly] equals [Peanut Butter, Peanut Butter]? false
[Peanut Butter, Peanut Butter] equals [Peanut Butter, Peanut
Butter]? true
[Peanut Butter, Peanut Butter] equals [Jelly, Mustard]? false
[Jelly, Mustard] equals [Jelly, Mustard]? true
[Jelly, Mustard] equals [Peanut Butter, Mustard]? false
[Peanut Butter, Mustard] equals [Peanut Butter, Mustard]?
false