-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
55 lines (45 loc) · 1.6 KB
/
Simulation.java
File metadata and controls
55 lines (45 loc) · 1.6 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
import java.util.Map;
public class Simulation {
private Integer numberOfDies;
private Integer numberOfTosses;
private Bins bins;
public Simulation(Integer numberOfDies, Integer numberOfTosses) {
this.numberOfDies = numberOfDies;
this.numberOfTosses = numberOfTosses;
}
public Bins runSimulation() {
Dice dice = new Dice(numberOfDies);
bins = new Bins(numberOfDies, numberOfDies * 6);
Integer total;
for (int i = 0; i < numberOfTosses; i++) {
total = dice.tossAndSum();
bins.incrementBin(total);
}
return bins;
}
public void printResults() {
System.out.println("***");
System.out.println("Simulation of " + numberOfDies + " dice tossed for " + numberOfTosses + " times.");
System.out.println("***\n");
for (Map.Entry value : bins.getBinMap().entrySet()) {
Integer val = (Integer) value.getValue();
Double percent = Double.valueOf(val) / numberOfTosses;
String stringFormat = String.format("%2d : %8d : %.2f", value.getKey(), value.getValue(), percent);
System.out.println(stringFormat + " " + printStars(val));
}
}
public String printStars(Integer number) {
Integer dividedByTenThousand = number / 10000;
String stars = "";
for (int i = 0; i < dividedByTenThousand; i++) {
stars += "*";
}
return stars;
}
public Integer getNumberOfDies() {
return numberOfDies;
}
public Integer getNumberOfTosses() {
return numberOfTosses;
}
}