forked from Team254/FRC-2019-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovingAverage.java
45 lines (34 loc) · 872 Bytes
/
MovingAverage.java
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
package com.team254.lib.util;
import java.util.ArrayList;
/**
* Helper class for storing and calculating a moving average
*/
public class MovingAverage {
ArrayList<Double> numbers = new ArrayList<Double>();
private int maxSize;
public MovingAverage(int maxSize) {
this.maxSize = maxSize;
}
public void add(double newNumber) {
numbers.add(newNumber);
if (numbers.size() > maxSize) {
numbers.remove(0);
}
}
public double getAverage() {
double total = 0;
for (double number : numbers) {
total += number;
}
return total / numbers.size();
}
public int getSize() {
return numbers.size();
}
public boolean isUnderMaxSize() {
return getSize() < maxSize;
}
public void clear() {
numbers.clear();
}
}