瀏覽代碼

Merge 8f7b5a02afd31972c39b7ddcb5754284f13c19c2 into 1eaff706f0dcc7a467d1c6648bcb494e313ba6a5

April Rivera 6 年之前
父節點
當前提交
ca928a8d59
沒有帳戶連結到提交者的電子郵件
共有 5 個文件被更改,包括 108 次插入0 次删除
  1. 19
    0
      pom.xml
  2. 28
    0
      src/main/java/Bins.java
  3. 15
    0
      src/main/java/Dice.java
  4. 29
    0
      src/main/java/Simulation.java
  5. 17
    0
      src/test/java/DiceTest.java

+ 19
- 0
pom.xml 查看文件

@@ -7,6 +7,25 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+        </dependency>
28
+    </dependencies>
10 29
 
11 30
 
12 31
 </project>

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

@@ -1,4 +1,32 @@
1 1
 
2
+import java.util.Map;
3
+import java.util.TreeMap;
4
+
5
+
6
+
2 7
 public class Bins {
3 8
 
9
+   static  Map<Integer, Integer> diceRollTotalsAndFrequency = new TreeMap<>();
10
+
11
+   public static void persistDiceRoll(Integer diceRollValue){
12
+
13
+       Integer currentValue = diceRollTotalsAndFrequency.get(diceRollValue);
14
+       if(currentValue == null){
15
+           currentValue = 1;
16
+       }
17
+       else {
18
+           currentValue++;
19
+       }
20
+       diceRollTotalsAndFrequency.put(diceRollValue, currentValue);
21
+
22
+
23
+
24
+       //diceRollTotalsAndFrequency.put(diceRollValue,
25
+       // diceRollTotalsAndFrequency.get(diceRollValue) == null ?
26
+       // 1 : diceRollTotalsAndFrequency.get(diceRollValue) + 1);
27
+
28
+   }
29
+
30
+
31
+
4 32
 }

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

@@ -1,4 +1,19 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
2 4
 
5
+    public Integer rollDiceAndGiveValue(){
6
+        Random random = new Random();
7
+        int tossAndSum = random.nextInt(6) + random.nextInt(6) + 2;
8
+        return tossAndSum;
9
+    }
10
+
11
+    public void persistDiceRoll(int tossAndSum){
12
+        Bins.persistDiceRoll(tossAndSum);
13
+    }
14
+
15
+    public void rollDiceAndPersistValues(){
16
+        persistDiceRoll(rollDiceAndGiveValue());
17
+    }
3 18
 
4 19
 }

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

@@ -1,5 +1,34 @@
1
+import java.util.Map;
2
+
1 3
 public class Simulation {
2 4
 
3 5
 
6
+    public static void main(String[] args) {
7
+
8
+        Dice dice = new Dice();
9
+
10
+        for (int i = 0; i < 1000000; i++) {
11
+            dice.rollDiceAndPersistValues();
12
+        }
13
+
14
+//        String results = Bins.diceRollTotalsAndFrequency.toString().replaceAll("[\\[\\](){}]", "");
15
+        for(Map.Entry entry : Bins.diceRollTotalsAndFrequency.entrySet()){
16
+            System.out.println(entry +"\n");
17
+        }
4 18
 
19
+    }
5 20
 }
21
+
22
+//         2 :    27917: 0.03 **
23
+//         3 :    55422: 0.06 *****
24
+//         4 :    83457: 0.08 ********
25
+//         5 :   110961: 0.11 ***********
26
+//         6 :   139098: 0.14 *************
27
+//         7 :   166977: 0.17 ****************
28
+//         8 :   138386: 0.14 *************
29
+//         9 :   111102: 0.11 ***********
30
+//         10 :    83367: 0.08 ********
31
+//         11 :    55799: 0.06 *****
32
+//         12 :    27514: 0.03 **
33
+
34
+

+ 17
- 0
src/test/java/DiceTest.java 查看文件

@@ -0,0 +1,17 @@
1
+import org.junit.Before;
2
+import org.junit.Test;
3
+
4
+import java.util.Random;
5
+
6
+public class DiceTest{
7
+
8
+    @Before
9
+    public void setUp(){
10
+        Random random = new Random();
11
+    }
12
+    @Test
13
+    public void rollDiceAndGiveValueTest(){
14
+
15
+    }
16
+
17
+}