Browse Source

Dicey Lab completed

Brandon Defrancis 6 years ago
parent
commit
289885774a

+ 9
- 0
pom.xml View File

@@ -7,6 +7,15 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+    </dependencies>
10 19
 
11 20
 
12 21
 </project>

+ 19
- 0
src/main/Test/BinsTest.java View File

@@ -0,0 +1,19 @@
1
+import org.junit.Test;
2
+import org.junit.Assert;
3
+
4
+public class BinsTest {
5
+
6
+    @Test
7
+    public void BinTest1(){
8
+
9
+        Bins bin1 = new Bins(2);
10
+        bin1.incrementBin(3);
11
+
12
+        int expected = 1;
13
+        int actual = bin1.getBin(3);
14
+
15
+        System.out.println(actual);
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+
19
+}

+ 37
- 0
src/main/Test/DiceTest.java View File

@@ -0,0 +1,37 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class DiceTest {
5
+
6
+    @Test
7
+    public void DiceTest1(){
8
+
9
+        Dice dice = new Dice(2);
10
+        boolean expected = true;
11
+
12
+
13
+        boolean actual = false;
14
+        int roll = dice.tossAndSum();
15
+        if (roll <= 12 && roll >= 1){
16
+            actual = true;
17
+        }
18
+        System.out.println(roll);
19
+        Assert.assertTrue(actual);
20
+    }
21
+
22
+    @Test
23
+    public void DiceTest2(){
24
+
25
+        Dice dice = new Dice(5);
26
+        boolean expected = true;
27
+
28
+
29
+        boolean actual = false;
30
+        int roll = dice.tossAndSum();
31
+        if (roll <= 30 && roll >= 5){
32
+            actual = true;
33
+        }
34
+        System.out.println(roll);
35
+        Assert.assertTrue(actual);
36
+    }
37
+}

+ 12
- 0
src/main/Test/SimulationTest.java View File

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

+ 29
- 0
src/main/java/Bins.java View File

@@ -1,4 +1,33 @@
1 1
 
2 2
 public class Bins {
3 3
 
4
+    private int diceNum;
5
+    private int[] results;
6
+
7
+    public Bins(int diceNum) {
8
+        this.diceNum = diceNum;
9
+        calculate(diceNum);
10
+    }
11
+
12
+    private void calculate(int diceNum) {
13
+        results = new int[(diceNum * 6) - (diceNum) + 1];
14
+
15
+        for (int i = 0; i < results.length; i++) {
16
+            results[i] = 0;
17
+        }
18
+    }
19
+
20
+    public void incrementBin(int binNumber) {
21
+
22
+    results[binNumber-diceNum] = (results[binNumber-diceNum]) + 1;
23
+
24
+    }
25
+
26
+    public int getBin(int binNumber) {
27
+
28
+        int test = results[(binNumber - diceNum)];
29
+        return test;
30
+    }
31
+
32
+
4 33
 }

+ 35
- 1
src/main/java/Dice.java View File

@@ -1,4 +1,38 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
2 4
 
5
+    private int numOfDice;
6
+
7
+    public Dice(int numOfDice){
8
+         this.numOfDice = numOfDice;
9
+    }
10
+
11
+    public int tossAndSum() {
12
+
13
+        Random rand = new Random();
14
+        int sumRoll = 0;
15
+
16
+        for (int i = 0; i < numOfDice; i++) {
17
+            sumRoll += (rand.nextInt(6) + 1);
18
+        }
19
+        return sumRoll;
20
+    }
21
+
22
+    }
23
+
24
+    /*public static void main(String[] args) {
25
+        Random rand = new Random();
26
+        int freq[] = new int[7];
27
+
28
+        for (int roll = 1; roll<1000; roll++){
29
+            ++freq[1+rand.nextInt(6)];
30
+        }
31
+
32
+        for (int face=1;face<freq.length;face++){
33
+            System.out.println(face + " " + freq[face]);
34
+        }
35
+    }*/
36
+
37
+
3 38
 
4
-}

+ 45
- 0
src/main/java/Simulation.java View File

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