Explorar el Código

Garrett Completed Lab

Garrett Arant hace 6 años
padre
commit
84a7355e1a

+ 7
- 0
pom.xml Ver fichero

@@ -7,6 +7,13 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

+ 40
- 0
src/main/java/Bins.java Ver fichero

@@ -1,4 +1,44 @@
1 1
 
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
4
+import java.util.List;
5
+import java.util.TreeMap;
6
+
2 7
 public class Bins {
3 8
 
9
+    public TreeMap<Integer, Integer> countTotals = new TreeMap<Integer, Integer>();
10
+    int numOfDice;
11
+    int numOfKeys;
12
+
13
+    public Bins(Dice dice){
14
+        this.numOfDice=dice.getNumberOfDice();
15
+        this.numOfKeys=dice.getNumberOfDice()*6;
16
+
17
+    }
18
+
19
+    public void createStorageBin(){
20
+        //Integer numberOfKeysNeeded = (dice.getNumberOfDice() * 6);
21
+
22
+        for(int i = 2; i <= getNumOfKeys(); i++) {
23
+            countTotals.put(i, 0);
24
+        }
25
+    }
26
+
27
+    public void storeCountInBin(Dice dice, Integer numberOfRolls){
28
+        createStorageBin();
29
+        for(int i = 0; i < numberOfRolls; i++){
30
+            int key = dice.diceRoll();
31
+            int value = countTotals.get(key);
32
+            value += 1;
33
+            countTotals.put(key, value);
34
+        }
35
+    }
36
+
37
+    public int getNumOfDice() {
38
+        return numOfDice;
39
+    }
40
+
41
+    public int getNumOfKeys() {
42
+        return numOfKeys;
43
+    }
4 44
 }

+ 54
- 0
src/main/java/BinsTest.java Ver fichero

@@ -0,0 +1,54 @@
1
+import org.junit.After;
2
+import org.junit.Assert;
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+import java.util.TreeMap;
9
+
10
+import static org.junit.Assert.*;
11
+
12
+public class BinsTest {
13
+
14
+    @Before
15
+    public void setUp() throws Exception {
16
+    }
17
+
18
+    @After
19
+    public void tearDown() throws Exception {
20
+    }
21
+
22
+    @Test
23
+    public void createStorageBinTest(){
24
+        Dice dice = new Dice(3);
25
+        Bins bins = new Bins(dice);
26
+        // : Given
27
+
28
+        // : When
29
+        bins.createStorageBin();
30
+        Integer actual= bins.countTotals.lastKey();
31
+        Integer expected = 18;
32
+
33
+        // : Then
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test
38
+    public void storeCountInBinTest(){
39
+        Dice dice = new Dice(2);
40
+        Bins bins = new Bins(dice);
41
+        // : Given
42
+        // : When
43
+        bins.storeCountInBin(dice, 100);
44
+        int actualSumOfValues=0;
45
+        for(int i=2;i<=12;i++){
46
+            actualSumOfValues+=bins.countTotals.get(i);
47
+        }
48
+        int expected=100;
49
+
50
+        // : Then
51
+        Assert.assertEquals(expected, actualSumOfValues);
52
+    }
53
+
54
+}

+ 21
- 1
src/main/java/Dice.java Ver fichero

@@ -1,4 +1,24 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
2 4
 
5
+    public int numberOfDice;
6
+
7
+    public Dice(int numOfDice) {
8
+        this.numberOfDice = numOfDice;
9
+    }
10
+
11
+    public Integer diceRoll() {
12
+
13
+        int totalSumOfDice = 0;
14
+        for (int i = 0; i < numberOfDice; i++) {
15
+            totalSumOfDice += (int) Math.floor(Math.random() * 6) + 1;
16
+
17
+        }
18
+        return totalSumOfDice;
19
+    }
3 20
 
4
-}
21
+    public int getNumberOfDice() {
22
+        return numberOfDice;
23
+    }
24
+}

+ 31
- 0
src/main/java/DiceTest.java Ver fichero

