Question

In: Computer Science

Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...

  1. Write a Java program that implements a song database. 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 genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below.




public class SongsDatabase {

private Map<String, Set<String>> genreMap =

new HashMap<String, Set<String>>();

    public void addSong(String genre, String songTitle) {

        // You must implement this method

    }

    public Set<String> getSongs(String genre) {

        // You must implement this method

    }

    public String getGenreOfSong(String songTitle) {

        // You must implement this method

    }

}

So, your hashMap, visually, looks like the following:

Soul -> (SoulSong1, SoulSong2, SoulSong3, etc.)

Rock -> (RockSong1, RockSong2, RockSong3, etc.)

Jazz -> (JazzSong1, JazzSong2, JazzSong3, etc.)

Solutions

Expert Solution

Code:

import java.util.*;

public class SongsDatabase {

    private Map<String, Set<String>> genreMap = new HashMap<String, Set<String>>();
    
        public void addSong(String genre, String songTitle) {
    
            // You must implement this method
            if(genreMap.containsKey(genre)){
                genreMap.get(genre).add(songTitle);//adding songTitle with genre
            }else{
                //making a new set when the genre does not exist and adding it to the hashmap
                Set<String> newSet = new HashSet<String>();
                newSet.add(songTitle);
                genreMap.put(genre,newSet);
            }
    
        }
    
        public Set<String> getSongs(String genre) {
    
            // You must implement this method
            return genreMap.get(genre);//returning the set corresponding to the genre
    
        }
    
        public String getGenreOfSong(String songTitle) {
    
            // You must implement this method
            for (Map.Entry<String, Set<String>> entry : genreMap.entrySet()) {
                
                if(entry.getValue().contains(songTitle)){
                    return entry.getKey();
                }
            
            }

            return "No value found";
    
        }

        public static void main(String args[]){
            SongsDatabase db = new SongsDatabase();

            //adding songs
            db.addSong("Soul","SoulSong1");
            db.addSong("Soul","SoulSong2");
            db.addSong("Soul","SoulSong3");
            db.addSong("Rock","RockSong1");
            db.addSong("Rock","RockSong2");
            db.addSong("Rock","RockSong3");
            db.addSong("Jazz","JazzSong1");
            db.addSong("Jazz","JazzSong2");

            //getting songs 
            Set<String> set = db.getSongs("Rock");
            Iterator value = set.iterator();
            System.out.println("Songs in Rock genre:");
            while(value.hasNext()){
                System.out.println(value.next());
            }

            System.out.print("Getting genre of the song SoulSong3 : ");
            System.out.println(db.getGenreOfSong("SoulSong3"));
        }
    
}



Output:

Code screenshot:


Related Solutions

Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
IN JAVA: Write a parent class, Device, which implements ageneric computer device.Create two child...
IN JAVA: Write a parent class, Device, which implements a generic computer device.Create two child classes, Disk and Printer, which specialize a Device with added functionality.Device Task:We are only interested in three things for the time being: the name of the device, an ID number identifying the device, and a flag indicating whether or not it is enabled. Thus, three fields are necessary, a String for the name, a int for the ID, and a boolean for the enabled status.Any...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT