You will be creating a JUnit Test Class for Gradebook.java, that has been provid
ID: 3681131 • Letter: Y
Question
You will be creating a JUnit Test Class for Gradebook.java, that has been provided for you.
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.
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 );
-------------------------------------------
Just Junit test
no need output.
just like this
@Test
public void testGetScoreSize() {
assertEquals(3,G1.getScoreSize());
assertEquals(3,G2.getScoreSize());
}
@Test
public void testToString() {
assertEquals
}
}
I dont know how to write testtostring and testAddScore
just need that part
Explanation / Answer
/*********** GradeBookTest.java ************/
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class GradeBookTest {
GradeBook g1;
GradeBook g2;
GradeBook g3;
@Before
public void setUp() throws Exception {
g1 = new GradeBook(5);
g1.addScore(50);
g1.addScore(43);
g1.addScore(22);
g1.addScore(4);
g1.addScore(100);
g2 = new GradeBook(5);
g2.addScore(43);
g2.addScore(34);
g2.addScore(67);
g2.addScore(82);
g2.addScore(97);
g3 = new GradeBook(5);
g3.addScore(13);
g3.addScore(25);
g3.addScore(90);
g3.addScore(78);
g3.addScore(100);
}
@After
public void tearDown() throws Exception {
g1 = null;
g2 = null;
g3 = null;
}
@Test
public void testAddScore() {
assertTrue(g1.toString().equals("50.0 43.0 22.0 4.0 100.0 "));
assertTrue(g2.toString().equals("43.0 34.0 67.0 82.0 97.0 "));
assertTrue(g3.toString().equals("13.0 25.0 90.0 78.0 100.0 "));
}
@Test
public void testSum() {
assertEquals(219, g1.sum(), .0001);
assertEquals(323, g2.sum(), .0001);
assertEquals(306, g3.sum(), .0001);
}
@Test
public void testMinimum() {
assertEquals(4, g1.minimum(), .001);
assertEquals(34, g2.minimum(), .001);
assertEquals(13, g3.minimum(), .001);
}
@Test
public void testFinalScore() {
assertEquals(215, g1.finalScore(), .0001);
assertEquals(289, g2.finalScore(), .0001);
assertEquals(293, g3.finalScore(), .0001);
}
}
/***** remaining methods for the given test class ***/
public int getScoreSize(int scoresSize){
return scoresSize;
}
public String toString(){
String out = "";
for(int i = 0; i <scores.length; i++){
out += (Double.toString(scores[i]) + " ");
}
return out;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.