-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathBins.java
More file actions
87 lines (72 loc) · 2.4 KB
/
Bins.java
File metadata and controls
87 lines (72 loc) · 2.4 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
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
public class Bins {
private List<Integer> results = new ArrayList<>();
private Integer min;
private Integer max;
public Bins(Integer min, Integer max){
this.min = min;
this.max = max;
List<Integer> resultsList = this.getResults();
for(int i = min; i <= max; i++){
resultsList.add(0);
}
}
public List<Integer> getResults() {
return results;
}
public Integer getBin(Integer binNumber){
return this.getResults().get(binNumber);
}
public void incrementBin(Integer binNumber){
if(binNumber >= this.min && binNumber <= this.max){
Integer binIndex = binNumber - this.min;
Integer currentBinResult = this.getBin(binIndex);
this.results.set(binIndex, currentBinResult + 1);
}
}
public List<Double> tallyResults(Integer numberOfTosses){
List<Double> percentages = new ArrayList<Double>();
for(int i = 0; i < results.size(); i++){
double percentage = (double)results.get(i) / numberOfTosses;
BigDecimal bigDecimal = new BigDecimal(Double.toString(percentage));
bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP);
percentages.add(bigDecimal.doubleValue());
}
return percentages;
}
public void printResults(Integer numberOfTosses){
List<Double> talliedResultList = tallyResults(numberOfTosses);
Integer count = this.min;
Integer i = 0;
String printout = "";
int numberOfStars = 0;
for(Double element : talliedResultList) {
numberOfStars = (int)(element * 100);
i = 1;
printout += String.format("%3d", count) + ": " + String.format("%7d", this.getBin(count - 2)) +
" " + String.format("%1.2f", element) + " ";
while(i < numberOfStars){
printout += "*";
i++;
}
count++;
printout += "\n";
}
System.out.println(printout);
}
public Integer getMin() {
return min;
}
public void setMin(Integer min) {
this.min = min;
}
public Integer getMax() {
return max;
}
public void setMax(Integer max) {
this.max = max;
}
}