-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCR3.java
More file actions
45 lines (36 loc) · 921 Bytes
/
Copy pathCR3.java
File metadata and controls
45 lines (36 loc) · 921 Bytes
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
import java.util.ArrayList;
public class CR3 {
public static void main(String[] args) {
Accumulator acc4 = new Accumulator();
acc4.addValue(4);
acc4.addValue(-3);
if (acc4.getSum()){
System.out.println("test passed");
}
else {
System.out.println("test failed");
}
}
}
class Accumulator {
ArrayList<Integer> NumList = new ArrayList<Integer>();
public Accumulator(){
// in the future intialize within constructors and declare on fields instead
}
public void addValue(int value) {
if (value < 0){
return;
}
NumList.add(value);
}
public boolean getSum() {
int sum = 0;
for (int i = 0; i < NumList.size(); i++) {
sum += NumList.get(i);
}
if (sum == 5){
return true;
}
return false;
}
}