I followed Java in a Nutshell's "recipe for a high-quality equals method" (p. 33). To test its validity, I wrote a method to ensure that an instance of StringSet:
- Was equal to itself
- Was not equal to another class of object
- Was equal to another StringSet instance which contained identical strings and the same number of strings (and vice versa)
- Was not equal to a StringSet with a different number of strings
- Was not equal to a StringSet instance which did not contain identical strings
Additionally, the contract calls for consistency and for non-null to return false.
It is easy to test the hashCode function. Simply create two logically equivalent instances and check that they produce the same result.
One problem I had, which I haven't resolved, is with the second constructor, which takes a collection as a parameter, to be added to the StringSet. This should've been very simple, and I thought my code was correct, as no exceptions were thrown. I don't get any input mismatch errors, but the ArrayList I try to pass it cannot be accessed. Other than that, the program is working wonderfully.
No comments:
Post a Comment