Question
The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.).The class uses a HashMap to map a
TheSongsDatabaseclass keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.).The class uses a HashMap to map a genre with a set of songs that belong to such a genre.The set of songs will be represented using a HashSet. You must implement the following:
- Provide a definition for the required map. This definition would appear where you see the comments
// You must provide the initialization
- Implement theaddSongThis method adds a song to the set associated with the specified genre.If there is not set associated with the genre, one will be added to the map.
- Implement thegetGenreOfSong. The method will return the genre for the specified song or null if the song is not part of the database.
public class SongsDatabase {
// You must provide the initialization
private Map
public void addSong(String genre, String songTitle) {
// You must implement this method
// [2] Add your code here
}
public String getGenreOfSong(String songTitle) {
// You must implement this method
// [3] Add your code here
}
}
The following are map methods that you may find helpful for this problem:
- Vget(Objectkey) - Returns the value to which this map maps the specified key.
- Vput(Kkey,Vvalue) - Associates the specified value with the specified key in this map.
- Set
keySet() - Returns a set view of the keys contained in this map.
The following are set methods that you may find helpful for this problem:
- booleancontains(Objecto) - Returns true if this set contains the specified element.
- booleanadd(Eo) - Adds the specified element to this set if it is not already present
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started