瀏覽代碼

created dice, bins and simulation methods

Michelle DiMarino 6 年之前
父節點
當前提交
af27e2d881
共有 10 個文件被更改,包括 143 次插入1 次删除
  1. 二進制
      .DS_Store
  2. 17
    1
      pom.xml
  3. 二進制
      src/.DS_Store
  4. 二進制
      src/main/.DS_Store
  5. 41
    0
      src/main/java/Bins.java
  6. 22
    0
      src/main/java/BinsTest.java
  7. 16
    0
      src/main/java/Dice.java
  8. 14
    0
      src/main/java/DiceTest.java
  9. 22
    0
      src/main/java/Simulation.java
  10. 11
    0
      src/main/java/SimulationTest.java

二進制
.DS_Store 查看文件


+ 17
- 1
pom.xml 查看文件

@@ -8,5 +8,21 @@
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+        <dependency>
19
+            <groupId>junit</groupId>
20
+            <artifactId>junit</artifactId>
21
+            <version>RELEASE</version>
22
+            <scope>compile</scope>
23
+        </dependency>
24
+    </dependencies>
25
+
26
+
27
+</project>
11 28
 
12
-</project>

二進制
src/.DS_Store 查看文件


二進制
src/main/.DS_Store 查看文件


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

@@ -1,4 +1,45 @@
1
+import java.util.ArrayList;
1 2
 
2 3
 public class Bins {
4
+    int numberOfDice1;
5
+    int totalDiceFaces;
6
+    ArrayList <Integer> allBins;
7
+
8
+    public Bins(int numberOfDice, int totalDiceFaces) {
9
+        this.numberOfDice1 = numberOfDice;
10
+        this.totalDiceFaces = totalDiceFaces;
11
+        this.allBins = new ArrayList<Integer>();
12
+
13
+        for (int i = 0; i < totalDiceFaces+1; i++){
14
+            this.allBins.add(i,0);
15
+        }
16
+    }
17
+
18
+
19
+
20
+    public int getBin(int binNumber){
21
+        int i;
22
+        int numberOfOutcomes = 0;
23
+        for (i = 0; i < this.allBins.size(); i++){
24
+            if (binNumber == i){
25
+                numberOfOutcomes = this.allBins.get(i);
26
+                break;
27
+            }
28
+        }
29
+        return numberOfOutcomes;
30
+    }
31
+
32
+    public void incrementBin(int binNumber){
33
+        for (int i = 0; i <= this.allBins.size(); i++){
34
+            if (binNumber == i){
35
+                int counter = this.allBins.get(i);
36
+                this.allBins.add(i,counter += 1);
37
+                break;
38
+
39
+            }
40
+        }
41
+
42
+    }
43
+
3 44
 
4 45
 }

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

@@ -0,0 +1,22 @@
1
+import org.junit.Test;
2
+import org.junit.Assert;
3
+
4
+public class BinsTest {
5
+
6
+    @Test
7
+   public void incrementGetBinTest() {
8
+        Bins bin1 = new Bins(2, 12);
9
+        Dice dice1 = new Dice(2);
10
+
11
+        int expectedOutput = 3;
12
+
13
+        bin1.incrementBin(1);
14
+        bin1.incrementBin(1);
15
+        bin1.incrementBin(1);
16
+
17
+        int actualOutput = bin1.allBins.get(1);
18
+
19
+        Assert.assertEquals(expectedOutput, actualOutput);
20
+    }
21
+
22
+}

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

@@ -1,4 +1,20 @@
1
+
1 2
 public class Dice {
2 3
 
4
+    int numberOfDice;
5
+
6
+    public Dice (int numberOfDice){
7
+        this.numberOfDice = numberOfDice;
8
+
9
+    }
10
+
11
+    public int tossAndSum(int nOfDice){
12
+        int toss = 0;
13
+        for (int i = 0; i < nOfDice; i++){
14
+            int diceRoll = (int)(Math.random() * 5 + 1);
15
+            toss = toss + diceRoll;
16
+        }
17
+        return toss;
18
+    }
3 19
 
4 20
 }

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

@@ -0,0 +1,14 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class DiceTest {
6
+
7
+    @Test
8
+    public void tossAndSum() {
9
+        Dice dice1 = new Dice(1);
10
+
11
+
12
+
13
+    }
14
+}

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

@@ -1,5 +1,27 @@
1 1
 public class Simulation {
2 2
 
3
+    int numberOfDice2;
4
+    int numberOfRolls;
5
+
6
+    public Simulation(int numberOfDice2, int numberOfRolls){
7
+        this.numberOfDice2 = numberOfDice2;
8
+        this.numberOfRolls = numberOfRolls;
9
+    }
10
+
11
+    Dice dice = new Dice(numberOfDice2);
12
+    Bins bin = new Bins(numberOfDice2, (numberOfDice2*6));
13
+
14
+    public void runSimulation(){
15
+        int i = 0;
16
+        for (i = 0; i <= numberOfRolls; i++){
17
+            int toss = dice.tossAndSum(numberOfDice2);
18
+            bin.incrementBin(toss);
19
+
20
+        }
21
+
22
+    }
23
+
24
+
3 25
 
4 26
 
5 27
 }

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

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