Question

This is a Java text only program. This program will ask if the user wants to...

This is a Java text only program. This program will ask if the user wants to create a music playlist. If user says he or she would like to create this playlist, then the program should first ask for a name for the playlist and how many songs will be in the playlist. The user should be informed that playlist should have a minimum of 3 songs and a maximum of 10 songs. Next, the program will begin a loop to allow the user to enter the song information for the specified number of songs. The user should be asked to enter the name of the song, the artist, the song duration, and the song genre, for example Pop, Rock, Rap, Country, Hip-Hop, Jazz, Classical, Foreign Songs. After all of the songs have been entered, the program should display a list of all of the information and ask which or if any of the entries need to be corrected. If the user indicates a particular song, then the program should let the user re-enter and replace the data for that particular song. If the user indicates that no songs need to be corrected, then the program will display the final list of songs that are displayed, it should also show the total duration of the playlist, a count of songs in each genre, and the average song duration.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The screenshots of Java code for the given problem (copyable code also included below) are:

Although the code is well-commented and all the variables used are self-explanatory, here is a brief explanation of the code:

  • To store all the information of the song, a separate class called "song" is being made. It contains a method to add a song (i.e., to initialize the data members or properties like name, artist, etc. of the song), getter methods that return genre and duration (as they are needed in the main() method), and a method that displays the song info.
  • In the main() method, the program first asks if the user wants to create a playlist. If not, then the program ends.
  • If yes, then the program asks for the name of the playlist and the number of songs to be added in the playlist.
  • Then, an array of objects of the Song class is made with size equal to the number of songs. Each object will represent a particular song.
  • One by one, all the objects are initialized inside a for loop.
  • After adding all the songs, the final list is displayed. The program then asks the user if any of the entries need to be corrected. If not, then the above displayed list is the final list.
  • If yes, then the user is asked the number of the particular song that needs to be modified. Then, the new information for that song is asked from the user and the final list after modification is displayed on the screen.
  • Now is the time when we will need the duration and genre of each song to calculate the total duration, average duration, and number of songs in each genre (genre count).
  • To get the duration and genre for each song, we will use the getDuration() and getGenre() methods of the Song class. This is why we created them in the first place.
  • For finding out the genre count, a hashmap is used with the genre names as the keys and the number of songs of that genre as its value.
  • All the calculated values are finally displayed.

The copyable code is given below:

import java.util.*;

class Song

{

String name, artist, genre;

int duration; //in seconds

public void addSong()

{

Scanner sc = new Scanner(System.in);

System.out.print("Name: ");

name = sc.nextLine();

System.out.print("Artist: ");

artist = sc.nextLine();

System.out.print("Duration (in seconds): ");

duration = sc.nextInt();

System.out.print("Genre: ");

genre = sc.next();

}

public String getGenre()

{

return genre;

}

public int getDuration()

{

return duration;

}

public void displaySongInfo()

{

System.out.println("Name: " + name);

System.out.println("Artist: " + artist);

System.out.println("Duration (seconds): " + duration);

System.out.println("Genre: " + genre);

}

}

public class Main

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int n; //number of songs

String playlist; //name of the playlist

char choice; //variable to indicate "Yes" or "No" entered by user

System.out.print("Do you want to create a playlist? (Y/N) ");

choice = sc.next().charAt(0);

if(choice=='Y' || choice=='y')

{

System.out.print("Enter a name for the playlist: ");

playlist = sc.next();

System.out.print("How many songs do you want in the playlist (min 3 max 10)? ");

n = sc.nextInt();

Song s[] = new Song[n]; //n objects of Song class, one for each song

for(int i=0; i<n; i++)

{

System.out.println("\nEnter the information for song " + (i+1));

s[i] = new Song();

s[i].addSong();

}

System.out.println("\nAll the songs added successfully.");

System.out.println("Here is the list of them: ");

//Displaying all the songs

for(int i=0; i<n; i++)

{

System.out.println("\nSong " + (i+1));

s[i].displaySongInfo();

}

System.out.print("\nDo you want to modify any song info? (Y/N) ");

choice = sc.next().charAt(0);

if(choice=='Y' || choice=='y') //Modify a particular song

{

System.out.print("Enter the song number that you want to modify: ");

int songNumber = sc.nextInt();

System.out.println("Here is the information for song " + songNumber);

s[songNumber-1].displaySongInfo();

System.out.println("\nEnter the new information for song " + songNumber);

s[songNumber-1].addSong();

System.out.println("Song modified successfully! The final playlist is:");

for(int i=0; i<n; i++)

{

System.out.println("\nSong " + (i+1));

s[i].displaySongInfo();

}

}

else //When the user doesn't want to modify any song

{

System.out.println("Okay! So the above playlist is final.");

}

int totalDuration = 0; //Total Duration of the playlist

double avgDuration; //Average song duration

for(int i=0; i<n; i++)

totalDuration += s[i].getDuration();

avgDuration = (double)totalDuration / (double)n;

System.out.println("\nThe total duration of the playlist is " + totalDuration + " seconds.");

System.out.println("Average song duration = " + avgDuration + " seconds");

HashMap<String, Integer> genreCount = new HashMap<>(); //To store the count of each genre

String genre;

for(int i=0; i<n; i++)

{

genre = s[i].getGenre(); //Genre of current song

if(genreCount.containsKey(genre))

genreCount.replace(genre, genreCount.get(genre)+1); //If present, increment count

else

genreCount.put(genre, 1); //If not present, insert, with count 1

}

System.out.println("Number of songs in each genre:");

System.out.println(genreCount);

}

