This is from a Java course which I'm taking. The class StudentUtil
, including method-declarations and comments, was provided by the instructor. I just had to finish the bodies of the two methods.
calculateGPA()
computes the average performance of students. Every A-grade gives 4 points, every B-grade 3 and every C-grade 2 point.
calculateGPA()
returns students which have performed within a specified range.
The StudentUtil
class:
package modul;
import java.util.ArrayList;
public class StudentUtil {
public static double[] calculateGPA(int[] studentIdList, char[][] studentsGrades) {
double[] grades = new double[studentIdList.length];
int i = 0;
for (char[] studentGrade : studentsGrades) {
double sumGrades = 0.0;
for (char grade : studentGrade) {
if (grade == 'A') {
sumGrades += 4;
} else if (grade == 'B') {
sumGrades += 3;
} else {
sumGrades += 2;
}
}
grades[i++] = sumGrades / studentGrade.length;
}
return grades;
}
public static int[] getStudentsByGPA(double lower, double higher, int[] studentIdList, char[][] studentsGrades) {
// perform parameter validation. Return null if passed parameters are not valid
if (lower < 0.0) {
return null;
}
if (higher > 4.0) {
return null;
}
if (lower > higher) {
return null;
}
if (studentIdList.length != studentsGrades.length) {
return null;
}
// invoke calculateGPA
double[] gpas = calculateGPA(studentIdList, studentsGrades);
ArrayList<Integer> ids = new ArrayList<>();
for (int i = 0; i < studentIdList.length; i++) {
if (gpas[i] >= lower && gpas[i] <= higher) {
ids.add(studentIdList[i]);
}
}
// construct the result array and return it. You would need an extra for loop to compute the size of the resulting array
int[] ret = new int[ids.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = ids.get(i);
}
return ret;
}
}
My test class:
package modul;
import static java.lang.System.out;
import java.text.DecimalFormat;
import static java.lang.System.out;
public class Sandbox {
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);
}
int[] ids = StudentUtil.getStudentsByGPA(3.2, 3.5, studentIdList, studentsGrades);
for (int result : ids) {
out.println(result);
}
}
}
It has passed the automated test.
But what do you think about the demand of returning null when the validation of getStudentsByGPA()
fails? Would one really do that this way? Or would one throw some specific exception instead, IllegalArgumentException
for example?
What do you think about my code in general? What would you have done else and why?
public static
. \$\endgroup\$int[]
containing the student id's is completely irrelevant as a parameter for the first method, but if the return type was not part of the requirement, then maybe the expectation was to include the student id's in the returned result, which would makedouble[]
an inappropriate return type. \$\endgroup\$studentIdList
as a meaningful parameter (asking for only those students' grades), but the first method doesn't actually use it as such. \$\endgroup\$