/** * This class models a gradebook used to record scores for students in a class. * It uses a 2D array to store the scores. The array has numStudents rows and * numAssignments columns. * * @author Brad Richards */ public class GradeBook { // Declares a 2D array of integers. We'll think of the first "dimension" as // being students and the second as assignments. Each "cell" in the 2D array // of integers is a score. Note that we don't STORE any student names or // assignment names -- just the scores. int[][] grades; // Unused cells are marked with this. Don't want to confuse with 0. public static final int NO_GRADE = -1; /** * Constructor creates the 2D array and fills it with NO_GRADE values. * * @param numStudents The number of students in the class * @param numAssignments The number of assignments used */ public GradeBook(int numStudents, int numAssignments) { grades = new int[numStudents][numAssignments]; // Create 2D array // For loop across rows // For loop down columns for(int student=0; student= 0 && student < grades.length && assignNum >= 0 && assignNum < grades[student].length) { grades[student][assignNum] = score; } } /** * Calculates a student's average score across assignments. Ignores * assignments that haven't been done yet. (Ignores -1) * * @param student The student whose assignments should be averaged * @return Student's average score */ public double getStudentAverage(int student) { int sum = 0; int numAsmts = 0; // Make sure the student number is valid if (student >= 0 && student < grades.length) { // Step through each assignment, keeping the row (student) fixed. for(int asmt=0; asmt= 0 && asmt < grades[0].length) { for(int student=0; student