Question

TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList...

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 }

Homework Answers

Answer #1

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();

      }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Create a field with the boolean value of goodBoi. In the constructor, set your good boi...
Create a field with the boolean value of goodBoi. In the constructor, set your good boi to being false, as all dogs has deserve being called a good boiii. Make a method makeGoodBoi, which sets the good boi value to being true. Make a method isGoodBoi which returns the value the good boi field (hint: it returns a boolean value). Now we need a method called whosAGoodBoi. If the doggo is indeed a good boi, then print: "[doggo] is such...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
IN JAVA: Write recursive method to return true if a given array has element equal to...
IN JAVA: Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp,...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...