I was just trying to get suggestions per the course mentor's advice on StackOverflow. I am sorry.
/**
* prints a student’s average grade by student ID
* @param (String studentID)/ */
public static void print_average_grade(String studentID)
{
// Here, you have studentID passed into the function but you are not using it
for (Student s : studentList)
{
// Use the studentID here!
if(s.getStudentID().equals(studentID)) {
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
}
}
Use the studentID passed in as an argument, so that you will print out the average grade for the correct student each time.
You're using two loops, first you use
for (Student s : studentList) {
print_average_grade(s.getStudentID());
}
to call print_average_grade, passing it the studentID, which you promptly ignore and then loop over all the students, again...
public static void print_average_grade(String studentID) {
for (Student s : studentList) {
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
}
Instead, change your print_average_grade method to accept a Student and simply calculate the average for said student
public static void print_average_grade(Student s) {
double total = s.getGrade1() + s.getGrade2() + s.getGrade3();
double average = total / 3;
System.out.println("StudentID# " + studentID + " " + average);
}
Then in your main, loop, just pass it the Student you want...
for (Student s : studentList) {
print_average_grade(s);
}
If you "really" need to do this by id, then I would write a method which took a int value (id) and does the lookup passing back the Student object, since you seem to want to do this a few times...(mind you a Map would be more useful)
public Student getStudentById(int id) {
Student match = null;
for (Student s : studentList) {
if (s.getStudentID() == id) {
match = s;
break;
}
}
return match;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With