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

View File

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

View File

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

View File

@ -16,7 +16,6 @@ public class ComplexClassTest {
strArrayList.add("def");
ComplexClass aObject = 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>();
strArrayListD.add("lmn");
@ -24,11 +23,7 @@ public class ComplexClassTest {
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
// equals()
Assert.assertTrue(aObject.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()
Assert.assertTrue(aObject.hashCode() == bObject.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 bObject = new PrimitiveClass(false, 2);
PrimitiveClass cObject = new PrimitiveClass(false, 2);
PrimitiveClass dObject = new PrimitiveClass(true, 2);
// equals()
Assert.assertTrue(aObject.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()
Assert.assertTrue(aObject.hashCode() == bObject.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 bObject = new Square(10, Color.BLUE);
Square cObject = new Square(10, Color.BLUE);
Square dObject = new Square(20, Color.BLUE);
// equals()
Assert.assertTrue(aObject.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()
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
// non-equal objects are not equals() and have different hashCode()