I'm new to Java and I'm writting a program that manages students, their subjects and the grades in that subject.
The code asks for the details of the student and then it saves all to an Arraylist. I'm trying to get the average grade of a subject without success, I've already calculated the average grade of a certain student, but getting all the grades from certain subject and then calculating the average is beyond my comprehension.
This is the code I used for the creation of the Arraylist:
public class options { public String name; public ArrayList<students> studentlist = new ArrayList<students>(10); public String createstudent(String name, String subject, Float rade1, Float grade2, Float grade3) { studentlist.add(new students(name, subject, grade1, grade2, grade3); return "The student has been added to the database. "; } }
And this is the code of the student class:
public class students{ public String name; public String subject; public Float grade1; public Float grade2; public Float grade3; public students(String name, String subject, Float grade1, Float grade2, Float grade3) { this.name= name; this.subject= subject; this.grade1= grade1; this.grade2= grade2; this.grade3= grade3; } }
I've tried something like
public double getAverageValue(){ double averageValue = 0; double sum = 0; if(studentlist.size() > 0){ for ( int i=0; i < studentlist.size() ; i++) { sum += studentlist.get(i).getname().getgrade1().getgrade2().getgrade3(); } averageValue = (sum / (double)studentlist.size()) } return averageValue; }
But I don't know how to select only the grades from a certain subject and not all the grades from all the students, so I'm kinda stuck.
https://stackoverflow.com/questions/66605982/java-getting-the-average-of-some-elements-in-an-arraylist March 13, 2021 at 03:21AM
没有评论:
发表评论