updated code/tests based on feedback (#676)

This commit is contained in:
Nancy Bosecker 2016-09-07 21:39:27 -07:00 committed by Grzegorz Piwowarek
parent 113afd40d4
commit 15d45cffd6
8 changed files with 236 additions and 246 deletions

View File

@ -8,8 +8,7 @@ public class ComplexClass {
private ArrayList<?> genericArrayList; private ArrayList<?> genericArrayList;
private HashSet<Integer> integerHashSet; private HashSet<Integer> integerHashSet;
public ComplexClass(ArrayList<?> genericArrayList, public ComplexClass(ArrayList<?> genericArrayList, HashSet<Integer> integerHashSet) {
HashSet<Integer> integerHashSet) {
super(); super();
this.genericArrayList = genericArrayList; this.genericArrayList = genericArrayList;
this.integerHashSet = integerHashSet; this.integerHashSet = integerHashSet;
@ -19,11 +18,8 @@ public class ComplexClass {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime result = prime * result + ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
* result result = prime * result + ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
+ ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
result = prime * result
+ ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
return result; return result;
} }

View File

@ -42,11 +42,9 @@ public class Rectangle extends Shape {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Rectangle other = (Rectangle) obj; Rectangle other = (Rectangle) obj;
if (Double.doubleToLongBits(length) != Double if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
.doubleToLongBits(other.length))
return false; return false;
if (Double.doubleToLongBits(width) != Double if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
.doubleToLongBits(other.width))
return false; return false;
return true; return true;
} }

View File

@ -11,6 +11,9 @@ public class Square extends Rectangle {
this.color = color; this.color = color;
} }
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@ -19,20 +22,28 @@ public class Square extends Rectangle {
return result; return result;
} }
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) {
return true; return true;
if (!super.equals(obj)) }
if (!super.equals(obj)) {
return false; return false;
if (getClass() != obj.getClass()) }
if (!(obj instanceof Square)) {
return false; return false;
}
Square other = (Square) obj; Square other = (Square) obj;
if (color == null) { if (color == null) {
if (other.color != null) if (other.color != null) {
return false; return false;
} else if (!color.equals(other.color)) }
} else if (!color.equals(other.color)) {
return false; return false;
}
return true; return true;
} }

View File

@ -14,21 +14,16 @@ public class ComplexClassTest {
ArrayList<String> strArrayList = new ArrayList<String>(); ArrayList<String> strArrayList = new ArrayList<String>();
strArrayList.add("abc"); strArrayList.add("abc");
strArrayList.add("def"); strArrayList.add("def");
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67)); ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67)); ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
ComplexClass cObject = new ComplexClass(strArrayList, new HashSet<Integer>(45,67));
ArrayList<String> strArrayListD = new ArrayList<String>(); ArrayList<String> strArrayListD = new ArrayList<String>();
strArrayListD.add("lmn"); strArrayListD.add("lmn");
strArrayListD.add("pqr"); strArrayListD.add("pqr");
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45,67)); ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
// equals() // equals()
Assert.assertTrue(aObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject)); Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject));
Assert.assertTrue(bObject.equals(cObject));
Assert.assertTrue(aObject.equals(cObject));
// hashCode() // hashCode()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode()); Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode() // non-equal objects are not equals() and have different hashCode()

View File

@ -10,15 +10,10 @@ public class PrimitiveClassTest {
PrimitiveClass aObject = new PrimitiveClass(false, 2); PrimitiveClass aObject = new PrimitiveClass(false, 2);
PrimitiveClass bObject = new PrimitiveClass(false, 2); PrimitiveClass bObject = new PrimitiveClass(false, 2);
PrimitiveClass cObject = new PrimitiveClass(false, 2);
PrimitiveClass dObject = new PrimitiveClass(true, 2); PrimitiveClass dObject = new PrimitiveClass(true, 2);
// equals() // equals()
Assert.assertTrue(aObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject)); Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject));
Assert.assertTrue(bObject.equals(cObject));
Assert.assertTrue(aObject.equals(cObject));
// hashCode() // hashCode()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode()); Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode() // non-equal objects are not equals() and have different hashCode()

View File

@ -12,16 +12,11 @@ public class SquareClassTest {
Square aObject = new Square(10, Color.BLUE); Square aObject = new Square(10, Color.BLUE);
Square bObject = new Square(10, Color.BLUE); Square bObject = new Square(10, Color.BLUE);
Square cObject = new Square(10, Color.BLUE);
Square dObject = new Square(20, Color.BLUE); Square dObject = new Square(20, Color.BLUE);
// equals() // equals()
Assert.assertTrue(aObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject)); Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
Assert.assertTrue(aObject.equals(bObject));
Assert.assertTrue(bObject.equals(cObject));
Assert.assertTrue(aObject.equals(cObject));
// hashCode() // hashCode()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode()); Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode() // non-equal objects are not equals() and have different hashCode()