1. This question involves the PlaylistBuilder class, which is used to generate a music playlist based on a starting song and a time limit. The PlaylistBuilder class contains helper methods to determine the next song and the duration of a song. You will write a constructor and a method in the PlaylistBuilder class.
public class PlaylistBuilder
{
/** The string containing the list of songs in the playlist */
private String playlist; // To be initialized in part (a)
/** The total duration of the playlist in minutes */
private int totalDuration; // To be initialized in part (a)
/**
* Constructs a playlist starting with the song specified by the
* parameter and adds subsequent songs until the playlist is full,
* as described in part (a)
* Precondition: firstSong is a non-empty string.
* maxMinutes > 0
*/
public PlaylistBuilder(String firstSong, int maxMinutes)
{ /* to be implemented in part (a) */ }
/**
* Returns the song that should follow the parameter song
* or null if there are no more songs to add.
* Precondition: currentSong is a non-empty string.
*/
public String getNextSong(String currentSong)
{ /* implementation not shown */ }
/**
* Returns the duration of the parameter song in minutes.
* Precondition: song is a non-empty string.
*/
public int getDuration(String song)
{ /* implementation not shown */ }
/**
* Returns a summary string containing the first few characters
* of each song in the playlist, as described in part (b)
* Precondition: playlist contains at least one song.
*/
public String createSummary()
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}Write the PlaylistBuilder constructor, which initializes the playlist and totalDuration instance variables. The constructor starts the playlist with the parameter firstSong and adds its duration to totalDuration. It then repeatedly calls the helper method getNextSong to obtain the next song to add.
Each new song is added to the playlist string, preceded by the delimiter " -> ", and its duration is added to totalDuration. This process continues as long as a next song exists (is not null) AND adding the song would not cause totalDuration to exceed maxMinutes.
Example 1
| Method Call | Return Value |
|---|---|
| getDuration("Song A") | 5 |
| getNextSong("Song A") | "Song B" |
| getDuration("Song B") | 4 |
| getNextSong("Song B") | "Song C" |
| getDuration("Song C") | 6 |
| getNextSong("Song C") | "Song D" |
| getDuration("Song D") | 10 |
/** * Constructs a playlist starting with the song specified by the * parameter and adds subsequent songs until the playlist is full, * as described in part (a) * Precondition: firstSong is a non-empty string. * maxMinutes > 0 */ public PlaylistBuilder(String firstSong, int maxMinutes)
Write the createSummary method, which returns a string consisting of the first 3 characters of each song title in the playlist. If a song title has fewer than 3 characters, the entire title is included in the summary. The summary should not include the " -> " delimiters.
Example 1
| Instance Variable | Value |
|---|---|
| playlist | "Hello -> World -> Of -> Java" |
/** * Returns a summary string containing the first few characters * of each song in the playlist, as described in part (b) * Precondition: playlist contains at least one song. */ public String createSummary()