In: Computer Science
Use JAVA language.
Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs.
Then alter the script to achieve the following in each test run:
a) Print all the songs.
b) Print only song 15.
c) Print only song 19.
The full code is below, comment or uncomment the calls to printSongs(listOfSongs, artist, 0), printSongs(listOfSongs, artist, 15) and printSongs(listOfSongs, artist, 19) depending on the testcase you wish to execute. Please note that the ask says to add 18 songs, whereas the test run in c) asks to print only song #19. Hence, I have added 19 songs in total for this case to execute successfully. If this was not meant, please let me know and I will modify the code accordingly.
package myStore;
import java.util.*;
public class PickSongs {
private static void printSongs(HashMap<Integer,
String> list, String artist, Integer choice) {
/*
* Based on the choice given, print the song from the
list
* If choice is not in the range [1, 19], print all the
songs.
*/
switch(choice){
case 1:
System.out.println("----- The 1st song is: " +
list.get(1));
break;
case 2:
System.out.println("----- The 2nd song is: " +
list.get(2));
break;
case 3:
System.out.println("----- The 3rd song is: " +
list.get(3));
break;
case 4:
System.out.println("----- The 4th song is: " +
list.get(4));
break;
case 5:
System.out.println("----- The 5th song is: " +
list.get(5));
break;
case 6:
System.out.println("----- The 6th song is: " +
list.get(6));
break;
case 7:
System.out.println("----- The 7th song is: " +
list.get(7));
break;
case 8:
System.out.println("----- The 8th song is: " +
list.get(8));
break;
case 9:
System.out.println("----- The 9th song is: " +
list.get(9));
break;
case 10:
System.out.println("----- The 10th song is: " +
list.get(10));
break;
case 11:
System.out.println("----- The 11th song is: " +
list.get(11));
break;
case 12:
System.out.println("----- The 12th song is: " +
list.get(12));
break;
case 13:
System.out.println("----- The 13th song is: " +
list.get(13));
break;
case 14:
System.out.println("----- The 14th song is: " +
list.get(14));
break;
case 15:
System.out.println("----- The 15th song is: " +
list.get(15));
break;
case 16:
System.out.println("----- The 16th song is: " +
list.get(16));
break;
case 17:
System.out.println("----- The 17th song is: " +
list.get(17));
break;
case 18:
System.out.println("----- The 18th song is: " +
list.get(18));
break;
case 19:
System.out.println("----- The 19th song is: " +
list.get(19));
break;
default:
// Print the entire playlist
System.out.println("----- The playlist of " +
artist + " -----");
for(Map.Entry<Integer, String>
m:list.entrySet()) {
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
public static void main(String args[]) {
String artist = "Madonna";
// Create a hashmap for list of
songs to do a faster search of the songs
HashMap<Integer, String>
listOfSongs = new HashMap<Integer, String>();
// Add 19 songs in the
listOfSongs
listOfSongs.put(1, "La Isla
Bonita");
listOfSongs.put(2, "Vogue");
listOfSongs.put(3, "Take A
Bow");
listOfSongs.put(4, "Papa Don't
Preach");
listOfSongs.put(5, "Frozen");
listOfSongs.put(6, "Hung
Up");
listOfSongs.put(7, "Into The
Groove");
listOfSongs.put(8,
"Borderline");
listOfSongs.put(9, "4
Minutes");
listOfSongs.put(10, "Express
Yourself");
listOfSongs.put(11, "Live To
Tell");
listOfSongs.put(12, "The Power of
Good-Bye");
listOfSongs.put(13, "Dress You
UP");
listOfSongs.put(14, "Burning
Up");
listOfSongs.put(15, "Don't Cry for
Me Argentina");
listOfSongs.put(16, "This Used to
Be My Playground");
listOfSongs.put(17,
"Cherish");
listOfSongs.put(18,
"Holiday");
listOfSongs.put(19, "Beat Goes
On");
// a) If user gives any number that
is not in the range [1,19],
// the default case of printing all
the songs will be executed
printSongs(listOfSongs, artist,
0);
// print only 15th song
// printSongs(listOfSongs, artist,
15);
// print only 19th song
// printSongs(listOfSongs, artist,
19);
}
}
IDE screenshots to help with indentation and syntax-highlighting:
----------------------------------------------
Test run a): To print all the songs, comment the other two function calls in main(), save the program and run.
Test run b): To print only 15th song, leave only the second function call uncommented and comment the other two function calls in main(), save the program and run.
Test run c): To print only 19th song, leave only the third function call uncommented and comment the other two function calls in main(), save the program and run.