Преглед на файлове

Merge ded94ff6238a1b8b84a122b100c3cf3ffb8a0fe6 into 1eaff706f0dcc7a467d1c6648bcb494e313ba6a5

ahsonali преди 6 години
родител
ревизия
cbe43d8cff
No account linked to committer's email
променени са 6 файла, в които са добавени 194 реда и са изтрити 1 реда
  1. 13
    0
      pom.xml
  2. 33
    0
      src/Test/DiceTest.java
  3. 25
    0
      src/Test/SimulationTest.java
  4. 30
    0
      src/main/java/Bins.java
  5. 23
    1
      src/main/java/Dice.java
  6. 70
    0
      src/main/java/Simulation.java

+ 13
- 0
pom.xml Целия файл

@@ -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>

+ 33
- 0
src/Test/DiceTest.java Целия файл

@@ -0,0 +1,33 @@
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
+
30
+
31
+
32
+
33
+}

+ 25
- 0
src/Test/SimulationTest.java Целия файл

@@ -0,0 +1,25 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.*;
5
+
6
+public class SimulationTest {
7
+
8
+    @Test
9
+    public void runSimulation() {
10
+    }
11
+
12
+    @Test
13
+    public void testAsterisks()
14
+    {
15
+        Simulation simulation = new Simulation(2, 10);
16
+
17
+        String expected = "****";
18
+
19
+        String actual = simulation.asterisks(4);
20
+
21
+        Assert.assertEquals(expected, actual);
22
+    }
23
+
24
+
25
+}

+ 30
- 0
src/main/java/Bins.java Целия файл

@@ -1,4 +1,34 @@
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
+    with the exception of 0 and 1*/
11
+
12
+    public Bins()
13
+    {
14
+
15
+    }
16
+
17
+    public Bins(int startBound, int endBound)
18
+    {
19
+        sumOfDiceRolls = new int[endBound + 1];
20
+    }
21
+
22
+    //This returns the index value of the sum of the Dies
23
+    public int getBin(int valueOfDiceRoll)
24
+    {
25
+        return sumOfDiceRolls[valueOfDiceRoll];
26
+    }
27
+
28
+    //This increments the Bin result by 1 each time it occurs
29
+    public void incrementBin(int valueOfDiceRoll)
30
+    {
31
+        sumOfDiceRolls[valueOfDiceRoll] = sumOfDiceRolls[valueOfDiceRoll] + 1;
32
+    }
33
+
4 34
 }

+ 23
- 1
src/main/java/Dice.java Целия файл

@@ -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
 }

+ 70
- 0
src/main/java/Simulation.java Целия файл

@@ -1,4 +1,74 @@
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 = numberOfTosses;
18
+
19
+        bins = new Bins(startBound, endBound);
20
+
21
+    }
22
+
23
+    public void runSimulation()
24
+    {
25
+        for (int i = 0; i < numberOfTosses; i++)
26
+        {
27
+            bins.incrementBin(dice.tossAndSum());
28
+        }
29
+    }
30
+
31
+    public static String asterisks(Integer percentages)
32
+    {
33
+        StringBuilder sb = new StringBuilder();
34
+
35
+        for (int i = 0; i < percentages; i++)
36
+        {
37
+            sb.append("*");
38
+        }
39
+
40
+        return sb.toString();
41
+
42
+    }
43
+
44
+    public String printResults()
45
+    {
46
+        StringBuilder sb = new StringBuilder();
47
+        sb.append("***\nSimulation of " + startBound + " dice tossed for " + numberOfTosses + " times.\n***\n\n");
48
+
49
+        for (int i = startBound; i <= endBound; i++)
50
+        {
51
+            int diceSum = i;
52
+            int numberOfOccurence = bins.getBin(diceSum);
53
+            int percent = (int)(numberOfOccurence/(float)numberOfTosses * 100);
54
+
55
+            sb.append(String.format("%2d : %7d: %.2f", diceSum, numberOfOccurence, (numberOfOccurence/(float)numberOfTosses)));
56
+            sb.append(asterisks(percent) + "\n");
57
+        }
58
+
59
+        System.out.println(sb.toString());
60
+        return sb.toString();
61
+    }
62
+
63
+    public static void main(String[] args)
64
+    {
65
+        Simulation simulation = new Simulation(2, 1000000);
66
+        simulation.runSimulation();
67
+        simulation.printResults();
68
+
69
+    }
70
+
71
+
2 72
 
3 73
 
4 74