-1

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);
            }


        }
    }
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
Gokul UK
  • 49
  • 8
  • Your two loops loop from 0 to studentsGrades.length. And for some reason I can't understand, you have i++ twice. And you divide by the wrong number, too. Extract a method to compute the average for a single array. That will make things much clearer. – JB Nizet Dec 10 '19 at 23:27

1 Answers1

0

The problem is in the inner loop: your loop condition is j < studentsGrades.length, when it should be j < studentsGrades[i].length to iterate over all entries of the array studentsGrades[i].

A cleaner way to write this code is to use an enhanced for loop, since you just need the values anyway, not the indices. As well as being easier to read, this avoids the possibility of a subtle mistake like writing the wrong loop bound.

            for(int i = 0; i < studentsGrades.length; i++) {
                double sumGrades = 0.0;
                char[] currentGrades = studentsGrades[i];

                for(char grade : currentGrades) {
                    if(grade == 'A') {
                        sumGrades += 4;
                    }
                    else if(grade == 'B') {
                        sumGrades += 3;
                    }
                    else if(grade == 'C') {
                        sumGrades += 2;
                    }
                }
                Grades[i] = sumGrades / currentGrades.length;
            }

The division at the end had the same problem; you need to divide by how many grades this student has, not by the number of students.

Note also that i++ is already done in your outer loop's update step, so you shouldn't also do it in the line Grades[i] = ....

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • Hi @kaya3 Please explain me this logic "char[] currentGrades = studentsGrades[i];". I'm not getting clear idea about it. – Gokul UK Dec 11 '19 at 23:16
  • `studentGrades` is an array of arrays - one array per student - so `studentGrades[i]` is just the array of student `i`'s grades. – kaya3 Dec 11 '19 at 23:18