Attendance is #3 from the from the 2026 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/media/pdf/ap26-frq-computer-science-a.pdf

moreHistoryThanMathAbsences method

public int moreHistoryThanMathAbsences()
{
    int moreHistory = 0;

    for(CourseRecord historyRecord : historyList)
    {
        int mathAbsences = -1;
        int mathIndex = 0;
        while(mathIndex < mathList.size() && mathAbsences == -1)
        {
            if(mathList.get(mathIndex).getStudentID().equals(
                    historyRecord.getStudentID()))
            {
                mathAbsences = mathList.get(mathIndex).getAbsences();
            }

            mathIndex++;
        }

        if(mathAbsences != -1 && historyRecord.getAbsences() > mathAbsences)
            moreHistory++;
    }

    return moreHistory;
}

When I first approached this problem I considered making a helper method (demonstrated as an alternate solution below) to count the number of absences in a given list for a given student ID (similar to my solution to the Schedule free response).

I stuck with nested loops since the number of absences for historyList (the list I picked for the outer loop) was already accessible within the outer loop.

This solution uses a while loop to traverse mathList so it stops when it finds a matching student. This is not required since the precondition specifies that a student ID will not repeat within either list, but it’s how I thought about my solution.

moreHistoryThanMathAbsences with helper method

private static int countAbsences(ArrayList<CourseRecord> list, String id)
{
    for(CourseRecord record : list)
        if(record.getStudentID().equals(id))
            return record.getAbsences();

    return -1;
}

public int moreHistoryThanMathAbsences()
{
    int moreHistory = 0;

    for(CourseRecord historyRecord : historyList)
    {
        int mathAbsences = countAbsences(mathList, historyRecord.getStudentID());

        if(mathAbsences != -1 && historyRecord.getAbsences() > mathAbsences)
            moreHistory++;

    }

    return moreHistory;
}

The helper methods makes stopping upon finding a match slightly cleaner. As discussed above, stopping when finding a matching student ID is not required, but it is how I thought about my solution.

I don’t encourage students to seek opportunities to write helper methods, but solutions that use them correctly and without breaking context (ex: trying to add a helper method to another class) generally receive full credit.

The helper method is static since it does not require access to any instance variables (only its parameters). it would still work if it was an instance method. See Static vs instance methods and fields for additional discussion.

Java files with test code

CourseRecord.java
Attendance.java
ThreeTest.java

See Running JUnit 5 tests.

2026 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on Attendance