ACSA Exam Prep
← All free-response questions
Question 3 · Data Analysis with ArrayList · 5 points

Playlist Cleanup

A music app stores songs in a playlist. Each song is a Song object.

public class Song
{
    /** Returns the song's artist */
    public String getArtist()
    { /* implementation not shown */ }

    /** Returns the length of the song in seconds */
    public int getSeconds()
    { /* implementation not shown */ }

    // There may be instance variables, constructors,
    // and methods that are not shown.
}

public class Playlist
{
    /** The songs in this playlist; guaranteed not null */
    private ArrayList<Song> songs;

    /** Removes every song by artist that is longer than
     *  maxSeconds, and returns the total number of seconds
     *  removed. Other songs keep their relative order.
     */
    public int trimArtist(String artist, int maxSeconds)
    { /* to be implemented */ }

    // There may be instance variables, constructors,
    // and methods that are not shown.
}

Your class

5 points

Write the method trimArtist. It removes from songs every song whose artist equals artist and whose length is greater than maxSeconds. It returns the total length in seconds of the songs removed.

Suppose songs holds (artist, seconds):

("Nova", 250), ("Kai", 300), ("Nova", 410), ("Nova", 390), ("Kai", 120)

trimArtist("Nova", 260) returns 800 and leaves ("Nova", 250), ("Kai", 300), ("Kai", 120).

Tab inserts spaces. Drafts save as you type.
Self-score
0 / 5