Calculate middle class for x class objects

Can anyone give me a clue on how to calculate the average class of all objects from the Student class?

My class student:

String name // of student
String [] courses // the array that contains all the courses for each student
Int [] grades // the array that contains the grades for each student

      

I created a method that calculates the average for one object (student):

public double averageGradeStud() {
        double total = 0;

        for (int i = 0; i < numberOfGrades; i++) {

            total += grades[i];

        }

        return (total / numberOfGrades);

      

But how can I create a method that calculates the middle class for all students (objects) that have been created?

+3


source to share


2 answers


I am assuming the students are in the collection. You need to iterate over the collection and display your function that calculates the average student level for each student. At the same time, you sum these values ​​and after you finish iterating, divide it by the number of students:



List<Student> students;
double total = 0;
for(Student s : students){
    total += s.averageGradeStud();
}
double averageForAll = total / students.size();

      

+1


source


I would say that the most concise way to do this would be to make an ArrayList of your students and then make a method that will get the GPA. After that, you can use a for loop to calculate the grand total for all students.



 //Example
 int totalGrad = 0;
 for(int i = 0; i < studentList.size(); i++)
 {
     totalGrad += studentList.get(i).getAverageGrade();
     // if you want to then average the totalGrad you can do the following
     totalGrad = (totalGrad / studentList.size());
 }

      

0


source







All Articles