/** * This class implements a grading tool for organizing, storing and computing * grades for a particular course. * * @author Matt Kretchmar * @version 3/27/31 */ import java.util.Scanner; public class GradeBook { // Instances variables private int numStudents; // number of students in the class private int numExams; // number of exams per student private String names[]; // names of students in class private int grades[][]; // grades for each exam per student // row=student, col=exam // Methods /** * Constructor. This is the only constructor. The user must specify * the size of the grade book in terms of both number of students * and number of exams. * @param numberOfStudents the number of students in the class. * @param numberOfExams the number of exams for the course. */ public GradeBook ( int numberOfStudents, int numberOfExams ) { numStudents = numberOfStudents; numExams = numberOfExams; grades = new int[numStudents][numExams]; names = new String[numStudents]; // initialize all scores to -1 meaning not yet entered // initialize all names to null meaning not yet entered for ( int i = 0; i < numStudents; i++ ) { for ( int j = 0; j < numExams; j++ ) grades[i][j] = -1; names[i] = null; } } /** * A method to read the class names roster. Each name is a single string. */ public void enterNames ( ) { Scanner keyboard = new Scanner(System.in); for ( int i = 0; i < numStudents; i++ ) { System.out.println("Enter name for student " + (i+1) + ": "); names[i] = keyboard.nextLine(); } } /** * A method to read the exam scores for a particular exam. * @param examNumber specifies which exam to enter grades. Note that exams * are numbered starting at 1 (not 0). */ public void enterScores ( int examNumber ) { if ( examNumber < 1 || examNumber > numExams ) { System.out.println("enterScores: Invalid exam number. Exam number must be in the"); System.out.println("range of 1 to " + numExams + "."); return; } Scanner keyboard = new Scanner(System.in); for ( int i = 0; i < numStudents; i++ ) { String tempName; if ( names[i] != null ) tempName = names[i]; else tempName = "unknown"; System.out.println("Enter score for " + tempName + ", student " + (i+1) + ": "); grades[i][examNumber-1] = keyboard.nextInt(); } } /** * A method to compute the mean for a particular exam score. * If a students score has not been entered, it will not be included in the * calculation. If no scores have been entered, a 0 will be returned. * @param examNumber specifies which exam to compute mean. Note that exams * are numbered starting at 1 (not 0). * @return the mean of exam scores. */ public double getExamMean ( int examNumber ) { double mean = 0; double sum = 0; // sum of exam scores int count = 0; // number of valid scores if ( examNumber < 1 || examNumber > numExams ) { System.out.println("Invalid exam number. Exam number must be in the"); System.out.println("range of 1 to " + numExams + "."); return mean; } for ( int i = 0; i < numStudents; i++ ) { if ( grades[i][examNumber-1] != -1 ) { count++; sum += grades[i][examNumber-1]; } } if ( count != 0 ) mean = sum / count; else System.out.println("Warning, no scores entered yet for this exam."); // round off to two decimal places mean = Math.round(mean*100)/100.0; return mean; } /** * A method to compute the mean grade for a particular student. * If a students score has not been entered, it will not be included in the * calculation. If no scores have been entered, a 0 will be returned. Essentially * this calculation returns an average of their entered grades. * @param studentNumber specifies which student to compute mean. Note that students * are numbered starting at 1 (not 0). * @return the mean of exam scores. */ public double getStudentGrade ( int studentNumber ) { double mean = 0; double sum = 0; // sum of exam scores int count = 0; // number of valid scores if ( studentNumber < 1 || studentNumber > numStudents ) { System.out.println("Invalid stduent number. Student number must be in the"); System.out.println("range of 1 to " + numStudents + "."); return mean; } for ( int j = 0; j < numExams; j++ ) { if ( grades[studentNumber-1][j] != -1 ) { count++; sum += grades[studentNumber-1][j]; } } if ( count != 0 ) mean = sum / count; else System.out.println("Warning, no scores entered yet for this student."); // round off to two decimal places mean = Math.round(mean*100)/100.0; return mean; } /** * A method to compute the letter grade for a particular student. * This method defaults to A,B,C,D,F from the standard numeric ranges of: *