Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The java class is provided. You only need to create the test class going by the

ID: 3689391 • Letter: T

Question

The java class is provided. You only need to create the test class going by the instruction.

You will be creating a JUnit Test Class for Gradebook.java, that has been provided for you.

1.      Add a getScoreSize() method to the Gradebook class which returns scoresSize;

2.      Add a toString() method to the Gradebook class that returns a string with each score in scores separated by a space.

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;

in testSum():

assertEquals(125, g1.sum(), .0001);

in testMinimum():

            assertEquals(50, g1.minimum(), .001);

in addScoreTest();

            assertTrue(g1.toString().equals(“50.0 75.0 ”);

import java.util.ArrayList;

public class GradeBook

{

   private double[] scores;

   private int scoresSize;

   /**

      Constructs a gradebook with no scores and a given capacity.

      @capacity the maximum number of scores in this gradebook

   */

   public GradeBook(int capacity)

   {

      scores = new double[capacity];

      scoresSize = 0;

   }

   /**

      Adds a score to this gradebook.

      @param score the score to add

      @return true if the score was added, false if the gradebook is full

   */

   public boolean addScore(double score)

   {

      if (scoresSize < scores.length)

      {

         scores[scoresSize] = score;

         scoresSize++;

         return true;

      }

      else

         return false;     

   }

   /**

      Computes the sum of the scores in this gradebook.

      @return the sum of the scores

   */

   public double sum()

   {

      double total = 0;

      for (int i = 0; i < scoresSize; i++)

      {

         total = total + scores[i];

      }

      return total;

   }

     

   /**

      Gets the minimum score in this gradebook.

      @return the minimum score, or 0 if there are no scores.

   */

   public double minimum()

   {

      if (scoresSize == 0) return 0;

      double smallest = scores[0];

      for (int i = 1; i < scoresSize; i++)

      {

         if (scores[i] < smallest)

         {

            smallest = scores[i];

         }

      }

      return smallest;

   }

   /**

      Gets the final score for this gradebook.

      @return the sum of the scores, with the lowest score dropped if

      there are at least two scores, or 0 if there are no scores.

   */

   public double finalScore()

   {

      if (scoresSize == 0)

         return 0;

      else if (scoresSize == 1)

         return scores[0];

      else

         return sum() - minimum();

   }

}

Explanation / Answer

Hi below i have written the sample code for Gradebook in Java for your reference.

public class GradeBook {  

    private String courseName;  

    private int[] grades;  

  

    public GradeBook(String name, int[] gradesArray) {  

        courseName = name;  

        grades = gradesArray;  

    }  

  

    public void setCourseName(String name) {  

        courseName = name;  

    }  

  

    public String getCourseName() {  

        return courseName;  

    }  

  

    public void displayMessage() {  

        System.out.printf("Welcome to the grade book for %s! ",  

                getCourseName());  

    }  

  

    public void processGrades() {  

        outputGrades();  

        System.out.printf(" Class average is %.2f ", getAverage());  

        System.out.printf("Lowest grade is %d Highest grade is %d ",  

                getMinimum(), getMaximum());  

        outputBarChart();  

    }  

  

    public int getMinimum() {  

        int lowGrade = grades[0];  

        for (int grade : grades) {  

            if (grade < lowGrade)  

                lowGrade = grade;  

        }  

        return lowGrade;  

    }  

  

    public int getMaximum() {  

        int highGrade = grades[0];  

        for (int grade : grades) {  

            if (grade > highGrade)  

                highGrade = grade;  

        }  

        return highGrade;  

    }  

  java class which will enable us to test the above class and methods

public class GradeBookTest {  

  

    public static void main(String[] args) {  

  

        // array of student grades. This is the data that we will use to  

        // populate the array.  

        // There are many ways of obtaining data to populate arrays, and some of  

        // those ways are by user-input,  

        // or by reading from a file, or in this case, we provide the data using  

        // an array initializer list.  

        int[] gradesArray = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };  

  

        GradeBook myGradeBook = new GradeBook("Java Programming", gradesArray);  

  

        myGradeBook.displayMessage();  

        myGradeBook.processGrades();  

    }  

}

    public double getAverage() {  

        int total = 0;  

        for (int grade : grades)  

            total += grade;  

        return (double) total / grades.length;  

    }  

  

    public void outputBarChart() {  

        System.out.println("Grade distribution: ");  

        int[] frequency = new int[11];  

        for (int grade : grades)  

            ++frequency[grade / 10];  

        for (int count = 0; count < frequency.length; count++) {  

            if (count == 10)  

                System.out.printf("%5d: ", 100);  

            else  

                System.out.printf("%02d-%02d: ", count * 10, count * 10 + 9);  

            for (int stars = 0; stars < frequency[count]; stars++)  

                System.out.print("*");  

            System.out.println();  

        }  

    }  

  

    public void outputGrades() {  

        System.out.println("The grades are: ");  

        for (int student = 0; student < grades.length; student++)  

            System.out.printf("Student %2d: %3d ", student + 1,  

                    grades[student]);  

    }  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote