/** * 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: * * @param studentNumber specifies which student to compute grade. Note that students * are numbered starting at 1 (not 0). * @return the letter grade for the specified student. The return value is a string so * that + and - could be added as well. */ public String getStudentLetterGrade ( int studentNumber ) { double ranges[] = {90,80,70,60,0}; String letters[] = {"A", "B", "C", "D", "F"}; return getStudentLetterGrade(studentNumber,ranges,letters); } /** * A method to compute the letter grade for a particular student. * The parameter ranges must give the lower range for each particular letter grade. * The parameter letters must give the corresponding letter for each grade. * The two arrays must be of the same length. * For example, a typical A-F grading scale would have range of * ranges[] = { 90, 80, 70, 60, 0 } * letters[] = { "A", "B", "C", "D", "F" } * Notice the lower range of the last entry should be the lowest possible numeric grade. * In the above example a "B" is awarded to any numeric grade < 90 and >= 80. The lower * bound is inclusive, the upper bound is exclusive (except for the implied upper bound of * the first grade which is not specified). * @param studentName specifies which student to compute grade. * @param ranges an array of lower bounds for each letter grade level. See comments above. * @param letters an array of grades corresponding to each letter grade level. See comments above. * @return the letter grade for the specified student. The return value is a string so * that + and - could be added as well. */ public String getStudentLetterGrade ( String studentName, double ranges[], String letters[] ) { int studentNumber = getStudentNumber(studentName); if ( studentNumber < 0 || studentNumber >= numStudents ) { System.out.println("Student name (" + studentName + ") not found."); return "unknown"; } return getStudentLetterGrade(studentNumber,ranges,letters); } /** * A method to compute the letter grade for a particular student. * The parameter ranges must give the lower range for each particular letter grade. * The parameter letters must give the corresponding letter for each grade. * The two arrays must be of the same length. * For example, a typical A-F grading scale would have range of * ranges[] = { 90, 80, 70, 60, 0 } * letters[] = { "A", "B", "C", "D", "F" } * Notice the lower range of the last entry should be the lowest possible numeric grade. * In the above example a "B" is awarded to any numeric grade < 90 and >= 80. The lower * bound is inclusive, the upper bound is exclusive (except for the implied upper bound of * the first grade which is not specified). * @param studentNumber specifies which student to compute grade. Note that students * are numbered starting at 1 (not 0). * @param ranges an array of lower bounds for each letter grade level. See comments above. * @param letters an array of grades corresponding to each letter grade level. See comments above. * @return the letter grade for the specified student. The return value is a string so * that + and - could be added as well. */ public String getStudentLetterGrade ( int studentNumber, double ranges[], String letters[] ) { double mean = getStudentGrade(studentNumber); int i = 0; // round up mean mean = Math.round(mean); while ( ranges[i] > mean && i < ranges.length ) i++; if ( i == ranges.length ) return "unknown"; else return letters[i]; } /** * This method searches for a student name and returns the corresponding number. */ private int getStudentNumber ( String name ) { for ( int i = 0; i < numStudents; i++ ) { if ( name.equals(names[i]) ) { return i; } } return -1; } /** * A method to print all student grades, means, and letter grades. */ public void print ( ) { System.out.printf("%10s ","Name"); for ( int j = 0; j < numExams; j++ ) System.out.printf("Exam %d ",j+1); System.out.printf("Average Grade\n"); for ( int i = 0; i < numStudents; i++ ) { System.out.printf("%10s ",names[i]); for ( int j = 0; j < numExams; j++ ) System.out.printf("%4d ",grades[i][j]); System.out.printf("%5.1f ",getStudentGrade(i+1)); System.out.printf("%5s\n",getStudentLetterGrade(i+1)); } } }