Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JUnit Test Classes You will be creating a JUnit Test Class for Gradebook.java, that has been provided for you. Upload Gradebook.java and GradeBookTest.java to the

JUnit Test Classes

You will be creating a JUnit Test Class for Gradebook.java, that has been provided for you. Upload Gradebook.java and GradeBookTest.java to the repository in GitHub you created in Lab 1 in a directory named JUnit_Lab.

Add the following two methods to the Gradebook class. Do not modify the other methods of Gradebook.

  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:
    1. addScore
      1. 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( . . .)

  1. Compare the scoreSize to the expected number of scores entered.

assertEquals(. . .)

  1. sum
    1. Compare what is returned by sum() to the expected sum of the scores entered.
  2. minimum
    1. Compare what is returned by minimum() to the expected minimum of the scores entered.
  3. finalScore
    1. 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:

Add 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 );

GradeBook.java

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(); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions

Question

What is the difference between anabolism and catabolism?

Answered: 1 week ago

Question

sharing of non-material benefits such as time and affection;

Answered: 1 week ago