Skip to main content

Practice · 1 of 3

Write the equals/hashCode Pair

CHALLENGE
Difficulty: intermediate+25 XP

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.

Back to lesson: Practice: Contract Lab