sc.close();

}

}

The screenshots of the output are:

Hope it helped. If you have any doubts or queries, please feel free to ask in the comments section. If it helped in any way, please consider giving a thumbs up as it takes a lot of time and effort to write such detailed answers.

Add a comment
Know the answer?
Add Answer to:
This is a Java text only program. This program will ask if the user wants to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Introduction In this final programming exercise, you'll get a chance to put together many of the...

    Introduction In this final programming exercise, you'll get a chance to put together many of the techniques used during this semester while incorporating OOP techniques to develop a simple song playlist class. This playlist class allows a user to add, remove and display songs in a playlist. The Base Class, Derived Class and Test Client Unlike the other PAs you completed in zyLabs, for this PA you will be creating THREE Java files in IntelliJ to submit. You will need...

  • Hi i need Help. i use MYSQL command line. THE QUESTION ARE BELOW please check the...

    Hi i need Help. i use MYSQL command line. THE QUESTION ARE BELOW please check the answer before you submit because sometimes query gives error thank you For Full database of SQL you can DOWNLOAD from this link: https://drive.google.com/file/d/1xh1TcBfMtvKoxvJr7Csgnts68fF53Q1t/view?usp=sharing ------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- What are the total sales for each year. Note that you will need to use the YEAR function. Order by year with the newest year first. ------------------------------------------------------- How many employees have no customers? ------------------------------------------------------------------ List the total sales for...

  • SQL Create a database called MyMusician Create three tables, one named “User”, one named “Song”, and...

    SQL Create a database called MyMusician Create three tables, one named “User”, one named “Song”, and one named “Playlist”. The “User” table should have a column for userID (integer, autoincrement, not null, primary key), username, and password. It should also have one column for a foreign key we will assign later called “playlistID” (integer). The “Playlist” table should have a “playlistID” (integer, autoincrement, not null, primary key), and a column for a foreign key we will assign later called “songID”...

  • Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work....

    Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work. You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. Playlist.h - Class declaration Playlist.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1...

  • Create two classes. Song. Should include the name of the song and the artist Playlist. Should...

    Create two classes. Song. Should include the name of the song and the artist Playlist. Should include a list of song objects from the above class, a title for the list and a description of the list. Specific method requirements below. Functionality You should be able to print both a song and a playlist. Formatting should follow the below examples EXACTLY. I should be able to print a so11ng or a playlist by calling the standard python print function. Format...

  • I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...

    I need c++ code Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

  • Many of us have large digital music collections that are not always very well organized. It...

    Many of us have large digital music collections that are not always very well organized. It would be nice to have a program that would manipulate our music collection based on attributes such as artist, album title, song title, genre, song length, number times played, and rating. For this assignment you will write a basic digital music manager (DMM). Your DMM program must have a text-based interface which allows the user to select from a main menu of options including:...

  • JAVA HELP Create a small program where You ask the user The amount of money he/she...

    JAVA HELP Create a small program where You ask the user The amount of money he/she wants to convert using GUI Pop up input (import javax.swing.JOptionPane;) The first pop up should ask the user to what currency he/she wants to convert, He needs to be able to click that option, The currency you should use are : Euros , Pesos , Yen , Pound , Ruble, Quetzal, Once the user Clicks the option It will ask The amount the user...

  • You will be building a linked list. Make sure to keep track of both the head and tail nodes.

    Ch 8 Program: Playlist (Java)You will be building a linked list. Make sure to keep track of both the head and tail nodes.(1) Create two files to submit.SongEntry.java - Class declarationPlaylist.java - Contains main() methodBuild the SongEntry class per the following specifications. Note: Some methods can initially be method stubs (empty methods), to be completed in later steps.Private fieldsString uniqueID - Initialized to "none" in default constructorstring songName - Initialized to "none" in default constructorstring artistName - Initialized to "none"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT