Beginner in JAVA. I'm trying to print the GPA of the students. Find the below instruction the problem.
We can have only 3 grades: A, B, C correspond to the points 4,3,2. Let's say if a student has received the grades A,A,A, and B. GPA = (4+4+4+3) / 4 = 3.75.
calculateGPA should return the array of GPA of students.
Input: StudentList = {1001, 1002} studentgrades = {{'A','A','A','B'},{'A','B','B'}};
Output:(Expected)
3.75, 3.333
Output:(Actual)
4.00, 0.00
import java.util.ArrayList;
public class StudentUtil {
public static double[] calculateGPA(int[] studentIdList, char[][] studentsGrades) {
double[] Grades = new double[studentIdList.length];
for(int i = 0; i < studentsGrades.length; i++) {
double sumGrades = 0.0;
for(int j = 0; j < studentsGrades.length; j++) {
if(studentsGrades[i][j] == 'A') {
sumGrades += 4;
}
else if(studentsGrades[i][j] == 'B') {
sumGrades += 3;
}
else if(studentsGrades[i][j] == 'C') {
sumGrades += 2;
}
}
Grades[i++] = sumGrades / studentsGrades.length;
}
return Grades;
}
//(Test Case)
import static java.lang.System.out;
import java.text.DecimalFormat;
import static java.lang.System.out;
public class TestStudentUtil {
public static void main(String[] args) throws Exception {
int[] studentIdList = { 1001, 1002 };
char[][] studentsGrades = { { 'A', 'A', 'A', 'B' }, { 'A', 'B', 'B' } };
double[] results = StudentUtil.calculateGPA(studentIdList, studentsGrades);
for (double result : results) {
out.printf("%.2f\n", result);
}
}
}