From 4d872553715a932ad55db0f56a926435635f1b3d Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 23:49:43 +0100 Subject: [PATCH] Added : added calculateAverageGrade() in Class Student --- src/main/java/org/example/Student.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index 4917f2e..02b0053 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.java @@ -128,4 +128,18 @@ public class Student { return totalCredits; } + public double calculateAverageGrade() { + double totalWeightedGrades = 0.0; + int totalCredits = calculateTotalCredits(); // Use the existing method to get the total credits + + for (Map.Entry entry : courseGrades.entrySet()) { + Course course = entry.getKey(); + double grade = Double.parseDouble(entry.getValue()); // Assuming grades are stored as String and can be parsed to double + int credits = course.getCourseCredits(); + + totalWeightedGrades += grade * credits; + } + + return totalCredits > 0 ? totalWeightedGrades / totalCredits : 0.0; + } }