public class CourseGrader { //Instance Variables private int finalExamScore; //Final Exam Score private int midtermScore; //Midterm Score private int[] homeworkScores; //Homework Scores private boolean dropLowScore; //Flag to drop lowest Homework Score //Constructor // @param scores: [final, midterm, hw1, hw2, hw3, ...] public CourseGrader(int[] scores) { //Check the validity of the parameter this.validate(scores); //Extract Final Exam and Midterm Scores from scores array this.finalExamScore = 0; //*** a: Final Exam Score this.midtermScore = 0; //*** a: Midterm Score //Extract the Homework Scores from the scores array this.homeworkScores = new int[1]; //*** b: Size the HW array for (int index = 0; index < homeworkScores.length; index++) this.homeworkScores[index] = 0; //*** c: Assign a HW score //By default, all Homework Scores are counted towards the Class Grade this.dropLowScore = false; } //Constructor Helper // @param scores: [final, midterm, hw1, hw2, hw3, ...] private static void validate(int[] scores) { //Exception thrown if there are fewer than 2 scores altogether if ( false ) //*** d: Test for too few scores throw new RuntimeException("Must have at least 2 scores"); //Exception thrown if any score is outside of 0 .. 100 range for (int aScore : scores) if ( false ) //*** e: Test for an invalid score throw new RuntimeException("Invalid score: " + aScore); } //Accessor - returns Final Exam Score public int getFinalExamScore() { return this.finalExamScore; } //Accessor - returns Midterm Exam Score public int getMidtermScore() { return this.midtermScore; } //Accessor - returns copies of all Homework Scores public int[] getHomeworkScores() { //Create the (copy) array that will be returned; int[] scores = new int[ 1 ]; //*** f: Make a new array for (int index = 0; index < scores.length; index++) scores[ 0 ] = this.homeworkScores[ 0 ];//*** g: Copy over a score //Return copies of this Homework Scores return scores; } //Accesor - returns 1 Homework Score selected by the parameter // @param number: the number of the selected Homework, 1 ... N public int getHomeworkScore(int number) { //Index of selected score is 1 less than the given number // number 1 selects homework[0], etc. return this.homeworkScores[number - 1]; } //Accessor - check whether lowest Homework Score will be dropped public boolean getDropLowScore() { return this.dropLowScore; } //Mutator - set the flag to indicate dropping the lowest Homework Score public void setDropLowScore() { if ( false ) //*** h: Fewer than 2 HW scores? throw new RuntimeException("Insufficient HW scores to drop lowest"); this.dropLowScore = true; } //Returns the smallest of the Homework Scores private int lowestHomeworkScore() { if (this.homeworkScores.length == 0) throw new RuntimeException("There are no Homework Scores"); int lowestScore = this.homeworkScores[0]; /* for ( ) //*** i: loop over all HW scores if (aScore < lowestScore) lowestScore = aScore; */ return lowestScore; } //Class constants for the LetterGrade cut-off's and designations public static final int[] GRADE_MINIMUMS = {90, 80, 65, 50}; public static final char[] LETTER_GRADES = {'A', 'B', 'C', 'D', 'F'}; //Returns a Letter Grade based on a Weighted Average of the // Final, Midterm and Homework Scores public char letterGrade() { int average = this.classAverage(); /* for ( ) //*** j: loop over all GRADE_MINIMUMS { int grade_cut_off = GRADE_MINIMUMS[0]; //*** k: selected GRADE_MINIMUMS element if ( average >= grade_cut_off ) return LETTER_GRADES[0]; //*** l: selected LATTER_GRADES element } */ return 'F'; } //Returns the average of the HomeworkScores, excluding the lowest // only when the this dropLowScore is set (true) public int homeworkAverage() { if (this.homeworkScores.length == 0) throw new RuntimeException("There are no Homework Scores"); //Find the total of all Homework Scores int homeworkTotal = 0; for (int aScore : this.homeworkScores) homeworkTotal += aScore; //Adjust for dropping the lowest Homework Score int homeworkCount = this.homeworkScores.length; if ( this.dropLowScore ) { homeworkTotal -= this.lowestHomeworkScore(); homeworkCount--; } double average = (double)homeworkTotal / homeworkCount; return (int)( average + 0.5 ); } //Class Constants for the Weights of the Course Grade Components public static final int WEIGHT_FINAL = 40; //Final Exam Weight public static final int WEIGHT_MIDTERM = 30; //Midterm Weight public static final int WEIGHT_HOMEWORK = 30;//Homework Weight //Returns the weighted average of Final, Midterm and Homework Scores public int classAverage() { //Scores Total and Sum of Weights for Final and Midterm Scores int weightedTotal = this.finalExamScore * WEIGHT_FINAL + this.midtermScore * WEIGHT_MIDTERM ; double sumOfWeights = WEIGHT_FINAL + WEIGHT_MIDTERM; //Include Homework Scores and Weight if appropriate if ( this.homeworkScores.length > 0 ) { weightedTotal += this.homeworkAverage() * WEIGHT_HOMEWORK; sumOfWeights += WEIGHT_HOMEWORK; } //Calculate and return the weighted class average score, rounded double weightedAverage = weightedTotal / sumOfWeights; return (int)( weightedAverage + 0.5 ); } }