I think main method is not required. Create a JUnit Test Class for Gradebook.jav
ID: 667995 • Letter: I
Question
I think main method is not required.
Create a JUnit Test Class for Gradebook.java, that has been provided for you below.
Add a getScoreSize() method to the Gradebook class which returns scoresSize;
Add a toString() method to the Gradebook class that returns a string with each score in scores separated by a space.
Then,
Create the Test Class GradebookTester.
1.Select the setUp and tearDown method.
2.Select all of the methods of Gradebook, except for the constructor to create tests for.
3.In the setUp method of GradebookTester, create at least two objects of Gradebook of size 5. Call the addScore method for each of the Gradebook classes at least twice (but no more than 5 times).
4.In the teardown method of GradebookTester, set the two objects of Gradebook to null;
5.Create test for the methods of Gradebook:
a, addScore
i. Use the toString method to compare the contents of what is in the scores array vs. what is expected to be in the scores array
assertTrue( . . .)
ii .Compare the scoreSize to the expected number of scores entered.
assertEquals(. . .)
b. sum
i.Compare what is returned by sum() to the expected sum of the scores entered.
C. minimum
i.Compare what is returned by minimum() to the expected minimum of the scores entered.
D.finalScore
i.Compare what is returned by finalScore() to the expected finalscore of the scores entered.
The finalScore is the sum of the scores, with the lowest score dropped if there are at least two scores, or 0 if there are no scores.
Example:
As a private member of GradeBookTest:
GradeBook g1;
In setup:
g1 = new GradeBook(5);
g1.addScore(50);
g1.addScore(75);
In teardown:
g1 = null;
intestSum():
assertEquals(125, g1.sum(), .0001);
intestMinimum():
assertEquals(50, g1.minimum(), .001);
inaddScoreTest();
assertTrue(g1.toString().equals( "50.0 75.0 ");
Here is the GradeBook programm:
Explanation / Answer
yes
I have created one more version of the code
// Fig. 5.9: GradeBook.java
// GradeBook class uses switch statement to count A, B, C, D and F grades.
import java.util.Scanner; // program uses class Scanner
public class GradeBook
{
private String courseName; // name of course this GradeBook represents
private int total; // sum of grades
private int gradeCounter; // number of grades entered
private int aCount; // count of A grades
private int bCount; // count of B grades
private int cCount; // count of C grades
private int dCount; // count of D grades
private int fCount; // count of F grades
// constructor initializes courseName;
// int instance variables are initialized to 0 by default
public GradeBook( String name )
{
courseName = name; // initializes courseName
} // end constructor
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
} // end method setCourseName
// method to retrieve the course name
public String getCourseName()
{
return courseName;
} // end method getCourseName
// display a welcome message to the GradeBook user
public void displayMessage()
{
// getCourseName gets the name of the course
System.out.printf( "Welcome to the grade book for %s! ",
getCourseName() );
} // end method displayMessage
// input arbitrary number of grades from user
public void inputGrades()
{
Scanner input = new Scanner( System.in );
int grade; // grade entered by user
System.out.printf( "%s %s %s %s ",
"Enter the integer grades in the range 0-100.",
"Type the end-of-file indicator to terminate input:",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter" );
// loop until user enters the end-of-file indicator
while ( input.hasNext() )
{
grade = input.nextInt(); // read grade
total += grade; // add grade to total
++gradeCounter; // increment number of grades
// call method to increment appropriate counter
incrementLetterGradeCounter( grade );
} // end while
} // end method inputGrades
// add 1 to appropriate counter for specified grade
public void incrementLetterGradeCounter( int grade )
{
// determine which grade was entered
switch ( grade / 10 )
{
case 9: // grade was between 90
case 10: // and 100
++aCount; // increment aCount
break; // necessary to exit switch
case 8: // grade was between 80 and 89
++bCount; // increment bCount
break; // exit switch
case 7: // grade was between 70 and 79
++cCount; // increment cCount
break; // exit switch
case 6: // grade was between 60 and 69
++dCount; // increment dCount
break; // exit switch
default: // grade was less than 60
++fCount; // increment fCount
break; // optional; will exit switch anyway
} // end switch
} // end method incrementLetterGradeCounter
// display a report based on the grades entered by user
public void displayGradeReport()
{
System.out.println( " Grade Report:" );
// if user entered at least one grade...
if ( gradeCounter != 0 )
{
// calculate average of all grades entered
double average = (double) total / gradeCounter;
// output summary of results
System.out.printf( "Total of the %d grades entered is %d ",
gradeCounter, total );
System.out.printf( "Class average is %.2f ", average );
System.out.printf( "%s %s%d %s%d %s%d %s%d %s%d ",
"Number of students who received each grade:",
"A: ", aCount, // display number of A grades
"B: ", bCount, // display number of B grades
"C: ", cCount, // display number of C grades
"D: ", dCount, // display number of D grades
"F: ", fCount ); // display number of F grades
} // end if
else // no grades were entered, so output appropriate message
System.out.println( "No grades were entered" );
} // end method displayGradeReport
} // end class GradeBook