Question: xxxxxxxxxxxxxx LabProgram.java, Tests.java, Gradebook.java, and TestUtility.java are marked as read-only. you can only change CourseGradebook.java. Also, I have written the code of CourseGradebook.java, please fixed
xxxxxxxxxxxxxx LabProgram.java, Tests.java, Gradebook.java, and TestUtility.java are marked as read-only. you can only change CourseGradebook.java.
Also, I have written the code of CourseGradebook.java, please fixed for me and give me the correct code.

XXXXXXXXX Tests.java:
import java.util.*;
public class Tests { public static boolean testGetScoreAndSetScore() { System.out.print(" "); System.out.print("---- testGetScoreAndSetScore() ----"); System.out.print(" ");
// Create a gradebook with sample data for testing CourseGradebook gradebook = TestUtility.makeSampleGradebook();
// Each test case is a (assignmentName, studentID, expectedScore) Tuple ArrayList
// Iterate through test cases for (var testCase : testCases) { String assignmentName = testCase.getVar0(); Integer studentID = testCase.getVar1(); Double expected = testCase.getVar2(); Double actual = gradebook.getScore(assignmentName, studentID);
// Reminder: Can't compare NaN with ==, so a special case is needed boolean areEqual = expected.isNaN() ? actual.isNaN() : (Double.compare(actual, expected) == 0);
if (areEqual) { System.out.print("PASS: getScore(\""); System.out.print(assignmentName); System.out.print("\", "); System.out.print(studentID); System.out.print(") returned "); System.out.print(actual); System.out.print(" "); } else { System.out.print("FAIL: getScore(\""); System.out.print(assignmentName); System.out.print("\", "); System.out.print(studentID); System.out.print(") returned "); System.out.print(actual); System.out.print(", but expected is "); System.out.print(expected); System.out.print(" "); return false; } } return true; }
public static boolean testGetAssignmentScores() { System.out.print(" "); System.out.print("---- testGetAssignmentScores() ----"); System.out.print(" ");
// Create a gradebook with sample data for testing CourseGradebook gradebook = TestUtility.makeSampleGradebook();
HashMap
// Each test case is a (assignmentName, mapOfExpectedScores) Pair ArrayList
// Iterate through all test cases for (var testCase : testCases) { String assignmentName = testCase.getVar0(); HashMap
// Get the actual map from the gradebook System.out.print("Calling getAssignmentScores(\""); System.out.print(assignmentName); System.out.print("\")"); System.out.print(" "); HashMap
// Compare sizes first if (expectedMap.size() != actualMap.size()) { System.out.print("FAIL: getAssignmentScores(\""); System.out.print(assignmentName); System.out.print("\") returned a map with "); if (1 == actualMap.size()) { System.out.print("1 score, "); } else { System.out.print(actualMap.size()); System.out.print(" scores, "); } System.out.print("but the expected map has "); System.out.print(expectedMap.size()); System.out.print(" scores"); System.out.print(" "); return false; }
// Sizes are equal, so now compare each ID/score pair for (Integer key : expectedMap.keySet()) { Integer studentID = key; if (!actualMap.containsKey(studentID)) { System.out.print("FAIL: getAssignmentScores(\""); System.out.print(assignmentName); System.out.print("\") returned a map that is missing an entry "); System.out.print("for student ID "); System.out.print(studentID); System.out.print(" "); return false; }
// Actual map has student ID, so now compare corresponding score Double expectedScore = expectedMap.get(key); Double actualScore = actualMap.get(studentID); boolean areEqual = expectedScore.isNaN() ? actualScore.isNaN() : (Double.compare(actualScore, expectedScore) == 0); if (!areEqual) { System.out.print("FAIL: getAssignmentScores(\""); System.out.print(assignmentName); System.out.print("\") returned a map that has a score of "); System.out.print(actualScore); System.out.print(" for student ID "); System.out.print(studentID); System.out.print(", but the expected score is "); System.out.print(expectedScore); System.out.print(" "); return false; } }
// All entries match System.out.print("PASS: getAssignmentScores(\""); System.out.print(assignmentName); System.out.print("\") returned a map with "); System.out.print(actualMap.size()); System.out.print(" correct scores"); System.out.print(" "); } return true; }
public static boolean testGetSortedAssignmentNames() { System.out.print(" "); System.out.print("---- testGetSortedAssignmentNames() ----"); System.out.print(" "); CourseGradebook gradebook = TestUtility.makeSampleGradebook();
ArrayList
boolean areEqual = true; if (actual.size() == expected.size()) { // Compare elements in order for (int i = 0; areEqual && i
// Show pass or fail message along with expected and actual ArrayList contents if (areEqual) { System.out.print("PASS: getSortedAssignmentNames()"); System.out.print(" "); } else { System.out.print("FAIL: getSortedAssignmentNames()"); System.out.print(" "); } System.out.print(" Expected: "); System.out.println(expected); System.out.print(" Actual: "); System.out.println(actual);
return areEqual; }
public static boolean testGetSortedStudentIDs() { System.out.print(" "); System.out.print("---- testGetSortedStudentIDs() ----"); System.out.print(" "); CourseGradebook gradebook = TestUtility.makeSampleGradebook();
ArrayList // Show pass or fail message along with expected and actual ArrayList contents if (areEqual) { System.out.print("PASS: getSortedStudentIDs()"); System.out.print(" "); } else { System.out.print("FAIL: getSortedStudentIDs()"); System.out.print(" "); } System.out.print(" Expected: "); System.out.println(expected); System.out.print(" Actual: "); System.out.println(actual); return areEqual; } public static boolean testGetStudentScores() { System.out.print(" "); System.out.print("---- testGetStudentScores() ----"); System.out.print(" "); CourseGradebook gradebook = TestUtility.makeSampleGradebook(); HashMap // Each test case is a (studentID, mapOfExpectedScores) Pair ArrayList // Iterate through all test cases for (var testCase : testCases) { Integer studentID = testCase.getVar0(); HashMap // Get the actual map from the gradebook System.out.print("Calling getStudentScores("); System.out.print(studentID); System.out.print(")"); System.out.print(" "); HashMap // Compare sizes first if (expectedMap.size() != actualMap.size()) { System.out.print("FAIL: getStudentScores("); System.out.print(studentID); System.out.print(") returned a map with "); if (1 == actualMap.size()) { System.out.print("1 score, "); } else { System.out.print(actualMap.size()); System.out.print(" scores, "); } System.out.print("but the expected map has "); System.out.print(expectedMap.size()); System.out.print(" scores"); System.out.print(" "); return false; } // Sizes are equal, so now compare each assignment name/score pair for (String key : expectedMap.keySet()) { String assignmentName = key; if (!actualMap.containsKey(assignmentName)) { System.out.print("FAIL: getStudentScores("); System.out.print(studentID); System.out.print(") returned a map that is missing an entry for "); System.out.print("assignment \""); System.out.print(assignmentName); System.out.print("\""); System.out.print(" "); return false; } // Actual map has assignment name, so now compare corresponding score Double expectedScore = expectedMap.get(key); Double actualScore = actualMap.get(assignmentName); boolean areEqual = expectedScore.isNaN() ? actualScore.isNaN() : (Double.compare(actualScore, expectedScore) == 0); if (!areEqual) { System.out.print("FAIL: getStudentScores("); System.out.print(studentID); System.out.print(") returned a map that has a score of "); System.out.print(actualScore); System.out.print(" for assignment \""); System.out.print(assignmentName); System.out.print("\", but the expected score is "); System.out.print(expectedScore); System.out.print(" "); return false; } } // All entries match System.out.print("PASS: getStudentScores("); System.out.print(studentID); System.out.print(") returned a map with "); System.out.print(actualMap.size()); System.out.print(" correct scores"); System.out.print(" "); } return true; } } //Basic Pair class for test cases class Pair public Pair(T x, U y) { var0 = x; var1 = y; } public T getVar0() { return var0; } public U getVar1() { return var1; } } //Basic Tuple class for test cases class Tuple public Tuple(T x, U y, V z) { var0 = x; var1 = y; var2 = z; } public T getVar0() { return var0; } public U getVar1() { return var1; } public V getVar2() { return var2; } } xxxxxxxxx Gradebook.java : import java.util.*; public abstract class Gradebook { // getScore() returns the specified student's score for the specified // assignment. NaN is returned if either: // - the assignment does not exist in the gradebook, or // - the assignment exists but no score exists for the specified student. public abstract double getScore(String assignmentName, Integer studentID); // setScore() adds or updates a score in the gradebook. public abstract void setScore(String assignmentName, Integer studentID, Double score); // getAssignmentScores() returns a HashMap that maps a student ID to // the student's corresponding score for the specified assignment. An entry // exists in the returned map only if a score has been entered with the // setScore() function. public abstract HashMap // getSortedAssignmentNames() returns an ArrayList with all distinct assignment // names, sorted in ascending order. public abstract ArrayList // getSortedStudentIDs() returns an ArrayList with all distinct student IDs, // sorted in ascending order. public abstract ArrayList // getStudentScores() gets all scores that exist in the gradebook for the // student whose ID matches the method parameter. getStudentScores() // returns a HashMap that maps an assignment name to the student's // corresponding score for that assignment. public abstract HashMap XXXXXXXXX TestUtility.java: import java.util.*; public class TestUtility { // Populates a CourseGradebook from an ArrayList of rows. Each row is an ArrayList // of Strings. Row 0 must be the header row. Column 0 must be the student ID // column. public static void populateGradebookFromRows(CourseGradebook gradebook, final ArrayList // Iterate through non-header rows for (int rowIndex = 1; rowIndex // Parse out student ID first int studentID = Integer.parseInt(row.get(0)); // Call setScore() for each non-empty entry for (int colIndex = 1; colIndex 0) { // Get the assignment name from the header row String assignmentName = rows.get(0).get(colIndex); // Convert score from string to double double score = Double.parseDouble(entry); // Add to gradebook gradebook.setScore(assignmentName, studentID, score); } } } } // Returns a sample gradebook to use for testing purposes. public static CourseGradebook makeSampleGradebook() { ArrayList CourseGradebook gradebook = new CourseGradebook(); populateGradebookFromRows(gradebook, rows); return gradebook; } } XXXXXXXXX CourseGradebook.java: import java.util.ArrayList; import java.util.HashMap; public class CourseGradebook extends Gradebook { // Fields to store gradebook data private HashMap private HashMap // Constructor public CourseGradebook() { scoresByAssignment = new HashMap(); scoresByStudent = new HashMap(); } // setScore method public void setScore(String assignmentName, int studentID, double score) { if (!scoresByAssignment.containsKey(assignmentName)) { scoresByAssignment.put(assignmentName, new HashMap } scoresByAssignment.get(assignmentName).put(studentID, score); if (!scoresByStudent.containsKey(studentID)) { scoresByStudent.put(studentID, new HashMap } scoresByStudent.get(studentID).put(assignmentName, score); } // getScore method public Double getScore(String assignmentName, int studentID) { if (scoresByAssignment.containsKey(assignmentName) && scoresByAssignment.get(assignmentName).containsKey(studentID)) { return scoresByAssignment.get(assignmentName).get(studentID); } else { return Double.NaN; } } // getAssignmentScores method public HashMap if (scoresByAssignment.containsKey(assignmentName)) { return new HashMap } else { return new HashMap } } // getSortedAssignmentNames method public ArrayList ArrayList sortedNames.sort(null); return sortedNames; } // getSortedStudentIDs method public ArrayList ArrayList sortedIDs.sort(null); return sortedIDs; } // getStudentScores method public HashMap if (scoresByStudent.containsKey(studentID)) { return new HashMap } else { return new HashMap } } } The CourseGradebook.java have a problem, please give the correct code. 
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
