Skip to main content

Practice ยท 1 of 3

Write the equals/hashCode Pair

Implement a Color value class inside Solution: - public constructor Color(int r, int g, int b) storing fields as final - equals(Object) returning true only for another Color with identical r, g, b - hashCode() consistent with equals (use Objects.hash(...)) - toString() returning Color[r=...,g=...,b=...] The tests put two equal Colors into a HashSet and expect the set to treat them as one element โ€” the classic contract check.

Difficulty: intermediate

Back to lesson: Practice: Contract Lab