DailySchedule is #1 from the from the 2006 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/apc/_ap06_frq_computer_sc_51649.pdf

On the 2006 AP CS A Exam, the ArrayList get method returned a reference of type Object. It was necessary to cast the reference to the correct type to call most methods. If this question appeared on a more recent AP CS A Exam, apptList would have been declared as ArrayList<Appointment> apptList and casting would not have been required.

Part (a) conflictsWith method

public boolean conflictsWith(Appointment other)
{
    return getTime().overlapsWith(other.getTime());
}

Part (b) clearConflicts method

public void clearConflicts(Appointment appt)
{
    int i = 0;
    
    while(i < apptList.size())
    {
        Appointment listAppt = (Appointment) apptList.get(i);
        if(appt.conflictsWith(listAppt))
            apptList.remove(i);
        else
            i++;
    }
}

See ArrayList practice for details on adding to and removing from an ArrayList within a loop.

Part (c) addAppt method

public boolean addAppt(Appointment appt, boolean emergency)
{
    if(emergency)
    {
        clearConflicts(appt);
        apptList.add(appt);
        return true;
    }
    
    for(int i = 0; i < apptList.size(); i++)
    {
        Appointment listAppt = (Appointment) apptList.get(i);
        if(appt.conflictsWith(listAppt))
            return false;
    }
    
    apptList.add(appt);
    return true;
}

2006 AP CS Exam Free Response Solutions

Additional ArrayList resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on DailySchedule