Labs
In these labs we will practice using equals and hashCode.
- The
RGBColorclass has three integer fields. Add anequalsmethod toRGBColorso the tests shown inTestColorcome out correctly based on the values of the three integer fields.
(Solution: RGBColor.java)
- In
RGBColor, implement thehashCodemethod. Starting with a prime number like17, multiply the prime number by itself and each of the three field values, adding the result back to the prime after each multiplication.
(Solution: RGBColor2.java)
- The
Triangleclass has twointfields,baseandheight. Write anequalsmethod for it so the tests inTestTrianglecome out correctly.
(Solution: Triangle.java)
- The
ColorTriangleclass extendsTriangle, adding a field of typeRGBColor. GiveColorTriangleanequalsmethod that invokesTriangle'sequalsto checkbaseandheight, and also usesRGBColor'sequalsto compare thecolorfield.
(Solution: ColorTriangle.java)
- In
Triangle, comment out yourequalsmethod. In the Eclipse Source menu, use Generate hashCode() and equals() to see Eclipse's default implementations for these methods. Do the same with yourColorTriangleclass.
(Solution: Triangle2.java, ColorTriangle2.java)