Question

In: Computer Science

You will be creating a JUnit Test Class for Gradebook.java, (listing 1.1) and you can download...

You will be creating a JUnit Test Class for Gradebook.java, (listing 1.1) and you can download it from blackboard.

Gradebook has two attributes: an array of int called scores to hold scores and scoreSize that indicates how many scores are currently held in the array. This field is initially set to 0.

Task #1:
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 field of the Gradebook separated by a space.

.
Task #2: Create the Test Class GradebookTester by right clicking on the GradeBook.java, select New, Junit Test Case.
Using the wizard:
Select the setUp and tearDown method check boxes and click Next.
Select all of the methods of Gradebook, except for the constructor to create tests for. Then click finish.

.
Task #3:
In the setUp method of GradebookTester, create at least two objects of Gradebook to hold 5 scores. Then call the addScore method for each of the Gradebook objects at least twice (but no more than 5 times) to add some random scores of your choice to the GradeBook objects
In the teardown method of GradebookTester, set the two objects of Gradebook to null.

Task #4: Create test for the methods of Gradebook:
addScore
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( . . .)
Compare the scoreSize to the expected number of scores entered, using assertEquals(. . .)
sum
Compare what is returned by sum() to the expected sum of the scores entered.
minimum
Compare what is returned by minimum() to the expected minimum of the scores entered.
finalScore
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.

Task #5: Upload both files to GitHub in a directory named CMSC203_Lab6.


Example:

As a private attribute (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 ”);

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

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

GradeBook.java

//Import required header files

import java.util.ArrayList;

//Declare the class GradeBook

class GradeBook {
  //Declare the local variables

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

  //Implement the method to return the scores size

  public int getScoreSize() {
    return scoresSize;
  }

  //Implement the method toString()

  public String toString() {
    String result = "";

    for (int i = 0; i < scoresSize; i++) {
      result += scores[i] + " ";
    }

    return result;
  }
}

GradeBookTester.java

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//Define the testclass

class GradeBookTester {
  //Create ibject for the class

  GradeBook g1;

  @Before
  //Implement the steUp method

  public void setUp() throws Exception {
    g1 = new GradeBook(5);

    g1.addScore(50.0);

    g1.addScore(75.0);
  }

  //Implement the tearDown method

  @After
  public void tearDown() throws Exception {
    g1 = null;
  }

  //Implement the testAddScore Method

  @Test
  public void testAddScore() {
    assertEquals(2.0, g1.getScoreSize(), 0.01);

    assertTrue(g1.toString().equals("50.0 75.0 "));
  }

  //Implemenet the testSum method

  @Test
  public void testSum() {
    assertEquals(125, g1.sum(), .0001);
  }

  //Implement the testMinimum method

  @Test
  public void testMinimum() {
    assertEquals(50.0, g1.minimum(), .0001);
  }

  //Implement the testFinalScore method

  @Test
  public void testFinalScore() {
    assertEquals(75.0, g1.finalScore(), .0001);
  }
}

Related Solutions

I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To test this Triangle class, you write a JUnit test class named TriangleTest.java. This test class contains multiple test cases in the format of methods. The test runner executes TriangleTest to generate a JUnit test report. In this project, you do not need to write the code for this Triangle class. Instead, you will write JUnit test cases to make sure that the implementation of...
How do I implement public class CircularArrayQueue<E> extends AbstractQueue <E> and test the methods with junit?
How do I implement public class CircularArrayQueue<E> extends AbstractQueue <E> and test the methods with junit?
I'm pretty new to JUnit testing. Can someone demonstrate how to test one of the tree...
I'm pretty new to JUnit testing. Can someone demonstrate how to test one of the tree traversals and the add() and addrecursive() methods? public class BinaryTree { private MorseNode<Character> root = new MorseNode<Character>(); private MorseNode<Character> left = null; private MorseNode<Character> right = null; public MorseNode<Character> getRoot() { return root; } public void printPostorder(MorseNode<Character> node) { if(node == null) {return;} //System.out.print(node.getLetter()); //traverse left subtree printPostorder(node.getLeft()); //traverse right subtree printPostorder(node.getRight()); //node if(node.getLetter() != null) { System.out.print(node.getLetter() + " ");} } public void...
You are required to write 4 significant JUnit tests for the Account class outlined below taking...
You are required to write 4 significant JUnit tests for the Account class outlined below taking into account the specifications below. Your code must include 3 negative test cases. Specifications for the Account class: The constructor takes as arguments the name, ID and the initial balance. The withdrawn method allows any specified amount between $20 and $1000 to be withdrawn but the withdrawn amount cannot exceed the current balance. The withdraw method is expected to throw the AmtTooLow exception if...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Objectives: • Learn to write test cases with Junit • Learn to debug code Problem: Sorting...
Objectives: • Learn to write test cases with Junit • Learn to debug code Problem: Sorting Book Objects Download the provided zipped folder from Canvas. The source java files and test cases are in the provided folder. The Book class models a book. A Book has a unique id, title, and author. The BookStore class stores book objects in a List, internally stored as an ArrayList. Also, the following methods are implemented for the BookStore class. • addBook(Book b): sto...
Create a project for this assignment. You can name it assignment02 if you wish. Download the...
Create a project for this assignment. You can name it assignment02 if you wish. Download the database file pizza-190807A.sqlite into the top level of the project. Create the pizza_services.py module first and put in the code given in the assignment. Using this code ensures that you can use the services in a similar way to the example. The assignment suggests adding a method customer to the class. This will return a list of rows from the customer table in the...
Part I – Understand a Given Class (die class) (40 pts) • Download the die class...
Part I – Understand a Given Class (die class) (40 pts) • Download the die class (attached as usingDieClass.cpp), save it as LabUseDieClassFirstName1_FirstName2.cpp • Add proper opening comments at the beginning of the program. The comments must include the description of all three parts of this lab. You may want to modify the comments after all parts are done to be sure that it is done properly. • Run the program and answer the following questions: o How many objects...
Download everything.c. You can compile and run it with the usual commands. You know that putting...
Download everything.c. You can compile and run it with the usual commands. You know that putting all your code into a single file is bad form. Therefore, your goal is to refactor everything.c so that the code is in multiple source (.c and .h) files, as well as write a Makefile to compile and run the program. Here are your constraints: There should be no code duplication Each .c and .h file must only #include header files that it actually...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT