-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
61 lines (46 loc) · 1.56 KB
/
Simulation.java
File metadata and controls
61 lines (46 loc) · 1.56 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
public class Simulation {
private Integer numberOfDie;
private Integer numberOfTosses;
private Bins bins;
public Simulation(Integer numberOfDie, Integer numberOfTosses) {
this.numberOfDie = numberOfDie;
this.numberOfTosses = numberOfTosses;
}
public Integer getNumOfDie() {
return this.numberOfDie;
}
public Integer getNumOfTosses() {
return this.numberOfTosses;
}
public Bins runSimulation() {
Dice dice = new Dice(numberOfDie);
bins = new Bins(numberOfDie, numberOfDie*6);
Integer result;
for (int i = 0; i < numberOfTosses; i++) {
result = dice.tossAndSum();
bins.incrementBin(result);
}
return bins;
}
public void printResults() {
System.out.println(new StringBuilder()
.append("\n***")
.append("\nSimulation of " + numberOfDie + " dice tossed " + numberOfTosses + " times.")
.append("\n***")
+ printBins(bins));
}
private String printBins(Bins bins) {
String binLine = "";
Double binPercent;
Integer stars = 0;
for(int i = numberOfDie; i <= (numberOfDie*6); i++) {
binPercent = ((double) bins.getBin(i)) / numberOfTosses;
binLine += String.format("\n%2d : %8d : %.2f ", i, bins.getBin(i), binPercent);
stars = (100 * bins.getBin(i)) / numberOfTosses;
for (int j = 0; j < stars; j++) {
binLine += "*";
}
}
return binLine;
}
}