Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

need help with sets

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


heres my attempt:

 
 
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's something I noticed:



That code can be simplified to this code:



Which isn't what you wanted to do, because it returns all of the keys. You only wanted to return a subset of the keys, didn't you?
 
Carey Brown
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
john besty
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:John, help us out here, how is 'skaters' defined?



so this is the skater class:



 
john besty
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:John, help us out here, how is 'skaters' defined?



Skaters is defined as:
Map<String, Skater> skaters = new HashMap<String, Skater>();
thanks for the help guys!
 
 
Sheriff
Posts: 7051
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think you need to roll through all keys, get the skater object from the value, then use getAgeCategory() to check if the skater is in range.
 
Carey Brown
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What's a year in metric? Do you know this metric stuff tiny ad?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic