StudentRecord is #4 from the from the 2005 AP Computer Science A Free Response problems.

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

Part (a) average method

private double average(int first, int last)
{
    int sum = 0;
    int numScores = 0;

    for(int i = first; i <= last; i++)
    {
        sum += scores[i];
        numScores++;
    }

    return sum / (double) numScores;
}

It is also possible to calculate the number of scores as (last - first + 1).

Part (b) hasImproved method

private boolean hasImproved()
{
    for(int i = 1; i < scores.length; i++)
        if(scores[i] < scores[i - 1])
            return false;
    
    return true;
}

Part (c) finalAverage method

public double finalAverage()
{
    int start = 0;

    if(hasImproved())
        start = scores.length / 2;

    return average(start, scores.length - 1);
}

2005 AP CS Exam Free Response Solutions

Comments

Comment on StudentRecord