Преглед изворни кода

dice, bin, and simulation classes done

mpierse пре 6 година
родитељ
комит
1bae8cb9b4

+ 16
- 4
src/main/java/BinsTest.java Прегледај датотеку

@@ -7,14 +7,26 @@ public class BinsTest {
7 7
     @Test
8 8
     public void testGetBin() {
9 9
         //given
10
-        Bins bin = new Bins(2);
11
-        bin.incrementBin(3);
10
+        Bins bin1 = new Bins(2);
11
+        bin1.incrementBin(3);
12 12
         //when
13 13
         int expected = 1;
14
-        int actual = bin.getBin(3);
14
+        int actual = bin1.getBin(3);
15 15
         //then
16 16
         assertEquals(expected, actual);
17 17
     }
18 18
 
19
-
19
+    @Test
20
+    public void testGetBinLoop() {
21
+        //given
22
+        Bins bin2 = new Bins(2);
23
+        for(int i=0; i<101; i++) {
24
+            bin2.incrementBin(7);
25
+        }
26
+        //when
27
+        int expected = 101;
28
+        int actual = bin2.getBin(7);
29
+        //then
30
+        assertEquals(expected, actual);
31
+    }
20 32
 }

+ 12
- 2
src/main/java/Dice.java Прегледај датотеку

@@ -1,6 +1,16 @@
1 1
 public class Dice {
2
+    private int diceRolled = 0;
2 3
 
4
+    public Dice(int numOfDice){
5
+        diceRolled = numOfDice;
6
+    }
3 7
 
8
+    public int tossAndSum(){
9
+        int toss = 0;
10
+        for (int i = 0; i < diceRolled; i++) {
11
+            toss += (int) (Math.ceil(Math.random() * 6));
12
+        }
13
+        return toss;
14
+    }
4 15
 
5
-
6
-}
16
+}

+ 40
- 0
src/main/java/DiceTest.java Прегледај датотеку

@@ -0,0 +1,40 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class DiceTest {
5
+    @Test
6
+    public void dieIsValid(){
7
+        // Given
8
+        Dice dice = new Dice(1);
9
+        boolean expected = true;
10
+
11
+        // When
12
+        boolean actual = false;
13
+        int dieRoll = dice.tossAndSum();
14
+        if (dieRoll <= 6 && dieRoll >= 1){
15
+            actual = true;
16
+        }
17
+
18
+        // Then
19
+        Assert.assertEquals(expected, actual);
20
+
21
+    }
22
+
23
+    @Test
24
+    public void rollThreeDice(){
25
+        // Given
26
+        Dice dice = new Dice(2);
27
+        boolean expected = true;
28
+
29
+        // When
30
+        boolean actual = false;
31
+        int rollTwo = dice.tossAndSum();
32
+        if (rollTwo <= 12 && rollTwo >= 2){
33
+            actual = true;
34
+        }
35
+
36
+        // Then
37
+        Assert.assertEquals(expected, actual);
38
+
39
+    }
40
+}

+ 2
- 0
src/main/java/Main.java Прегледај датотеку

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

+ 42
- 0
src/main/java/Simulation.java Прегледај датотеку

@@ -1,5 +1,47 @@
1
+import java.util.Formatter;
2
+
1 3
 public class Simulation {
2 4
 
5
+    private int diceNum=0;
6
+    private int tossNum=0;
7
+    private Dice dice = new Dice(diceNum);
8
+    private Bins bin = new Bins(diceNum);
9
+
10
+    public Simulation(int diceNum, int tossNum){
11
+        this.diceNum = diceNum;
12
+        this.tossNum = tossNum;
13
+        this.bin = bin;
14
+        this.dice = dice;
15
+}
16
+
17
+    public void runSimulation(){
18
+        for(int i=0; i<tossNum; i++){
19
+            bin.incrementBin(dice.tossAndSum());
20
+        }
21
+}
22
+
23
+    public void printSimulation() {
24
+
25
+        StringBuilder result = new StringBuilder();
26
+        Formatter format = new Formatter(result);
27
+
28
+        result.append("***\n Simulation of ");
29
+        result.append(diceNum);
30
+        result.append(" dice tossed for ");
31
+        result.append(tossNum);
32
+        result.append(" times.\n*** \n \n");
33
+        for (int i=0; i<((diceNum*6)-(diceNum)); i++) {
34
+            result.append(i+diceNum);
35
+            result.append(" : ");
36
+            format.format("%10d", bin.getBin(i+diceNum));
37
+            result.append(": ");
38
+            format.format("%.2f", (double)((bin.getBin(i+diceNum))/tossNum));
39
+
40
+        }
41
+        String printAns = result.toString();
42
+        System.out.print(printAns);
43
+    }
44
+
3 45
 
4 46
 
5 47
 }

+ 13
- 0
src/main/java/SimulationTest.java Прегледај датотеку

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