Browse Source

Not done, just iffy on tests

ahsonali 6 years ago
parent
commit
fe2762654f
6 changed files with 173 additions and 1 deletions
  1. 13
    0
      pom.xml
  2. 25
    0
      src/Test/BinsTest.java
  3. 29
    0
      src/Test/DiceTest.java
  4. 24
    0
      src/main/java/Bins.java
  5. 23
    1
      src/main/java/Dice.java
  6. 59
    0
      src/main/java/Simulation.java

+ 13
- 0
pom.xml View File

@@ -8,5 +8,18 @@
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
+        </dependency>
23
+    </dependencies>
11 24
 
12 25
 </project>

+ 25
- 0
src/Test/BinsTest.java View File

@@ -0,0 +1,25 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class BinsTest {
6
+
7
+    @Test
8
+    public void binsConstructorTest()
9
+    {
10
+
11
+    }
12
+
13
+    @Test
14
+    public void getBin()
15
+    {
16
+
17
+    }
18
+
19
+    @Test
20
+    public void incrementBin()
21
+    {
22
+
23
+
24
+    }
25
+}

+ 29
- 0
src/Test/DiceTest.java View File

@@ -0,0 +1,29 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class DiceTest {
7
+
8
+    @Test
9
+    public void testNumberOfDies()
10
+    {
11
+        Dice dice = new Dice(5);
12
+
13
+        Integer expected = 5;
14
+        Integer actual = dice.numberofDies;
15
+
16
+        Assert.assertEquals(expected, actual);
17
+
18
+    }
19
+
20
+    @Test
21
+    public void testSumOfToss()
22
+    {
23
+        Dice dice = new Dice(3);
24
+
25
+        Integer actual = dice.tossAndSum();
26
+
27
+        Assert.assertTrue(actual >= dice.numberofDies && actual <= dice.numberofDies *6);
28
+    }
29
+}

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

@@ -1,4 +1,28 @@
1 1
 
2 2
 public class Bins {
3 3
 
4
+    private int startBound;
5
+    private int endBound;
6
+
7
+    private int[] sumOfDiceRolls;
8
+
9
+    /*This sets array of all possible sums
10
+    wiht the exception of 0 and 1*/
11
+    public Bins(int startBound, int endBound)
12
+    {
13
+        sumOfDiceRolls = new int[endBound + 1];
14
+    }
15
+
16
+    //This returns the index value of the sum of the Dies
17
+    public int getBin(int valueOfDiceRoll)
18
+    {
19
+        return sumOfDiceRolls[valueOfDiceRoll];
20
+    }
21
+
22
+    //This increments the Bin result by 1 each time it occurs
23
+    public void incrementBin(int valueOfDiceRoll)
24
+    {
25
+        sumOfDiceRolls[valueOfDiceRoll] = sumOfDiceRolls[valueOfDiceRoll] + 1;
26
+    }
27
+
4 28
 }

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

@@ -1,4 +1,26 @@
1
-public class Dice {
2 1
 
2
+import java.util.Random;
3 3
 
4
+public class Dice
5
+{
6
+    public int numberofDies;
7
+
8
+    public Dice(int numberofDies)
9
+    {
10
+        this.numberofDies = numberofDies;
11
+    }
12
+
13
+    public Integer tossAndSum()
14
+    {
15
+        Random random = new Random();
16
+
17
+        Integer sumOfToss = 0;
18
+
19
+        for (int i = 0; i < numberofDies; i ++)
20
+        {
21
+            sumOfToss += random.nextInt(6) + 1;
22
+        }
23
+
24
+        return sumOfToss;
25
+    }
4 26
 }

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

@@ -1,4 +1,63 @@
1
+import java.util.Random;
2
+
1 3
 public class Simulation {
4
+    private Integer numberOfTosses;
5
+    private int startBound;
6
+    private int endBound;
7
+    private Dice dice; //Using the Dice class
8
+    private Bins bins; //Using the Bins class
9
+
10
+    public Simulation(int numberOfDies, int numberOfTosses)
11
+    {
12
+        dice = new Dice(numberOfDies);
13
+
14
+        startBound = numberOfDies;
15
+        endBound = numberOfDies * 6;
16
+
17
+        this.numberOfTosses = numberOfDies;
18
+
19
+    }
20
+
21
+    public void runSimulation()
22
+    {
23
+        for (int i = 0; i < numberOfTosses; i++)
24
+        {
25
+            bins.incrementBin(dice.tossAndSum());
26
+        }
27
+    }
28
+
29
+    public String asterisks(Integer percentages)
30
+    {
31
+        StringBuilder sb = new StringBuilder();
32
+
33
+        for (int i = 0; i < percentages; i++)
34
+        {
35
+            sb.append("*");
36
+        }
37
+
38
+        return sb.toString();
39
+
40
+    }
41
+
42
+    public String printResults()
43
+    {
44
+        StringBuilder sb = new StringBuilder();
45
+        sb.append("***\nSimulation of " + startBound + " dice tossed for " + numberOfTosses + " times.\n***\n\n");
46
+
47
+        for (int i = startBound; i <= endBound; i++)
48
+        {
49
+            int diceSum = i;
50
+            int numberOfOccurence = bins.getBin(diceSum);
51
+
52
+            sb.append(String.format(" 2d : %7d: %.2f", diceSum, numberOfOccurence, (numberOfOccurence/(float)numberOfTosses)));
53
+            sb.append(asterisks((int) ((numberOfTosses/(float)numberOfTosses) + 100)) + "\n");
54
+        }
55
+
56
+        System.out.println(sb.toString());
57
+        return sb.toString();
58
+    }
59
+
60
+
2 61
 
3 62
 
4 63