Question: Modify the GradeBook class of Fig.7.16 so that the constructor accepts as parameters the number of students and the number of exams, then builds an
Modify the GradeBook class of Fig.7.16 so that the constructor accepts as parameters the number of students and the number of exams, then builds an appropriately sized two-dimensional array, rather than receiving a preinitialized two-dimensional array as it does now. Set each element of the new two-dimensional array to -1 to indicate that no grade has been entered for that element. Add a setGrade method that sets one grade for a particular student on a particular exam. Modify class GradeBookTest of Fig.7.17 to input the number of students and number of exams for the GradeBook and to allow the instructor to enter one grade at a time.
Fig.7.16
Fig.7.17
I // Fig. 7.16: GradeBook.java // GradeBook class using a two-dimensional array to store grades. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 III 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 public class GradeBook { private String courseName; // name of course this grade book represents private int [] [] grades; // two-dimensional array of student grades 148 149 } // two-argument constructor initializes courseName and grades array public GradeBook (String courseName, int [] [] grades) { this.courseName = courseName; this.grades grades; } // method to set the course name. public void setCourseName (String courseName) { this.courseName = courseName; } // method to retrieve the course name public String getCourseName() { return courseName; } // perform various operations on the data public void processGrades () { // output grades array output Grades(); // call methods getMinimum and getMaximum System.out.printf("%n%s %d%n%s %d%n%n", } } // find minimum grade public int getMinimum() { // assume first element of grades array is smallest int lowGrade = grades [0] [0]; // output grade distribution chart of all grades on all tests outputBarChart (); "Lowest grade in the grade book is", getMinimum(), "Highest grade in the grade book is", getMaximum()); // loop through rows of grades array for (int[] studentGrades : grades) { } } // find maximum grade public int getMaximum() { // assume first element of grades array is largest int highGrade = grades [0] [0]; return lowGrade; // loop through columns of current row for (int grade studentGrades) { // if grade less than lowGrade, assign it to lowGrade if (grade lowGrade) { lowGrade grade; // loop through rows of grades array for (int[] student Grades grades) { // loop through columns of current row for (int grade studentGrades) { // if grade greater than highGrade, assign it to highGrade if (grade return highGrade; // determine average grade for particular set of grades public double getAverage (int[] setofGrades) { int total = 0; // sum grades for one student for (int grade : setofGrades) { total grade; } // return average of grades return (double) total / setofGrades.length; } // output bar chart displaying overall grade distribution public void outputBarChart() { System.out.println("Overall grade distribution:"); // stores frequency of grades in each range of 10 grades int[] frequency = new int [11]; } highGrade) { highGrade grade; // for each grade in GradeBook, increment the appropriate frequency for (int[] studentGrades grades) { for (int grade studentGrades) { } } } // for each grade frequency, print bar in chart for (int count = 0; count < frequency.length; count++) { // output bar label ("00-09: ", ..., "90-99: ", "100: ") if (count==10) { System.out.printf("%5d: ", 100); } else { ++frequency [grade / 10]; } System.out.printf("%02d-%02d: ", count* 10, count* 10 + 9); } // print bar of asterisks for (int stars = 0; stars frequency [count]; stars++) { System.out.print("*"); } // output the contents of the grades array public void outputGrades () { System.out.printf("The grades are: %n%n"); System.out.print(" System.out.println(); "); // align column heads // create a column heading for each of the tests for (int test = 0; test < grades [0] .length; test++) { System.out.printf("Test %d ", test + 1); } System.out.println("Average"); // student average column heading // create rows/columns of text representing array grades for (int student = 0; student < grades.length; student++) { System.out.printf("Student %2d", student + 1); for (int test grades [student]) { // output student's grades System.out.printf("%8d", test); } // call method getAverage to calculate student's average grade; // pass row of grades as the argument to getAverage double average = getAverage (grades [student]); System.out.printf("%9.2f%n", average);
Step by Step Solution
3.44 Rating (160 Votes )
There are 3 Steps involved in it
To modify the GradeBook class according to your specifications we need to perform the following steps 1 Change the constructor to accept the number of ... View full answer
Get step-by-step solutions from verified subject matter experts
