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 a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files. A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions: 1) setName to set the name member variable with a string...
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT