Post

Created by @nathanedwards
 at October 31st 2023, 10:12:55 pm.

Question:

You are given a program that simulates a student roster system. The program uses an ArrayList to store a list of student objects. Each student object represents a student's name, ID, and GPA. You need to complete the getAverageGPA method which calculates and returns the average GPA of all the students in the roster.

Complete the getAverageGPA method below:

import java.util.ArrayList;

public class StudentRoster {
    private ArrayList<Student> roster;

    // Constructor
    public StudentRoster() {
        roster = new ArrayList<Student>();
    }

    // Method to add a new student to the roster
    public void addStudent(String name, int id, double gpa) {
        Student student = new Student(name, id, gpa);
        roster.add(student);
    }

    // Method to calculate the average GPA of all students in the roster
    public double getAverageGPA() {
        // TODO: Complete this method
    }

    public static void main(String[] args) {
        StudentRoster roster = new StudentRoster();

        // Add some students to the roster
        roster.addStudent("Alice", 1, 3.7);
        roster.addStudent("Bob", 2, 4.0);
        roster.addStudent("Charlie", 3, 3.9);
        roster.addStudent("David", 4, 3.5);
        roster.addStudent("Eve", 5, 3.8);

        // Print the average GPA
        System.out.println("Average GPA: " + roster.getAverageGPA());
    }
}

class Student {
    private String name;
    private int id;
    private double gpa;

    public Student(String name, int id, double gpa) {
        this.name = name;
        this.id = id;
        this.gpa = gpa;
    }

    // Getters and setters
    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public double getGPA() {
        return gpa;
    }
}

Provide the implementation for the getAverageGPA method that calculates and returns the average GPA of all the students in the roster. Use the formula:

averageGPA = sum of all student GPAs / total number of students

Write your solution below:

Solution:

public double getAverageGPA() {
        double totalGPA = 0.0;
        int numStudents = roster.size();

        for (Student student : roster) {
            totalGPA += student.getGPA();
        }

        double averageGPA = totalGPA / numStudents;
        return averageGPA;
}

Explanation:

To calculate the average GPA, we start by initializing a variable totalGPA to 0.0 and numStudents to the size of the roster ArrayList. These variables will be used to calculate the sum of all student GPAs and the total number of students, respectively.

Next, we iterate over each student in the roster ArrayList using an enhanced for loop. For each student, we call the getGPA method to access their GPA and add it to the totalGPA variable. This will accumulate the sum of all student GPAs.

After iterating through all the students, we calculate the average GPA by dividing the totalGPA by the numStudents. The result is stored in the averageGPA variable.

Finally, the averageGPA is returned as the result of the method.

In the provided code, we have added some students to the roster using the addStudent method and printed the average GPA by calling the getAverageGPA method. When you run the program, it will output the average GPA based on the student roster provided.