-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSimulation.java
More file actions
70 lines (61 loc) · 2.13 KB
/
Simulation.java
File metadata and controls
70 lines (61 loc) · 2.13 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
import java.math.BigDecimal;
public class Simulation {
private Dice dice;
private Bins bin;
private Integer numOfToss;
private Integer numOfDice;
private Integer min;
public Simulation(Integer numOfDice, Integer numOfToss){
this.numOfToss = numOfToss;
this.numOfDice = numOfDice;
this.min = numOfDice;
this.dice = new Dice(numOfDice);
this.bin = new Bins(numOfDice, numOfDice * 6);
}
public void runSim(){
for (int i = 0; i < numOfToss; i++) {
bin.fillBins(dice.tossAndSum());
}
}
public void printResult(){
//Heading
String result = "***\n" +
"Simulation of " + numOfDice + " dice tossed for " + numOfToss+ " times.\n" +
"***\n";
//Iterating through each box and constructing string of boxNumber, values, percentage, stars.
Integer[] thisBin = bin.getBoxes();
for(int i = 0; i < thisBin.length; i++) {
int boxId = i + min;
String addToResult = getBoxNumber(boxId) + " :" + getBoxValue(bin.getBin(boxId)) + ": " + bin.getPercent(boxId) +
" " + getStars(bin.getPercent(boxId)) + "\n";
result += addToResult;
}
System.out.println(result);
}
private String getBoxNumber(Integer boxID){
String result = ""; //3 spaces
Integer whiteSpaces = 3 - boxID.toString().length();
for (int j = 0; j < whiteSpaces; j++) {
result += " ";
}
result += boxID.toString();
return result;
}
private String getBoxValue(Integer value){
String result = ""; //9 spaces
Integer spaces = 9 - value.toString().length();
for (int i = 0; i < spaces; i++) {
result += " ";
}
result += value.toString();
return result;
}
public String getStars(double number){
StringBuilder result = new StringBuilder();
double numOfStars = number * 100;
for (int i = 0; i < numOfStars; i++) {
result.append("*");
}
return result.toString();
}
}