I was given a task and I'm not sure how to get it to work. All the test cases must pass.
Assertions.assertDoesNotThrow(() -> p3.equals(null));
Assertions.assertFalse(p3.equals(null), "equals to null should return false");
I figured out the first one, the one that is tricking me is the Assertions.assertfalse(p3.equals(null)). I created the object p3 from Point as such:
Point p3 = new Point(x+1, y-1);
In my Point class I have an equals method which I override as such:
@Override
public boolean equals(Object obj) {
Point p = (Point) obj;
try {
if (x != p.x) {
return false;
}
if (y != p.y) {
return false;
}
} catch (NullPointerException e) {
System.err.println("null");
}
return true;
}
I used this to pass other tests not mentioned.
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int x() {
return x;
}
public int y() {
return y;
}
Where x and y are random integers between 0-30.
When I run the test, it always returns p3 equal to null which is confusing me. I know it is something to do with checking if the object passed through is of same type (Point) & downcasting, but I am not sure.