Question: 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 3, but you need to update the methods described below.
Playlist(): Initialize an empty Playlist.
addSong(int 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 0 or greater than the index of the last Song plus 1), leave the Playlist unchanged. If the Song is added, return true; otherwise, return false.
addSongs(Playlist 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 0.
removeSong(int index): Remove and return the Song with the given index. If the index is out of bounds (less than 0 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.
Playlist(String 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.
addSongs(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.
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; 5:46"+ System.lineSeparator()+ "Madness; Muse; 4:39"`
The lineSeparator method) returns the correct newline character sequence for your operating system ("\r
" for Windows and "
" for macOS and Linux).
saveSongs(String 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.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.jupiter.api.Test;
class PlaylistTest {
@Test
void testConstructorFile() throws IOException {
String info0= "Fake Plastic Trees; Radiohead; 4:52";
String info1= "Subterranean Homesick Alien; Radiohead; 4:27";
String info2="2+2=5; Radiohead; 3:21";
Playlist playlist = new Playlist("playlists/radiohead.txt");
assertEquals(info0, playlist.getSong(0).toString());
assertEquals(info1, playlist.getSong(1).toString());
assertEquals(info2, playlist.getSong(2).toString());
info0= "One Big Holiday; My Morning Jacket; 5:21";
info1= "Steam Engine; My Morning Jacket; 7:26";
info2= "Tropics (Erase Traces); My Morning Jacket; 5:10";
playlist = new Playlist("playlists/my-morning-jacket.txt");
assertEquals(info0, playlist.getSong(0).toString());
assertEquals(info1, playlist.getSong(1).toString());
assertEquals(info2, playlist.getSong(2).toString());
}
@Test
void testAddSongsFile() throws IOException {
String info0= "A Spoonful Weighs a Ton; The Flaming Lips; 3:32";
String info1= "What Is the Light?; The Flaming Lips; 4:05";
String info2= "Fight Test; The Flaming Lips; 4:14";
String info3= "Yoshimi Battles the Pink Robots, Pt.1; "
+ "The Flaming Lips; 4:45";
String info4="Do You Realize??; The Flaming Lips; 3:32";
String info5= "The Yeah Yeah Yeah Song (With All Your Power); "
+ "The Flaming Lips; 4:51";
Playlist playlist = new Playlist();
playlist.addSongs("playlists/the-flaming-lips-soft-bulletin.txt");
assertEquals(info0, playlist.getSong(0).toString());
assertEquals(info1, playlist.getSong(1).toString());
playlist.addSong(new Song(info2));
assertEquals(info0, playlist.getSong(0).toString());
assertEquals(info1, playlist.getSong(1).toString());
assertEquals(info2, playlist.getSong(2).toString());
playlist.addSongs("playlists/the-flaming-lips-yoshimi.txt");
assertEquals(info0, playlist.getSong(0).toString());
assertEquals(info1, playlist.getSong(1).toString());
assertEquals(info2, playlist.getSong(2).toString());
assertEquals(info3, playlist.getSong(3).toString());
assertEquals(info4, playlist.getSong(4).toString());
playlist.addSong(new Song(info5));
assertEquals(info0, playlist.getSong(0).toString());
assertEquals(info1, playlist.getSong(1).toString());
assertEquals(info2, playlist.getSong(2).toString());
assertEquals(info3, playlist.getSong(3).toString());
assertEquals(info4, playlist.getSong(4).toString());
assertEquals(info5, playlist.getSong(5).toString());
}
@Test
void testToString(){
String info0= "Dreams; Fleetwood Mac;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!