Seth 6 anni fa
parent
commit
038e98e35c

BIN
.DS_Store Vedi File


+ 8
- 0
pom.xml Vedi File

@@ -7,6 +7,14 @@
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
+            <scope>compile</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
     <dependency>
11 19
         <groupId>junit</groupId>
12 20
         <artifactId>junit</artifactId>

BIN
src/.DS_Store Vedi File


BIN
src/main/.DS_Store Vedi File


+ 38
- 0
src/main/java/BinTest.java Vedi File

@@ -0,0 +1,38 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class BinTest {
7
+
8
+
9
+    @Test
10
+    public void getBinTest() {
11
+        //Given
12
+        Bins bin = new Bins(2);
13
+        bin.incrememtBin(4);
14
+
15
+        //When
16
+        int expected = 1;
17
+        int actual = bin.getBin(4);
18
+
19
+        //Then
20
+        Assert.assertEquals(actual, expected);
21
+    }
22
+
23
+    @Test
24
+    public void loopTest() {
25
+        //given
26
+        Bins bin = new Bins(2);
27
+        for(int i=0; i<101; i++) {
28
+            bin.incrememtBin(10);
29
+        }
30
+        //when
31
+        int expected = 101;
32
+        int actual = bin.getBin(10);
33
+        //then
34
+        Assert.assertEquals(expected, actual);
35
+
36
+    }
37
+
38
+}

+ 10
- 8
src/main/java/Bins.java Vedi File

@@ -1,20 +1,21 @@
1 1
 
2 2
 public class Bins {
3 3
 
4
-    private int diceBin;
4
+    private int binNum;
5 5
     private int[] values;
6 6
 
7 7
 
8 8
     public Bins(int diceBin) {
9 9
 
10
-        this.diceBin = 0;
11
-
10
+        this.binNum = diceBin;
11
+        runArray(diceBin);
12 12
     }
13 13
 
14
-    public void runArray(){
14
+    public void runArray(int dicebin){
15
+
16
+        values = new int[(binNum * 6) - binNum + 1];
17
+        for (int i = 0; i < values.length; i++) {
15 18
 
16
-        values = new int[((diceBin * 6) - diceBin) +1 ];
17
-        for (int i : values) {
18 19
             values[i] = 0;
19 20
         }
20 21
 
@@ -23,13 +24,14 @@ public class Bins {
23 24
 
24 25
     public void incrememtBin(int binNumber) {
25 26
 
26
-        values[binNumber - diceBin] += 1;
27
+        values[binNumber - binNum] += 1;
28
+        //values[binNumber-diceBin] = ((values[binNumber-diceBin]) + 1);
27 29
 
28 30
 
29 31
     }
30 32
 
31 33
     public int getBin(int binNumber) {
32
-        return values[binNumber-diceBin];
34
+        return values[binNumber-binNum];
33 35
 
34 36
     }
35 37
 

+ 6
- 6
src/main/java/Dice.java Vedi File

@@ -2,19 +2,19 @@ import java.util.Random;
2 2
 
3 3
 public class Dice {
4 4
 
5
-    private int dice;
5
+    private int rolls;
6 6
 
7 7
     public Dice(int dice) {
8 8
 
9
-        this.dice = dice;
9
+        this.rolls = dice;
10 10
     }
11 11
 
12 12
     public int tossAndSum(){
13 13
         int sum = 0;
14
-        //Random random = new Random();
15
-        for (int i = 0; i <= dice ; i++) {
16
-            sum += (int) (Math.ceil(Math.random() * 6));
17
-
14
+        Random random = new Random();
15
+        for (int i = 0; i < rolls ; i++) {
16
+            int result = random.nextInt(6) + 1;
17
+            sum += result;
18 18
         }
19 19
         return sum;
20 20
     }

+ 28
- 0
src/main/java/DiceTest.java Vedi File

@@ -0,0 +1,28 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+
5
+
6
+public class DiceTest {
7
+
8
+
9
+    @Test
10
+    public void diceTest() {
11
+    //Given
12
+        Dice dice = new Dice(1);
13
+    //When
14
+        int result = dice.tossAndSum();
15
+        boolean expected = true;
16
+        boolean actual = false;
17
+        if (result <= 6 && result >= 1){
18
+            actual = true;
19
+        }
20
+
21
+
22
+        //Then
23
+        Assert.assertTrue(actual);
24
+
25
+    }
26
+
27
+
28
+}

+ 2
- 0
src/main/java/Main.java Vedi File

@@ -1,3 +1,5 @@
1
+
2
+
1 3
 public class Main {
2 4
 
3 5
 

+ 4
- 6
src/main/java/Simulation.java Vedi File

@@ -10,12 +10,10 @@ public class Simulation {
10 10
     private int diceNum;
11 11
 
12 12
     public Simulation(int diceNum, int tosses) {
13
-
14
-          this.dice = new Dice(diceNum);
15
-          this.diceBin = new Bins(diceNum);
16
-          this.tosses = tosses;
17
-          this.diceNum = diceNum;
18
-
13
+        this.tosses = tosses;
14
+        this.diceNum = diceNum;
15
+        this.dice = new Dice(diceNum);
16
+        this.diceBin = new Bins(diceNum);
19 17
 
20 18
     }
21 19