Daniel Horowitz 6 years ago
parent
commit
d0a389d123

+ 1
- 1
src/main/java/Bins.java View File

@@ -8,7 +8,7 @@ public class Bins {
8 8
     private int[] results;
9 9
 
10 10
     public Bins (int minIndex, int maxIndex) {
11
-        results = new int[maxIndex];
11
+        results = new int[maxIndex +1];
12 12
     }
13 13
 
14 14
     public int getBins(int outcome) {

+ 15
- 0
src/main/java/DanielResults.md View File

@@ -0,0 +1,15 @@
1
+***
2
+Simulation of 2 dice tossed for 1000000 times.
3
+***
4
+
5
+  2 :     27854: 0.03**
6
+  3 :     55661: 0.06*****
7
+  4 :     83465: 0.08********
8
+  5 :    110837: 0.11***********
9
+  6 :    138840: 0.14*************
10
+  7 :    166577: 0.17****************
11
+  8 :    139243: 0.14*************
12
+  9 :    111241: 0.11***********
13
+ 10 :     82793: 0.08********
14
+ 11 :     55743: 0.06*****
15
+ 12 :     27746: 0.03**

+ 0
- 19
src/main/java/DiceTest.java View File

@@ -1,19 +0,0 @@
1
-import org.junit.Assert;
2
-import org.junit.Test;
3
-
4
-import static org.junit.Assert.*;
5
-
6
-public class DiceTest {
7
-
8
-    @Test
9
-    public void rollDice() {
10
-        Dice dice = new Dice();
11
-
12
-        Integer actual = dice.rollDice(2);
13
-
14
-        Assert.assertTrue(actual >= 2 && actual <= 12);
15
-
16
-
17
-
18
-    }
19
-}

+ 34
- 2
src/main/java/Simulation.java View File

@@ -23,14 +23,46 @@ public class Simulation extends Dice {
23 23
         this.numberOfRolls = numberOfRolls;
24 24
           }
25 25
 
26
-    public void runSimulation (int numberOfRolls) {
26
+    public void runSimulation (int numberOfRolls, int rollsOfDie) {
27 27
 
28 28
         for (int i = 0; i < numberOfRolls; i++)
29
-            bins.incrementBin(dice.rollDice(2));
29
+            bins.incrementBin(dice.rollDice(rollsOfDie));
30 30
 
31 31
         }
32 32
 
33
+     public String printIt(int minIndex, int maxIndex, int numberOfRolls) {
34
+         StringBuilder sb = new StringBuilder();
35
+             sb.append("***\nSimulation of " + minIndex +" dice tossed for " + numberOfRolls + " times.\n***\n\n");
36
+        for (int y = minIndex; y <= maxIndex; y++){
37
+            int diceSum = y;
38
+            int numOfOccurrence = bins.getBins(diceSum);
39
+            sb.append(String.format(" %2d :   %7d: %.2f", diceSum, numOfOccurrence, (numOfOccurrence/(float)numberOfRolls)));
40
+            sb.append(numberOfStars((int) ((numOfOccurrence/(float)numberOfRolls)*100))+ "\n");
41
+         }
42
+
43
+         System.out.println(sb.toString());
44
+            return sb.toString();
45
+
46
+     }
47
+
48
+     public String numberOfStars(int percentOutcome){
49
+        StringBuilder sb1 = new StringBuilder();
50
+        for (int otc = 0; otc < percentOutcome; otc++) {
51
+            sb1.append("*");
52
+        }
53
+        return sb1.toString();
54
+     }
55
+
56
+
57
+
33 58
     public static void main(String[] args) {
59
+        Simulation simulation = new Simulation(2,2);
60
+        simulation.runSimulation(1000000,2);
61
+        simulation.printIt(2,12,1000000);
62
+
63
+
64
+
65
+
34 66
 
35 67
 
36 68
     }

+ 0
- 13
src/main/java/SimulationTest.java View File

@@ -1,13 +0,0 @@
1
-import org.junit.Test;
2
-
3
-import static org.junit.Assert.*;
4
-
5
-public class SimulationTest {
6
-
7
-    @Test
8
-    public void runSimulation() {
9
-
10
-
11
-
12
-    }
13
-}

+ 26
- 0
src/test/BinsTest.java View File

@@ -0,0 +1,26 @@
1
+import org.junit.Before;
2
+import org.junit.Test;
3
+import org.junit.Assert;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class BinsTest {
8
+
9
+    @Before
10
+    public void setUp() throws Exception {
11
+        Bins bins = new Bins(2, 12);
12
+        Integer expected1 = 0;
13
+        Integer actual1 = bins.getBins(3);
14
+        Assert.assertEquals(expected1, actual1);
15
+
16
+    }
17
+
18
+    @Test
19
+    public void getBins() {
20
+
21
+    }
22
+
23
+    @Test
24
+    public void incrementBin() {
25
+    }
26
+}

+ 27
- 0
src/test/SimulationTest.java View File

@@ -0,0 +1,27 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class SimulationTest {
5
+
6
+    @Test
7
+    public void runSimulation() {
8
+
9
+
10
+
11
+    }
12
+
13
+    public static class DiceTest {
14
+
15
+        @Test
16
+        public void rollDice() {
17
+            Dice dice = new Dice();
18
+
19
+            Integer actual = dice.rollDice(2);
20
+
21
+            Assert.assertTrue(actual >= 2 && actual <= 12);
22
+
23
+
24
+
25
+        }
26
+    }
27
+}