Skip to main content

Question 3: Data Analysis with ArrayList

Volunteers who reach an hours goal

  • 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 community garden records each volunteer shift. A Shift object stores the volunteer's name and the number of hours worked. The VolunteerLog class keeps all the shifts in an ArrayList. A volunteer can appear in many shifts.

The Shift class

Java
public class Shift
{
    /** Returns the name of the volunteer who worked this shift */
    public String getName()
    { /* implementation not shown */ }
 
    /** Returns the number of hours worked in this shift (at least 1) */
    public int getHours()
    { /* implementation not shown */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

The VolunteerLog class

Java
public class VolunteerLog
{
    /** All shifts, in the order they were worked; contains no null elements */
    private ArrayList<Shift> shifts;
 
    /**
     * Returns a list of the names of the volunteers whose total hours,
     * over all their shifts, are at least minHours. Each name appears
     * once, in the order of that volunteer's first shift.
     * Postcondition: shifts is unchanged.
     */
    public ArrayList<String> reachedGoal(int minHours)
    { /* to be implemented */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

Example: the shifts in the log

Index012345
getName()"Ava""Raj""Ava""Lu""Raj""Ava"
getHours()322621

Source: Hypothetical example

Example calls on the log above

CallValue returnedWhy
reachedGoal(5)["Ava", "Lu"]Ava has 3 + 2 + 1 = 6 hours, Raj has 4 and Lu has 6.
reachedGoal(4)["Ava", "Raj", "Lu"]Everyone has at least 4. The order follows each volunteer's first shift.
reachedGoal(7)[] (an empty list)No one has 7 hours.

Source: Hypothetical example

Suggested time: 18 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

5 points

Write the VolunteerLog method reachedGoal, as described above. Complete method reachedGoal.public ArrayList<String> reachedGoal(int minHours)

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.