| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
-
- public class Simulation
- {
- Integer numberOfDice;
- Integer numberOfTosses;
- public Simulation(Integer numberOfDice,Integer numberOfTosses){
- this.numberOfDice = numberOfDice;
- this.numberOfTosses = numberOfTosses;
- }
- Bins results = new Bins();
- public void runSimulation(){
- int currentRoll = 0;
- Dice dice = new Dice(numberOfDice);
- Bins results = new Bins();
- while(currentRoll < numberOfTosses){
- int result = dice.tossSum();
- currentRoll++;
- results.increment(result);
- }
- printHeader();
- printResults(results);
- }
-
- public float ratioOfTosses(int numberOfSpecificResult){
- return numberOfSpecificResult/(float)numberOfTosses;
- }
-
- public void printHeader(){
- System.out.println("***\n" + "Simulation of " + numberOfDice
- + " dice tossed for " + numberOfTosses + " times.\n***");
- }
-
- public void printResults(Bins results){
- for(int i = 2; i <=12;i++){
- float ratio = ratioOfTosses(results.getN(i));
- StringBuilder stars = new StringBuilder();
- for(int j = 1; j < (ratio*100); j++){
- stars.append("*");
- }
- System.out.println(String.format("%2s :%9s:%5.2f ",i,results.getN(i),ratio)+stars.toString());
- }
- }
-
- }
|