-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathBins.java
More file actions
45 lines (37 loc) · 1.07 KB
/
Bins.java
File metadata and controls
45 lines (37 loc) · 1.07 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
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
public class Bins {
private Integer[] boxes;
private final Integer min;
private final Integer max;
public Bins(Integer min, Integer max){
this.min = min;
this.max = max;
this.boxes = new Integer[(max - min) + 1];
Arrays.fill(boxes, 0);
}
public Integer[] getBoxes() {
return boxes;
}
public void fillBins(Integer number){
int index = number - min;
boxes[index]++;
}
public Integer getBin(int binIndex) {
return boxes[binIndex - min];
}
public void incrementBin(int binIndex) {
boxes[binIndex - min]++;
}
public double getPercent(Integer boxOfResult){
Integer sum = 0;
for(Integer number: boxes){
sum += number;
}
double raw = (double) this.getBin(boxOfResult)/sum;
BigDecimal rounded = new BigDecimal(raw).setScale(2, RoundingMode.HALF_EVEN);
double result = rounded.doubleValue();
return result;
}
}