Question

In: Computer Science

Use JAVA language. Using the switch method concept create a program in which you have an...

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.

Solutions

Expert Solution

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.


Related Solutions

Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
Use Visual Basic Language In this assignment you will need to create a program that will...
Use Visual Basic Language In this assignment you will need to create a program that will have both a “for statement” and an “if statement”. Your program will read 2 numbers from the input screen and it will determine which is the larger of the 2 numbers. It will do this 10 times. It will also keep track of both the largest and smallest numbers throughout the entire 10 times through the loop. An example of the program would be...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT