-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeBook.java
More file actions
124 lines (110 loc) · 2.52 KB
/
GradeBook.java
File metadata and controls
124 lines (110 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package gradebook;
import java.util.ArrayList;
public class GradeBook{
private double[] scores;
private int scoresSize;
public GradeBook(int capacity){
scores = new double[capacity];
scoresSize = 0;
}
//Task1
//---------------------------------------------------
public int getScoreSize(){
return this.scoresSize;
}
@Override
public String toString(){
String scoreString = "";
for(int i=0;i<scoresSize;i++){
scoreString += Double.toString(scores[i]) + " ";
}
return scoreString;
}
//---------------------------------------------------
public boolean addScore(double score){
if(scoresSize<scores.length){
scores[scoresSize] = score;
scoresSize++;
return true;
}
else
return false;
}
public double sum(){
double total = 0;
for(int i=0;i<scoresSize;i++){
total = total + scores[i];
}
return total;
}
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;
}
public double finalScore(){
if(scoresSize==0)
return 0;
else if(scoresSize==1)
return scores[0];
else
return sum()-minimum();
}
}
GradeBookTester.java:
package gradebook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class GradeBookTester {
private GradeBook g1;
private GradeBook g2;
@Before
public void setUp() {
//Creating two new object of GradeBook.
g1 = new GradeBook(5);
g2 = new GradeBook(5);
//Adding the score to each object.
g1.addScore(50);
g1.addScore(75);
g2.addScore(80);
g2.addScore(90);
}
@After
public void tearDown() {
//Setting the both object to null.
g1 = null;
g2 = null;
}
@Test
public void testSum(){
//Checking the actual sum and sum returned by the sum() are equivalent.
assertEquals(125,g1.sum(),0.001);
assertEquals(170,g2.sum(),0.001);
}
@Test
public void testMinimum(){
//Checking if the minimum score returned by minimum() is equals to actual minimum value.
assertEquals(50,g1.minimum(),0.001);
assertEquals(80,g2.minimum(),0.001);
}
@Test
public void addScoreTest(){
//Checking if the returned string is equal to expected string.
assertTrue(g1.toString().equals("50.0 75.0 "));
assertTrue(g2.toString().equals("80.0 90.0 "));
}
@Test
public void finalScoreTest(){
//Since there are two scores in both g1 and g2 the finalScore will return sum of all scores - minimum score.
assertEquals(75,g1.finalScore(),0.001);
assertEquals(90,g2.finalScore(),0.001);
}
}