-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCR3.java
More file actions
36 lines (28 loc) · 786 Bytes
/
CR3.java
File metadata and controls
36 lines (28 loc) · 786 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
import java.util.ArrayList;
public class CR3 {
public static void main(String[] args) {
Accumulator acc4 = new Accumulator();
acc4.addValue(5);
acc4.addValue(-3);
System.out.println("after 2 additions, one being negative: " + acc4.getSum());
}
}
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 int getSum() {
int sum = 0;
for (int i = 0; i < NumList.size(); i++) {
sum += NumList.get(i);
}
return sum;
}
}