Hey guys im trying to create a method that returns a set that contains objects and im getting a bit turned around any chance you can help with this?
heres the question i have to complete:
Write a public instance method called findSkatersInAgeCat() for the IceRink class which takes a char argument and returns an unordered set of Skater objects.
The method should loop through skaters looking for instances of Skater whose ageCategory matches the given argument.
As such instances are found, the method should add these instances of Skater to the set that will be returned.
Two things I noticed right away, your method says it returns a Skater and not a set of Skaters. And the other is the return only happens when a condition is met. What's supposed to happen when the condition is not met?
Carey Brown wrote:I'm assuming that 'ageCat' needs to be used as a filter, so I think there may be a bit more to it than that.
Yes, that's not everything. But if this undocumented skaters object is a Map whose key is age category, then you'd think it could only contain at most one value for each age category. And if we assume the values in that map are Skater objects (we also don't know that), then only one of them can be in each age category. Which kind of conflicts with the instructions which suggest that it's possible to have more than one Skater in the set which is returned.
Note that in order for a Set to work properly, Skater must override equals() and hashCode() methods so that the Set can only store unique Skater instances. If you don't really require that the Skaters be unique, then return a List instead of a Set.