Question 3: Data Analysis with ArrayList
Removing short songs from a playlist
- Unit 4
- 5 points
- About 18 minutes
You write one method that traverses, analyzes or changes data stored in an ArrayList of objects, using the classes described in the question. New since the course was revised for 2025–26 (first tested in May 2026): it's always an ArrayList now (it used to be an array or an ArrayList), it's worth 5 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 3 of 4, 5 points. Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.
The question and its sources
A music app stores playlists. Each song is represented by a Song object, and each playlist by a Playlist object that keeps its songs in an ArrayList.
The Song class
public class Song
{
/** Returns the title of the song */
public String getTitle()
{ /* implementation not shown */ }
/** Returns the length of the song, in seconds (always greater than 0) */
public int getSeconds()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods that are not shown.
}The Playlist class
public class Playlist
{
/** The songs in the playlist, in play order; contains no null elements */
private ArrayList<Song> songs;
/**
* Removes every song shorter than minSeconds from the playlist and
* returns the total length, in seconds, of the songs removed.
* The songs that are not removed stay in the same order.
* Precondition: minSeconds > 0
*/
public int removeShort(int minSeconds)
{ /* to be implemented */ }
// There may be instance variables, constructors, and methods that are not shown.
}Example: songs before the call removeShort(60)
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| getTitle() | "Intro" | "Glow" | "Skit" | "Interlude" | "Run" |
| getSeconds() | 45 | 210 | 30 | 50 | 185 |
Source: Hypothetical example
Suggested time: 18 minutes
Your answers are saved in this browser as you type.
Part (a)
5 pointsWrite the Playlist method removeShort. For the example above, the call removeShort(60) removes "Intro", "Skit" and "Interlude" and returns 125 (45 + 30 + 50). Afterward, songs contains "Glow" followed by "Run". A song that is exactly minSeconds long is not removed.
Complete method removeShort.public int removeShort(int minSeconds)
Tab indents. Press Esc, then Tab, to move on.
0 lines · 0 / 2,500 characters
Checking scoring…
Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.