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.

xxxxxxxxxxxxxx LabProgram.java, Tests.java, Gradebook.java, and TestUtility.java are marked as read-only. you can

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> testCases = new ArrayList>( Arrays.asList(new Tuple("Midterm", 11111, 91.0), new Tuple("Homework 1", 22222, Double.NaN), new Tuple("Homework 3", 55555, 71.5), new Tuple("Course project", 66666, 0.0), new Tuple("Homework 2", 10000, 90.0), new Tuple("Homework 4", 55555, 77.5), new Tuple("Homework 5", 33333, Double.NaN), new Tuple("Final exam", 44444, Double.NaN), new Tuple("Homework 2", 77777, 76.0), new Tuple("Homework 1", 88888, 64.5)));

// 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 hw2Scores = new HashMap() {{ put(11111, 89.0); put(22222, 75.0); put(33333, 100.0); put(44444, 50.0); put(55555, 76.5); put(66666, 84.5); put(77777, 76.0); put(88888, 74.5); put(99999, 100.0); put(10000, 90.0); put(90000, 85.0); }}; HashMap midtermScores = new HashMap() {{ put(11111, 91.0); put(22222, 77.5); put(33333, 88.0); put(44444, 40.0); put(55555, 64.5); put(66666, 91.0); put(77777, 75.0); put(88888, 88.0); put(99999, 88.0); put(10000, 92.0); put(90000, 90.0); }}; HashMap projectScores = new HashMap() {{ put(11111, 100.0); put(22222, 60.0); put(33333, 90.0); put(55555, 87.0); put(66666, 0.0); put(77777, 72.0); put(88888, 85.5); put(99999, 80.0); put(10000, 77.5); put(90000, 85.0); }};

// Each test case is a (assignmentName, mapOfExpectedScores) Pair ArrayList>> testCases = new ArrayList>>( Arrays.asList(new Pair>("Homework 2", hw2Scores), new Pair>("Midterm", midtermScores), new Pair>("Course project", projectScores)));

// Iterate through all test cases for (var testCase : testCases) { String assignmentName = testCase.getVar0(); HashMap expectedMap = testCase.getVar1();

// Get the actual map from the gradebook System.out.print("Calling getAssignmentScores(\""); System.out.print(assignmentName); System.out.print("\")"); System.out.print(" "); HashMap actualMap = gradebook.getAssignmentScores(assignmentName);

// 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 expected = new ArrayList(Arrays.asList("Course project", "Final exam", "Homework 1", "Homework 2", "Homework 3", "Homework 4", "Midterm")); ArrayList actual = gradebook.getSortedAssignmentNames();

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 expected = new ArrayList( Arrays.asList(10000, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 90000, 99999)); ArrayList actual = gradebook.getSortedStudentIDs(); 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: 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 student22222Scores = new HashMap() {{ put("Homework 2", 75.0); put("Midterm", 77.5); put("Homework 3", 80.5); put("Homework 4", 81.0); put("Course project", 60.0); put("Final exam", 54.0); }}; HashMap student44444Scores = new HashMap() {{ put("Homework 1", 60.0); put("Homework 2", 50.0); put("Midterm", 40.0); put("Homework 3", 30.0); }}; HashMap student88888Scores = new HashMap() {{ put("Homework 1", 64.5); put("Homework 2", 74.5); put("Midterm", 88.0); put("Homework 3", 84.0); put("Homework 4", 84.0); put("Course project", 85.5); put("Final exam", 81.5); }}; HashMap student90000Scores = new HashMap() {{ put("Homework 1", 80.0); put("Homework 2", 85.0); put("Midterm", 90.0); put("Homework 3", 95.0); put("Homework 4", 100.0); put("Course project", 85.0); put("Final exam", 94.5); }};

// Each test case is a (studentID, mapOfExpectedScores) Pair ArrayList>> testCases = new ArrayList>>( Arrays.asList(new Pair>(22222, student22222Scores), new Pair>(44444, student44444Scores), new Pair>(88888, student88888Scores), new Pair>(90000, student90000Scores)));

// Iterate through all test cases for (var testCase : testCases) { Integer studentID = testCase.getVar0(); HashMap expectedMap = testCase.getVar1();

// Get the actual map from the gradebook System.out.print("Calling getStudentScores("); System.out.print(studentID); System.out.print(")"); System.out.print(" "); HashMap actualMap = gradebook.getStudentScores(studentID);

// 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 T var0; public U var1;

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 T var0; public U var1; public V var2;

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 getAssignmentScores(String assignmentName);

// getSortedAssignmentNames() returns an ArrayList with all distinct assignment // names, sorted in ascending order. public abstract ArrayList getSortedAssignmentNames();

// getSortedStudentIDs() returns an ArrayList with all distinct student IDs, // sorted in ascending order. public abstract ArrayList getSortedStudentIDs();

// 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 getStudentScores(Integer studentID); }

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> rows) {

// 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> rows = new ArrayList>() {{ add(new ArrayList(Arrays.asList( "Student ID", "Homework 1", "Homework 2", "Midterm", "Homework 3", "Homework 4", "Course project", "Final exam"))); add(new ArrayList(Arrays.asList("11111", "92", "89", "91", "100", "100", "100", "95"))); add(new ArrayList(Arrays.asList("22222", "", "75", "77.5", "80.5", "81", "60", "54"))); add(new ArrayList(Arrays.asList("33333", "100", "100", "88", "100", "100", "90", "77.5"))); add(new ArrayList(Arrays.asList("44444", "60", "50", "40", "30", "", "", ""))); add(new ArrayList(Arrays.asList("55555", "73.5", "76.5", "64.5", "71.5", "77.5", "87", "63.5"))); add(new ArrayList(Arrays.asList("66666", "82.5", "84.5", "91", "92.5", "86", "0", "97"))); add(new ArrayList(Arrays.asList("77777", "77", "76", "75", "74", "73", "72", "71"))); add(new ArrayList(Arrays.asList("88888", "64.5", "74.5", "88", "84", "84", "85.5", "81.5"))); add(new ArrayList(Arrays.asList("99999", "100", "100", "88", "100", "100", "80", "79"))); add(new ArrayList(Arrays.asList("10000", "88", "90", "92", "87", "88.5", "77.5", "90"))); add(new ArrayList(Arrays.asList("90000", "80", "85", "90", "95", "100", "85", "94.5"))); }};

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> scoresByAssignment;

private HashMap> scoresByStudent;

// 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 getAssignmentScores(String assignmentName) {

if (scoresByAssignment.containsKey(assignmentName)) {

return new HashMap(scoresByAssignment.get(assignmentName));

} else {

return new HashMap();

}

}

// getSortedAssignmentNames method

public ArrayList getSortedAssignmentNames() {

ArrayList sortedNames = new ArrayList(scoresByAssignment.keySet());

sortedNames.sort(null);

return sortedNames;

}

// getSortedStudentIDs method

public ArrayList getSortedStudentIDs() {

ArrayList sortedIDs = new ArrayList(scoresByStudent.keySet());

sortedIDs.sort(null);

return sortedIDs;

}

// getStudentScores method

public HashMap getStudentScores(int studentID) {

if (scoresByStudent.containsKey(studentID)) {

return new HashMap(scoresByStudent.get(studentID));

} else {

return new HashMap();

}

}

}

The CourseGradebook.java have a problem, please give the correct code.

only change CourseGradebook.java. Also, I have written the code of CourseGradebook.java, please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!