0
public class Playlist 
{
    String title;
    String genre;
    Boolean privatePlaylist = true;
    Song[] listOfSongs;

    public Playlist(String tl, String gn, Boolean priv)
    {
        tl = title;
        gn = genre;
        priv = privatePlaylist;
    }

    Song[] mostPlayedSongs()
   {
       return listOfSongs;
   }

}

Above is my code. I am trying to create a java class that returns a playlist. I want it to return a title, genre, whether the playlist is private or public. All three of these things are already constructed above. However, to that list, as a fourth property I would like to add an array that lists all the song in the playlist. The array is already a property I created above(Song[]listOfSongs). However, I do not know how to join this array to the other three properties and how to put the songs in this array. Any suggestions?

0

2 Answers 2

1

if you don't want to change the songs, you could just place it in the constructor:

public PlayList(String title, String genre, Boolean privatePlaylist, Song[] songs) {
    this.title = title;
    this.genre = genre;
    this.privatePlaylist = privatePlaylist; //original constructor wasn't assigning this property
    this.listOfSongs = listOfSongs;
  }

if the list of songs shouldn't be added in the constructor (i.e, it needs to be updated) I would use an ArrayList so you can change the size:

    // Field:
private ArrayList<Song> listOfSongs;

// Constructor
public PlayList(String title, String genre, Boolean privatePlaylist) {
        this.title = title;
        this.genre = genre;
        this.privatePlaylist = privatePlaylist; //original constructor wasn't assigning this property
        this.listOfSongs = new ArrayList<>();
}

// To edit the songs:
public void setListOfSongs (ArrayList<Song> listOfSongs) {
     this.listOfSongs = listOfSongs;
}
Sign up to request clarification or add additional context in comments.

4 Comments

The usage of ArrayList does not help at all here because it is private. To change the value one has to keep a reference to the list (after setting it, which makes the new ArrayList in the constructor redundant) which is not optimal in my opinion.
In order to edit it, you can use a getter, update that list, and then set it as the new one. Alternatively, you can create a function that adds songs public void addSong (Song newSong) { this.listOfSongs.add(newSong); }) or one that removes them, if you have some way to identify the song in order to get the index
I'm sorry, but I honestly cannot see the benefit of using setters and getters here, what's wrong with just using a public field? I've seen the setter/getter pattern everywhere and frankly I dislike it where it is not necessary.
For me, it all depends on what you are trying to do with those fields. I agree that some people stress encapsulation a little too much, particularly in schooling environments. But I feel that there are some instances where it's important, and, without knowing what the Song class consists of and what you are trying to do with the PlayList class, I feel that it is the safer option.
0
import java.util.ArrayList;

public class Playlist {
    public final String title;
    public final String genre;
    public final Boolean privatePlaylist;
    public final ArrayList<Song> listOfSongs;

    public Playlist(String tl, String gn, Boolean priv) {
        title = tl;
        genre = gn;
        privatePlaylist = priv;
        listOfSongs = new ArrayList<>();
    }
}

Example usage:

public class Main {
    public static void main(String[] args) {
        Playlist pl = new Playlist("title", "genre", false);
        pl.listOfSongs.add(...);
        System.out.println(pl.title);
    }
}

2 Comments

Your code for class Playlist won't compile. You can not do privatePlaylist = priv; as the reassignment is not possible to a final variable.
Indeed, I didn't notice he had a value for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.