@@ -0,0 +1,31 @@
1
+import org.junit.After;
2
+import org.junit.Assert;
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+public class DiceTest {
9
+
10
+    @Before
11
+    public void setUp() throws Exception {
12
+    }
13
+
14
+    @After
15
+    public void tearDown() throws Exception {
16
+    }
17
+
18
+    @Test
19
+    public void diceRoll() {
20
+        Dice dice = new Dice(2);
21
+        // : Given
22
+
23
+
24
+        // : When
25
+        Boolean actual = (2 <= dice.diceRoll() && dice.diceRoll() <= 12);
26
+        Boolean expected = (2 <= dice.diceRoll() && dice.diceRoll() <= 12);
27
+
28
+        // : Then
29
+        Assert.assertEquals(expected, actual);
30
+    }
31
+    }

+ 14
- 0
src/main/java/GarrettResults.md Ver fichero

@@ -0,0 +1,14 @@
1
+***
2
+Simulation of 2 dice tossed for 1000000 times.
3
+***
4
+ 2 :     27934: 0.03 **
5
+ 3 :     55832: 0.06 *****
6
+ 4 :     83885: 0.08 ********
7
+ 5 :    111285: 0.11 ***********
8
+ 6 :    138983: 0.14 *************
9
+ 7 :    166606: 0.17 ****************
10
+ 8 :    138859: 0.14 *************
11
+ 9 :    111058: 0.11 ***********
12
+10 :     82863: 0.08 ********
13
+11 :     54992: 0.05 *****
14
+12 :     27703: 0.03 **

+ 24
- 0
src/main/java/Simulation.java Ver fichero

@@ -1,5 +1,29 @@
1
+import java.util.TreeMap;
2
+
1 3
 public class Simulation {
2 4
 
5
+    public String simulation(Bins bins, Integer numberOfRolls){
6
+
7
+        String printSimulationResults="***\nSimulation of "+bins.getNumOfDice()+" dice tossed for "+numberOfRolls+" times.\n***\n";
8
+
9
+        for (int i = 2; i<= bins.getNumOfKeys(); i++){
10
+
11
+            String keyValue=String.format("%2d", i);
12
+            String numberOfEachValue= String.format("%10d", bins.countTotals.get(i));
13
+            String percentOfTotal=String.format("%.2f", (bins.countTotals.get(i).floatValue()/numberOfRolls.floatValue()));
14
+            int numberofStars= (int)Math.floor((bins.countTotals.get(i).floatValue()/numberOfRolls.floatValue() *100));
15
+            String starString= printStars(numberofStars);
16
+            printSimulationResults+=keyValue+" :"+numberOfEachValue+": "+percentOfTotal+" "+starString +"\n";
17
+        }
18
+        return printSimulationResults.trim();
19
+    }
3 20
 
21
+    public String printStars(int numberOfStars){
22
+        String starString="";
23
+        for(int i=0;i<numberOfStars;i++){
24
+            starString+="*";
25
+        }
26
+        return starString;
27
+    }
4 28
 
5 29
 }

+ 39
- 0
src/main/java/SimulationTest.java Ver fichero

@@ -0,0 +1,39 @@
1
+import org.junit.After;
2
+import org.junit.Assert;
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+public class SimulationTest {
9
+
10
+    @Before
11
+    public void setUp() throws Exception {
12
+    }
13
+
14
+    @After
15
+    public void tearDown() throws Exception {
16
+    }
17
+
18
+    @Test
19
+    public void simulationTest(){
20
+
21
+        Simulation simulation = new Simulation();
22
+        Dice dice = new Dice(2);
23
+        Bins bins = new Bins(dice);
24
+
25
+        // : Given
26
+        bins.storeCountInBin(dice, 1000000);
27
+        // : When
28
+
29
+        String returnedString= simulation.simulation(bins, 1000000);
30
+        String actual= returnedString.substring(returnedString.length()-2, returnedString.length());
31
+        String expected="**";
32
+        System.out.println(simulation.simulation(bins, 1000000));
33
+
34
+        //: Then
35
+        Assert.assertEquals(expected, actual);
36
+    }
37
+
38
+
39
+}