Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Playlist Class Playlist has been refactored to use an ArrayList, which allows it to encapsulate an arbitrary number of Songs. The starter code includes updated
Playlist Class
Playlist has been refactored to use an ArrayList, which allows it to encapsulate an arbitrary number of Songs. The starter code includes updated versions of most of the methods from Lab but you need to update the methods described below.
Playlist: Initialize an empty Playlist.
addSongint index, Song song: Add the given Song to the Playlist at the given index. If the Song reference is null or the index is out of bounds less than or greater than the index of the last Song plus leave the Playlist unchanged. If the Song is added, return true; otherwise, return false.
addSongsPlaylist playlist: Add the given Songs to the end of the Playlist in the given order. Return the number of added Songs. If the Playlist reference is null, return
removeSongint index: Remove and return the Song with the given index. If the index is out of bounds less than or greater than the index of the last Song return null.
Playlist has been expanded with methods that read and write song information to text files. A method has also been added that returns the total time.
PlaylistString filename: Initialize a Playlist by parsing a text file of info Strings with the given name. The resulting Playlist contains a Song for each line of the file, and the Song order matches the order of the info Strings.
addSongsString 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.
toString: Return the String representations of the Songs joined by line separators. For example, suppose a Playlist contains the songs "Resistance" and "Madness" by Muse. Calling toString returns
java "Resistance; Muse; : System.lineSeparator "Madness; Muse; :
The lineSeparator method returns the correct newline character sequence for your operating system r
for Windows and
for macOS and Linux
saveSongsString filename: Save the output of toString to a file with the given name. Overwrite the contents of the file if it already exists.
getTotalTime: Return the total time of all the Songs as an array of integers. Use the same format as the time field in the Song class.
playlist test
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.ioBufferedReader;
import java.ioFileReader;
import java.ioIOException;
import org.junit.jupiter.api.Test;
class PlaylistTest
@Test
void testConstructorFile throws IOException
String info "Fake Plastic Trees; Radiohead; :;
String info "Subterranean Homesick Alien; Radiohead; :;
String info; Radiohead; :;
Playlist playlist new Playlistplaylistsradioheadtxt;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
info "One Big Holiday; My Morning Jacket; :;
info "Steam Engine; My Morning Jacket; :;
info "Tropics Erase Traces; My Morning Jacket; :;
playlist new Playlistplaylistsmymorningjacket.txt;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
@Test
void testAddSongsFile throws IOException
String info "A Spoonful Weighs a Ton; The Flaming Lips; :;
String info "What Is the Light?; The Flaming Lips; :;
String info "Fight Test; The Flaming Lips; :;
String info "Yoshimi Battles the Pink Robots, Pt;
"The Flaming Lips; :;
String infoDo You Realize??; The Flaming Lips; :;
String info "The Yeah Yeah Yeah Song With All Your Power;
"The Flaming Lips; :;
Playlist playlist new Playlist;
playlist.addSongsplayliststheflaminglipssoftbulletin.txt;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
playlist.addSongnew Songinfo;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
playlist.addSongsplayliststheflaminglipsyoshimi.txt;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
playlist.addSongnew Songinfo;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
assertEqualsinfo playlist.getSongtoString;
@Test
void testToString
String info "Dreams; Fleetwood Mac;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started