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...
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...
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...
in java Write a contacts database program that presents the user with a menu that allows...
in java 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 prompts...
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...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
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...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT