?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

ArrayList of ArrayLists: Find Average Class Grade

APCSA-VZAKYV

classGrades is an array list of array lists. Each element is an array list of an individual student’s grades. The classGrades elements could have different sizes since each student could have a different number of grades. Which of the following methods will return the average of the grades of all students in the class?

A
public static double avgClassGrades(ArrayList<ArrayList<Integer>> classGrades)
{
    double total=0, count=0;
    for (int a=0; a<classGrades.size(); a++)
    {
        for (int b=0; b<classGrades.get(a).size(); b++)
        {
            total += classGrades.get(a).get(b);
            count++;
        }
    }
    double avg = total/count;
}
B
public static double avgClassGrades(ArrayList<ArrayList<Integer>> classGrades)
{
    double total=0, count=0;
    for (int a=0; a<classGrades.size(); a++)
    {
        for (int b=0; b<classGrades.get(a).size(); b++)
        {
            total += classGrades.get(a);
            count++;
        }
    }
    if (count>0)
        return total/count;
    return 0;
}
C
public static double avgClassGrades(ArrayList<ArrayList<Integer>> classGrades)
{
    double total=0, count=0;
    for (int a=0; a<classGrades.size(); a++)
    {
        for (int b=0; b<classGrades.get(a).size(); b++)
        {
            total += classGrades.get(a).get(b);
            count++;
        }
    }
    if (count>0)
        return total/count;
    return 0;
}
D
public static double avgClassGrades(ArrayList<ArrayList<Integer>> classGrades)
{
    double total=0, count=0;
    for (int a=0; a<classGrades.size(); a++)
    {
        for (int b=0; b<classGrades.get(0).size(); b++)
        {
            total += classGrades.get(a).get(b);
            count++;
        }
    }
    if (count>0)
        return total/count;
    return 0;
}
E
public static double avgClassGrades(ArrayList<ArrayList<Integer>> classGrades)
{
    double total=0, count=0;
    for (int a=0; a<classGrades.size(); a++)
    {
        for (int b=0; b<classGrades.size(); b++)
        {
            total += classGrades.get(a).get(b);
            count++;
        }
    }
    if (count>0)
        return total/count;
    return 0;
}