Advertisement
CR7CR7

grade2D

Sep 21st, 2022
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1.  
  2. public class StudentUtil {
  3.  
  4.     public static double[] calculateGPA(int[] studentIdList, char[][] studentsGrades) {
  5.         double[] grades = new double[studentIdList.length];
  6.         int i = 0;
  7.  
  8.         for (char[] studentGrade : studentsGrades) {
  9.             double sumGrades = 0.0;
  10.  
  11.             for (char grade : studentGrade) {
  12.                 if (grade == 'A') {
  13.                     sumGrades += 4;
  14.                 } else if (grade == 'B') {
  15.                     sumGrades += 3;
  16.                 } else {
  17.                     sumGrades += 2;
  18.                 }
  19.             }
  20.             grades[i++] = sumGrades / studentGrade.length;
  21.         }
  22.  
  23.         return grades;
  24.     }
  25.  
  26.     public static int[] getStudentsByGPA(double lower, double higher, int[] studentIdList, char[][] studentsGrades) {
  27.         // perform parameter validation. Return null if passed parameters are not valid
  28.         if (lower < 0.0) {
  29.             return null;
  30.         }
  31.  
  32.         if (higher > 4.0) {
  33.             return null;
  34.         }
  35.  
  36.         if (lower > higher) {
  37.             return null;
  38.         }
  39.  
  40.         if (studentIdList.length != studentsGrades.length) {
  41.             return null;
  42.         }
  43.         // invoke calculateGPA
  44.         double[] gpas = calculateGPA(studentIdList, studentsGrades);
  45.         ArrayList<Integer> ids = new ArrayList<>();
  46.  
  47.         for (int i = 0; i < studentIdList.length; i++) {
  48.             if (gpas[i] >= lower && gpas[i] <= higher) {
  49.                 ids.add(studentIdList[i]);
  50.             }
  51.         }
  52.         // construct the result array and return it. You would need an extra for loop to compute the size of the resulting array
  53.         int[] ret = new int[ids.size()];
  54.  
  55.         for (int i = 0; i < ret.length; i++) {
  56.             ret[i] = ids.get(i);
  57.         }
  58.  
  59.         return ret;
  60.     }
  61.  
  62. }
  63.      
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement