i am having trouble with my code at test (addScore) thanks. this is GradeBookTest import static org.junit.Assert.assertTrue;
Question:
i am having trouble with my code at test (addScore)
thanks.
this is GradeBookTest
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;
class GradeBookTest {
GradeBook books0, books1;
@BeforeEach void setUp() throws Exception { books0 = new GradeBook(5); books1 = new GradeBook(5); books0.addScore(50); books0.addScore(75); books0.addScore(105); books1.addScore(25); books1.addScore(69);
}
@AfterEach void tearDown() throws Exception { books0 = null; books1 = null; }
@Test void addScore() {
assertTrue(books0.toString().equals("50 75 105")); assertEquals(3, books0.getScoreSize());
assertTrue(books1.toString().equals("25 69")); assertEquals(2, books1.getScoreSize());
}
@Test void sum() { assertEquals(230, books0.sum(), .001); assertEquals(94, books1.sum(), .001);
}
@Test void minimum() { assertEquals(50, books0.minimum(), 0.001); assertEquals(25, books1.minimum(), 0.001); }
@Test void finalScore() { assertEquals(180, books0.finalScore(), .001); assertEquals(69, books1.finalScore(), .001); }
}
this is Gradebook
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(); }
public int getScoreSize() { return scoresSize; }
@Override public String toString() {
String value = "";
for (int i = 0; i < scoresSize; i++) {
value += scores[i] + " "; // remember to add a space here!
} return value; } }