Question: When overriding the equals() method, programmers are also required to override thehashCode() method; otherwise clients cannot store instances of these objects in commonCollection structures such

When overriding the equals() method, programmers are also required to override thehashCode() method; otherwise clients cannot store instances of these objects in commonCollection structures such as HashSet. For example, the Point class from Chapter 1 is defective in this regard.
(a)Demonstrate the problem with Point using a HashSet.
(b)Write down the mathematical relationship required between equals() and hashCode().
(c)Write a simple JUnit test to show that Point objects do not enjoy this property.
(d)Repair the Point class to fix the fault.
(e)Rewrite your JUnit test as an appropriate JUnit theory. Evaluate it with suitableDataPoints.
class Point private int x; private int y: public Point (int x, int y) { this.x-x; this.y-y:) @Override public boolean equals (Object o) // Location A if (!o instanceof Point)) return false; Point p (Point) o: return (p.x-this.x) && (p.y-- this.y): class ColorPoint extends Point private Color color: // Fault: Superclass instantiable; subclass state extended public ColorPoint (int x, int y, Color color) super (x.y): this.color-color
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
