So this is the part of the Unit test:
public class LocationTester {
private Floor seventhFloor;
private int size = 10;
@Before
public void setup() {
size = 10;
seventhFloor = new Floor(7, size);
}
@Test
public void testLocationEquality() {
Location l1 = new Location(seventhFloor, 3, 3);
Location l2 = new Location(new Floor(7, size), 3, 3);
assertTrue(l1.equals(l2));
}}
Here's the part of Location class with method equals:
class Location {
private Floor floor;
private int size, position;
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj instanceof Location) {
return false;
}
return equals((Location) obj);
}
public boolean equals(Location loc) {
return floor.equals(loc.floor) && position == loc.position && size == loc.size;
}
And here's the part of Floor class:
class Floor {
private int number;
public boolean equals(Floor floor) {
return number == floor.number;
}
I don't know why equals method with parameter (Object obj) and typecasting is put when "new Location" at " Location l2 = new Location(new Floor(7, size), 3, 3);" is actually the object of Location itself. Why is it needed to put the equals(Object obj).
The test runs fine without the method equals(Object obj).
Not to mention, there is no subclass of Location in this project. So why put typecasting at "return equals((Location) obj);" anyway?