Browse Source

Created test class and corresponding method for Dice Class

Trinh Tong 5 years ago
parent
commit
ba7e171133
5 changed files with 66 additions and 1 deletions
  1. 8
    0
      pom.xml
  2. 25
    0
      src/main/java/Dice.java
  3. 1
    1
      src/main/java/Simulation.java
  4. 2
    0
      src/test/java/BinsTest.java
  5. 30
    0
      src/test/java/DiceTest.java

+ 8
- 0
pom.xml View 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>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 25
- 0
src/main/java/Dice.java View File

@@ -1,4 +1,29 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
2 4
 
5
+    // The dice class rolls a number of dice
6
+
7
+    private int numberOfDice = 1;
8
+
9
+    public Dice(int numOfDice) {
10
+
11
+        this.numberOfDice = numOfDice;
12
+
13
+    }
14
+
15
+    public int tossAndSum() {
16
+
17
+        int result = 0;
18
+
19
+        for (int i = 0; i < this.numberOfDice; i++) {
20
+
21
+            result += (int) (Math.random() * 6) + 1;
22
+        }
23
+
24
+        return result;
25
+
26
+    }
3 27
 
4 28
 }
29
+

+ 1
- 1
src/main/java/Simulation.java View File

@@ -2,4 +2,4 @@ public class Simulation {
2 2
 
3 3
 
4 4
 
5
-}
5
+}

+ 2
- 0
src/test/java/BinsTest.java View File

@@ -0,0 +1,2 @@
1
+public class BinsTest {
2
+}

+ 30
- 0
src/test/java/DiceTest.java View File

@@ -0,0 +1,30 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class DiceTest {
5
+
6
+    public DiceTest() {
7
+
8
+    }
9
+
10
+    @Test
11
+    public void testTossAndSumOneDie() {
12
+
13
+        int[] oneDieResults = new int[] {1, 2, 3, 4, 5, 6};
14
+        Dice testDie = new Dice(1);
15
+
16
+        boolean expected = true;
17
+
18
+        boolean findToken = false;
19
+
20
+        int actualRoll = testDie.tossAndSum();
21
+
22
+            for (int side : oneDieResults) {
23
+                if (side == actualRoll) {
24
+                    findToken = true;
25
+                    break;
26
+                 }
27
+            }
28
+        Assert.assertTrue(findToken);
29
+    }
30
+}