TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList that stores Song objects..
TODO 2: Implement the isEmpty method. Takes no parameters. Returns true if there are no songs on the list, false otherwise.
TODO 3: Implement the addSong method. This method takes a Song object as a parameter and does not return a value. It adds the song to the songList.
TODO 4: Implement the getSongListAsString method. This method takes no parameters. It returns a String, which is the concatenation of each Song on the song list’s String representation (its toString method). The line break character (or String) is inserted between each song. The number of each song is prepended to the song string. The numbers start at 1, not 0. This is an example of the String returned by this method as it would look when printed:
1 XO Tour Llif3, Luv Is Rage 2, Lil Uzi Vert, 3.02
2 Man's not hot, , Big Shaq, 3.06
3 Bodak Yellow (Money Moves), , Cardi B, 3.36
4 What About Us, Beautiful Trauma, P!nk, 4.31
5 DNA, DAMN., Kendrick Lamar, 3.06
If the song list is empty, the String “ no songs ” is returned.
TODO 5: Implement the removeSongByTitle method. Takes a String parameter which is the title of a song on the list. It returns true if that title matched a song on the list and that song was removed from the list. It returns false if the title was not found in the song list. It returns false if the song list is empty.
TODO 6: Implement the clearSongList method. Takes no parameters and does not return a value. It removes all of the songs from the song list. 1 import java.util.ArrayList;
SongList.java:
2 /* This class encapsulates a list of songs in a user's collection
and several
3 * operations that can be performed on that list. A song is
represented
4 * by an instance of the Song class. Each song has the following
fields:
5 * a title, an (optional) album, an artist, and a playing
time.
6 */
7 public class SongList {
8 //Class member variable declaration(s):
9 ArrayList<Song> songList;
10
11
12 /* Constructor that initializes the list and any other
13 * variables.
14 */
15 public SongList(){
16 // TODO 1: Implement this method.
17
18 }
19
20 /* Returns true if the song list contains no songs, false
otherwise.
21 */
22 public boolean isEmpty(){
23 // TODO 2: Implement this method.
24 return false;
25 }
26
27 /* Add the song passed in to the end of the list.
28 * For example, if the list contained: song1, song2,
29 * the next song added, song3, would result in this list:
30 * song1, song2, song3.
31 */
32 public void addSong(Song newSong){
33 // TODO 3: Implement this method.
34
35 }
36
37 /* This method returns a String which consists of the
String
38 * representation of each song in the list. A line break is
39 * inserted between each song String.
40 * If the song list is empty, the String "no songs" is
returned.
41 */
42 public String getSongListAsString(){
43 // TODO 4: Implement this method.
44
45
46
47
48 return null;
49 }
50
51 /* Remove the song in the songList with the targetTitle.
52 * First, the method searches for a song in the list with a title
that
53 * matches the targetTitle. If it is found, that song is removed
from
54 * the list. If the targetTitle is not matched, the list remains
the same and false is returned.
55 * Note that there should not be any null values between songs in
the list.
56 * For example, if the list contained: song1, song2, song3,
57 * and the title of song2 was "MyTitle", this call:
58 * removeSongByTitle("MyTitle");
59 * would result in this list: song1, song3.
60 * This method returns true if the targetTitle matches the title
of a song in the list,
61 * false otherwise.
62 */
63 public boolean removeSongByTitle(String targetTitle){
64 // TODO 5: Implement this method.
65
66 return false;
67 }
68
69 /*
70 * Return the song list object.
71 */
72 public ArrayList<Song> getSongList(){
73 return songList;
74 }
75
76 /* Remove all songs from the list, resulting in an empty
list.
77 */
78 public void clearSongList(){
79 // TODO 6: Implement this method.
80
81 }
82
83 }
Here is the completed code for this problem. Since you did not provide Song class, I completed this question based on my assumptions only. Assuming Song class exists and has the methods getTitle() and toString() implemented (along with others, but only these two are important to complete this). Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// SongList.java
import java.util.ArrayList;
/* This class encapsulates a list of songs in a user's collection and several
* operations that can be performed on that list. A song is represented
* by an instance of the Song class. Each song has the following fields:
* a title, an (optional) album, an artist, and a playing time.
*/
public class SongList {
// Class member variable declaration(s):
ArrayList<Song> songList;
/*
* Constructor that initializes the list and any other variables.
*/
public SongList() {
songList = new ArrayList<Song>();
}
/*
* Returns true if the song list contains no songs, false otherwise.
*/
public boolean isEmpty() {
return songList.isEmpty();
}
/*
* Add the song passed in to the end of the list. For example, if the list
* contained: song1, song2, the next song added, song3, would result in this
* list: song1, song2, song3.
*/
public void addSong(Song newSong) {
songList.add(newSong);
}
/*
* This method returns a String which consists of the String representation
* of each song in the list. A line break is inserted between each song
* String. If the song list is empty, the String "no songs" is returned.
*/
public String getSongListAsString() {
// returning "no songs" if songList is empty
if (isEmpty()) {
return "no songs";
}
int n = 1;
String str = "";
// looping through each song
for (Song s : songList) {
// appending n and value returned from toString() of current song to
// str, followed by a new line
str += n + " " + s + "\n";
n++; // next number
}
// returning str after removing extra newline at the end, if there's any
return str.trim();
}
/*
* Remove the song in the songList with the targetTitle. First, the method
* searches for a song in the list with a title that matches the
* targetTitle. If it is found, that song is removed from the list. If the
* targetTitle is not matched, the list remains the same and false is
* returned. Note that there should not be any null values between songs in
* the list. For example, if the list contained: song1, song2, song3, and
* the title of song2 was "MyTitle", this call:
* removeSongByTitle("MyTitle"); would result in this list: song1, song3.
* This method returns true if the targetTitle matches the title of a song
* in the list, false otherwise.
*/
public boolean removeSongByTitle(String targetTitle) {
//looping through songs list
for (int i = 0; i < songList.size(); i++) {
//checking if current song has given title
if (songList.get(i).getTitle().equalsIgnoreCase(targetTitle)) {
//removing it and returning true
songList.remove(i);
return true;
}
}
return false; //not found
}
/*
* Return the song list object.
*/
public ArrayList<Song> getSongList() {
return songList;
}
/*
* Remove all songs from the list, resulting in an empty list.
*/
public void clearSongList() {
songList.clear();
}
}
Get Answers For Free
Most questions answered within 1 hours.