Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Based on the ULM and method descriptions for the Song class. Please write the Playlist class in Java. The objectives of the Playlist class methods

Based on the ULM and method descriptions for the Song class. Please write the Playlist class in Java. The objectives of the Playlist class methods are listed below. Will thumbs up. Only need to write the Playlist class.

image text in transcribed

image text in transcribed

image text in transcribed

Description: In our earlier programs, the states of the objects we constructed at runtime were lost when the program stopped. For many applications, this is an unacceptable limitation. A word processor, for instance, wouldn't be very useful if we lost our documents when we turned off our computer. For our third lab, we will write a pair of classes that do not have this limitation: Song and Playlist. Playlist objects store a list of Song objects that can be modified in various ways. Most importantly, the state of a Playlist can be saved and loaded from a text file, so we can keep track of our music collection even after our program stops! 0..1 0..1 Class Diagram: Song -title: String -artist: String -minutes: int -seconds: int -numPlays: int -IDX TITLE = 0; int -IDX ARTIST = 1: int -IDX_LENGTH = 2: int - IDX_NUM PLAYS = 3: int +Song (info: String) +Song (other: Song) +getTitle(): String +getArtist(): String +getMinutes(): int +getSeconds(): int +getLength(): String +getNumPlays(): int +incrementPlays(): void +toString(): String Playlist -songs: ArrayList +Playlist) +Playlist(filename: String) - load(filename: String): void +save(filename: String): void +addSong (song: Song): void +addSong(index: int, song: Song): void +addSongs (filename: String): void +getSong(index: int): Song +numSongs(): int +playSong (index: int): void +removesong(index: int): Song +toString(): String +favoriteArtist(): String +totalPlaytime(): String tostrinsong(indi int): , Song Class: Each Song has a title, artist, length, and play count. The length is stored in two fields: minutes and seconds. Songs can be constructed from either an info String or another Song object. Info Strings have the following format: ", <artist>,<minutes>: <seconds>" The numbers "0.1" on the aggregation line indicate that each Song belongs to either 0 or 1 Playlist. Likewise, "O..*" indicates that each Playlist has 0 or more Songs. The values are separated by commas with no spaces. The index of each value is stored in a static variable. For instance, IDX_ARTIST stores the index of "<artist>", which is the value of the artist field. Optionally, an info String may have a fourth value that specifies the number of times a song has been played: "<title>, <artist>, <minutes>:<seconds>, <numplays>" If this value is absent, initialize numPlays to 0. To illustrate this format, consider the following info String: "Ten Years Gone, Led Zeppelin, 6:31" A Song constructed from this String stores "Ten Years Gone" in the title field, "Led Zeppelin" in the artist field, 6 in the minutes field, and 31 in the seconds field. The info String does not have a fourth value, so the object stores in the numPlays field. Below are descriptions of some methods of the Song class: Song(Song other): This is known as a "copy constructor." It creates a new song that is a copy of an existing song. That is, it initializes the fields of the new Song to the values stored in the given Song. The utility of this method will become clear when writing the Playlist class. .getLength(): Return the song length in the format of an info String: "<minutes>: <seconds>" If the number of seconds is less than 10, pad the value with a leading zero so it has two digits. The easiest way to do this is with the format method of the String class. Use the format String "%02d", which indicates that the value should be a decimal integer with at least two digits. incrementPlays(): Increase numPlays by 1. Note that this is a mutator, which implies that Songs are mutable. .toString(): Return an info String with the values of the fields. If numPlays is 0, do not include it in the output. (Hint: Use getLength() to avoid duplicating code.) Playlist Class: Each Playlist stores a reference to an ArrayList of Songs. We want to protect these Songs so they can only be modified by methods of the Playlist class. In order to accomplish this, any song must be copied before it is added or returned from a Playlist. Otherwise, the calling method will have a reference to mutable private data.? Below are descriptions of the methods of the Playlist class: Playlist(): Construct an empty Playlist. Playlist(String filename): Construct a Playlist from a file of info Strings. The Playlist should contain a Song for every line of the file, and the order of the Songs should match the file. load(String filename): Read a file of info Strings with the given name. For each line of the file, create a Song and add it to the end of the Playlist. This method is intended to be a helper method for Playlist(String filename) and addSongs(String filename). save(String filename): Save the output of toString() to a file with the given name. (Overwrite the contents of the file if it already exists.) .addSong(Song song): Add a copy of the given Song to the end of the Playlist. .addSong(int index, Song song): Add a copy of the given Song to the Playlist at the given index. .addSongs(String filename): A public version of the method Load(String filename). .getSong(int index): Return a copy of the song with the given index. numSongs(): Return the number of Songs in the Playlist. playSong(int index): Increment the number of plays of the song with the given index. removeSong(int index): Remove and return the song with the given index. (It is not necessary to return a copy.) .toString(): Return a String with the values of the fields of every Song in the Playlist. Create the String by calling toString() on each Song and joining the output with newline characters. (Do not terminate the String with a newline character.) For example, if a Playlist contains Songs constructed with these info Strings: "So What, Miles Davis, 9:22" "Firth of Fifth, Genesis,9:35" Then calling toString() on the Playlist should return this String: "So What, Miles Davis, 9:22 Firth of Fifth, Genesis, 9:35" favorite Artist(): Return the artist that appears most frequently in the Playlist. If the Playlist is empty, return null. totalPlaytime(): Return the total length of all the songs in the following format: "<hours>:<minutes>: <seconds the number of minutes and seconds should each be a between if hours is use this format instead:>:<seconds pad the number of seconds with a leading zero if it is less than hours greater minutes as well. description: in our earlier programs states objects we constructed at runtime were lost when program stopped. for many applications this an unacceptable limitation. word processor instance wouldn be very useful documents turned off computer. third lab will write pair classes that do not have limitation: song and playlist. playlist store list can modified various ways. most importantly state saved loaded from text file so keep track music collection even after stops diagram: string int title="0;" artist="1:" idx_num plays="3:" void arrayliststyle=""> +Playlist) +Playlist(filename: String) - load(filename: String): void +save(filename: String): void +addSong (song: Song): void +addSong(index: int, song: Song): void +addSongs (filename: String): void +getSong(index: int): Song +numSongs(): int +playSong (index: int): void +removesong(index: int): Song +toString(): String +favoriteArtist(): String +totalPlaytime(): String tostrinsong(indi int): , Song Class: Each Song has a title, artist, length, and play count. The length is stored in two fields: minutes and seconds. Songs can be constructed from either an info String or another Song object. Info Strings have the following format: "<title>, <artist>,<minutes>: <seconds>" The numbers "0.1" on the aggregation line indicate that each Song belongs to either 0 or 1 Playlist. Likewise, "O..*" indicates that each Playlist has 0 or more Songs. The values are separated by commas with no spaces. The index of each value is stored in a static variable. For instance, IDX_ARTIST stores the index of "<artist>", which is the value of the artist field. Optionally, an info String may have a fourth value that specifies the number of times a song has been played: "<title>, <artist>, <minutes>:<seconds>, <numplays>" If this value is absent, initialize numPlays to 0. To illustrate this format, consider the following info String: "Ten Years Gone, Led Zeppelin, 6:31" A Song constructed from this String stores "Ten Years Gone" in the title field, "Led Zeppelin" in the artist field, 6 in the minutes field, and 31 in the seconds field. The info String does not have a fourth value, so the object stores in the numPlays field. Below are descriptions of some methods of the Song class: Song(Song other): This is known as a "copy constructor." It creates a new song that is a copy of an existing song. That is, it initializes the fields of the new Song to the values stored in the given Song. The utility of this method will become clear when writing the Playlist class. .getLength(): Return the song length in the format of an info String: "<minutes>: <seconds>" If the number of seconds is less than 10, pad the value with a leading zero so it has two digits. The easiest way to do this is with the format method of the String class. Use the format String "%02d", which indicates that the value should be a decimal integer with at least two digits. incrementPlays(): Increase numPlays by 1. Note that this is a mutator, which implies that Songs are mutable. .toString(): Return an info String with the values of the fields. If numPlays is 0, do not include it in the output. (Hint: Use getLength() to avoid duplicating code.) Playlist Class: Each Playlist stores a reference to an ArrayList of Songs. We want to protect these Songs so they can only be modified by methods of the Playlist class. In order to accomplish this, any song must be copied before it is added or returned from a Playlist. Otherwise, the calling method will have a reference to mutable private data.? Below are descriptions of the methods of the Playlist class: Playlist(): Construct an empty Playlist. Playlist(String filename): Construct a Playlist from a file of info Strings. The Playlist should contain a Song for every line of the file, and the order of the Songs should match the file. load(String filename): Read a file of info Strings with the given name. For each line of the file, create a Song and add it to the end of the Playlist. This method is intended to be a helper method for Playlist(String filename) and addSongs(String filename). save(String filename): Save the output of toString() to a file with the given name. (Overwrite the contents of the file if it already exists.) .addSong(Song song): Add a copy of the given Song to the end of the Playlist. .addSong(int index, Song song): Add a copy of the given Song to the Playlist at the given index. .addSongs(String filename): A public version of the method Load(String filename). .getSong(int index): Return a copy of the song with the given index. numSongs(): Return the number of Songs in the Playlist. playSong(int index): Increment the number of plays of the song with the given index. removeSong(int index): Remove and return the song with the given index. (It is not necessary to return a copy.) .toString(): Return a String with the values of the fields of every Song in the Playlist. Create the String by calling toString() on each Song and joining the output with newline characters. (Do not terminate the String with a newline character.) For example, if a Playlist contains Songs constructed with these info Strings: "So What, Miles Davis, 9:22" "Firth of Fifth, Genesis,9:35" Then calling toString() on the Playlist should return this String: "So What, Miles Davis, 9:22 Firth of Fifth, Genesis, 9:35" favorite Artist(): Return the artist that appears most frequently in the Playlist. If the Playlist is empty, return null. totalPlaytime(): Return the total length of all the songs in the following format: "<hours>:<minutes>: <seconds the number of minutes and seconds should each be a between if hours is use this format instead:>:<seconds pad the number of seconds with a leading zero if it is less than hours greater minutes as well></seconds></seconds></minutes></hours></seconds></minutes><umplays></seconds></minutes></artist>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DNA Databases

Authors: Stefan Kiesbye

1st Edition

0737758910, 978-0737758917

More Books

Students also viewed these Databases questions