瀏覽代碼

Completed Lab

Haysel Santiago 6 年之前
父節點
當前提交
aa3b10befe

+ 12
- 0
pom.xml 查看文件

@@ -7,6 +7,18 @@
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>org.junit.jupiter</groupId>
13
+            <artifactId>junit-jupiter-api</artifactId>
14
+            <version>RELEASE</version>
15
+        </dependency>
16
+        <dependency>
17
+            <groupId>junit</groupId>
18
+            <artifactId>junit</artifactId>
19
+            <version>RELEASE</version>
20
+        </dependency>
21
+    </dependencies>
10 22
 
11 23
 
12 24
 </project>

+ 31
- 0
src/main/java/Bins.java 查看文件

@@ -3,6 +3,37 @@ import java.util.TreeMap;
3 3
 public class Bins {
4 4
 
5 5
     public TreeMap<Integer, Integer> countTotals = new TreeMap<Integer, Integer>();
6
+    int numberOfDice;
7
+    int numberOfKeys;
8
+
9
+    public Bins(Dice dice) {
10
+        this.numberOfDice = dice.getNumberOfDice();
11
+        this.numberOfKeys = dice.getNumberOfDice() * 6;
12
+    }
13
+
14
+    public void createBin() {
15
+        for(int i = 2; i <= getNumberOfKeys(); i++) {
16
+            countTotals.put(i, 0);
17
+        }
18
+
19
+    }
20
+
21
+    public void countPlacedIntoBin(Dice dice, Integer numberOfRolls){
22
+        createBin();
23
+        for(int i = 0; i < numberOfRolls; i++) {
24
+            int key = dice.diceRoll();
25
+            int value = countTotals.get(key);
26
+            value +=1;
27
+            countTotals.put(key, value);
28
+        }
29
+    }
30
+
31
+    public int getNumberOfKeys() {
32
+        return numberOfKeys;
33
+    }
34
+    public int getNumberOfDice() {
35
+        return getNumberOfKeys();
36
+    }
6 37
 
7 38
 
8 39
 }

+ 32
- 0
src/main/java/BinsTest.java 查看文件

@@ -0,0 +1,32 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class BinsTest {
6
+
7
+    @Test
8
+    public void createBin() {
9
+        Dice dice = new Dice(2);
10
+        Bins bins = new Bins(dice);
11
+
12
+        bins.createBin();
13
+        Integer actual = bins.countTotals.size();
14
+        Integer expected = 11;
15
+        assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test
19
+    public void countPlacedIntoBin() {
20
+        Dice dice = new Dice(2);
21
+        Bins bins = new Bins(dice);
22
+
23
+        bins.countPlacedIntoBin(dice, 100);
24
+        int actualSum = 0;
25
+        int expectedSum = 100;
26
+
27
+        for(int i = 2; i <=12; i++) {
28
+            actualSum += bins.countTotals.get(i);
29
+        }
30
+        assertEquals(expectedSum, actualSum);
31
+    }
32
+}

+ 18
- 17
src/main/java/Dice.java 查看文件

@@ -1,21 +1,22 @@
1
-import java.util.*;
1
+import java.util.Random;
2 2
 
3
-    public class Dice {
4
-        public static final int howManyRolls = 1000000;
3
+public class Dice {
5 4
 
6
-        public static void main(String[] args) {
7
-            int[] d = new int[13];
8
-            for (int i = 2; i < 13; i++) d[i] = 0;
5
+    public int numberOfDice;
9 6
 
10
-            for (int j = 0; j < howManyRolls; j++) {
11
-                int die1 = (int) (6.0 * Math.random() + 1.0);
12
-                int die2 = (int) (6.0 * Math.random() + 1.0);
13
-                int sumOfDice = die1 + die2;
14
-                d[sumOfDice]++;
15
-            }
16
-            System.out.print("Rolls: " + howManyRolls);
17
-            for (int i = 2; i < 13; i++)
18
-                System.out.print(", " + i + ": " + d[i]);
19
-            System.out.println();
7
+    public Dice(int numOfDice) {
8
+        this.numberOfDice = numOfDice;
9
+    }
10
+
11
+    public Integer diceRoll() {
12
+
13
+        int diceSum = 0;
14
+        for(int i = 0; i < numberOfDice; i++) {
15
+            diceSum += (int) Math.floor(Math.random() * 6) + 1;
20 16
         }
21
-    }
17
+        return diceSum;
18
+    }
19
+    public int getNumberOfDice() {
20
+        return numberOfDice;
21
+    }
22
+}

+ 15
- 0
src/main/java/DiceTest.java 查看文件

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

+ 14
- 0
src/main/java/GioResults.MD 查看文件

@@ -0,0 +1,14 @@
1
+***
2
+Simulation of 12 dice tossed for 1000000 times.
3
+***
4
+ 2 :      27830 : 0.03 **
5
+ 3 :      55614 : 0.06 *****
6
+ 4 :      82941 : 0.08 ********
7
+ 5 :     110677 : 0.11 ***********
8
+ 6 :     139062 : 0.14 *************
9
+ 7 :     166644 : 0.17 ****************
10
+ 8 :     139242 : 0.14 *************
11
+ 9 :     110909 : 0.11 ***********
12
+10 :      83717 : 0.08 ********
13
+11 :      55732 : 0.06 *****
14
+12 :      27632 : 0.03 **

+ 26
- 0
src/main/java/Simulation.java 查看文件

@@ -1,5 +1,31 @@
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.getNumberOfDice() + " dice tossed for " + numberOfRolls +
8
+                " times.\n***\n";
9
+
10
+        for(int i = 2; i <= bins.getNumberOfKeys(); i++){
11
+
12
+            String keyValue=String.format("%2d", i);
13
+            String numberOfValue = String.format("%10d", bins.countTotals.get(i));
14
+            String percentOfTotal = String.format("%.2f", (bins.countTotals.get(i).floatValue() / numberOfRolls.floatValue()));
15
+
16
+            int amountOfStars = (int)Math.floor((bins.countTotals.get(i).floatValue() / numberOfRolls.floatValue() *100));
17
+            String starString = printStars(amountOfStars);
18
+            printSimulationResults+= keyValue+ " : "+ numberOfValue+" : " + percentOfTotal+ " " +starString+ "\n";
19
+        }
20
+        return printSimulationResults;
21
+    }
3 22
 
23
+    private String printStars(int amountOfStars) {
24
+        String starString="";
25
+        for(int i =0; i< amountOfStars; i++){
26
+            starString+= "*";
27
+        }
28
+        return starString;
29
+    }
4 30
 
5 31
 }

+ 15
- 0
src/main/java/SimulationTest.java 查看文件

@@ -0,0 +1,15 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class SimulationTest {
6
+
7
+    @Test
8
+    public void simulation() {
9
+        Simulation simulation = new Simulation();
10
+        Dice dice = new Dice(2);
11
+        Bins bins = new Bins(dice);
12
+        bins.countPlacedIntoBin(dice, 1000000);
13
+        System.out.println(simulation.Simulation(bins, 1000000));
14
+    }
15
+}