Sfoglia il codice sorgente

Merge d0a389d12361cec8ff3922aa4d9e44380402ab85 into 1eaff706f0dcc7a467d1c6648bcb494e313ba6a5

danzcw 6 anni fa
parent
commit
2f95aaf6bd
No account linked to committer's email

+ 26
- 1
pom.xml Vedi File

@@ -7,6 +7,31 @@
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>4.12</version>
15
+        </dependency>
16
+        <dependency>
17
+            <groupId>junit</groupId>
18
+            <artifactId>junit</artifactId>
19
+            <version>RELEASE</version>
20
+        </dependency>
21
+    </dependencies>
10 22
 
11 23
 
12
-</project>
24
+</project>
25
+
26
+<!--<?xml version="1.0" encoding="UTF-8"?>-->
27
+<!--<project xmlns="http://maven.apache.org/POM/4.0.0"-->
28
+         <!--xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-->
29
+         <!--xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">-->
30
+    <!--<modelVersion>4.0.0</modelVersion>-->
31
+
32
+    <!--<groupId>com.zipcodewilmington</groupId>-->
33
+    <!--<artifactId>Dicey-Lab</artifactId>-->
34
+    <!--<version>1.0-SNAPSHOT</version>-->
35
+
36
+
37
+<!--</project>-->

+ 23
- 0
src/main/java/Bins.java Vedi File

@@ -1,4 +1,27 @@
1
+import java.util.TreeMap;
1 2
 
2 3
 public class Bins {
3 4
 
5
+    int minIndex;
6
+    int maxIndex;
7
+
8
+    private int[] results;
9
+
10
+    public Bins (int minIndex, int maxIndex) {
11
+        results = new int[maxIndex +1];
12
+    }
13
+
14
+    public int getBins(int outcome) {
15
+        return results[outcome];
16
+    }
17
+
18
+    public void incrementBin(int outcome) {
19
+        results[outcome]++;
20
+    }
21
+
4 22
 }
23
+
24
+
25
+//    Bins results = new Bins(2, 12); // for bins from 2..12
26
+//    Integer numberOfTens = results.getBin(10); // returns the number of tens in the 10 bin
27
+//    results.incrementBin(10); // should increment bin # 10

+ 15
- 0
src/main/java/DanielResults.md Vedi File

@@ -0,0 +1,15 @@
1
+***
2
+Simulation of 2 dice tossed for 1000000 times.
3
+***
4
+
5
+  2 :     27854: 0.03**
6
+  3 :     55661: 0.06*****
7
+  4 :     83465: 0.08********
8
+  5 :    110837: 0.11***********
9
+  6 :    138840: 0.14*************
10
+  7 :    166577: 0.17****************
11
+  8 :    139243: 0.14*************
12
+  9 :    111241: 0.11***********
13
+ 10 :     82793: 0.08********
14
+ 11 :     55743: 0.06*****
15
+ 12 :     27746: 0.03**

+ 18
- 0
src/main/java/Dice.java Vedi File

@@ -1,4 +1,22 @@
1
+
2
+
1 3
 public class Dice {
2 4
 
5
+    int numOfDie;
6
+
7
+    public Dice () {
8
+
9
+    }
10
+
11
+    public int rollDice(int numOfDie) {
12
+        Dice dice1 = new Dice();
13
+        int sumOfRoll = 0;
14
+        for (int roll = 1; roll <= numOfDie; roll++) {
15
+            sumOfRoll += (int) (Math.random() * 6 + 1);
16
+        }
17
+        return sumOfRoll;
18
+
19
+    }
20
+
3 21
 
4 22
 }

+ 68
- 2
src/main/java/Simulation.java Vedi File

@@ -1,5 +1,71 @@
1
-public class Simulation {
1
+public class Simulation extends Dice {
2 2
 
3
+    private int numberOfRolls;
4
+    private int numberOfDice;
5
+    private Dice dice;
6
+    private Bins bins;
7
+    private int minIndex;
8
+    private int maxIndex;
3 9
 
4 10
 
5
-}
11
+    public Simulation() {
12
+
13
+    }
14
+
15
+    public Simulation (int numberOfDice, int minIndex) {
16
+        dice = new Dice();
17
+
18
+        minIndex = numberOfDice;
19
+        maxIndex = numberOfDice * 6;
20
+
21
+        bins = new Bins(minIndex, maxIndex);
22
+
23
+        this.numberOfRolls = numberOfRolls;
24
+          }
25
+
26
+    public void runSimulation (int numberOfRolls, int rollsOfDie) {
27
+
28
+        for (int i = 0; i < numberOfRolls; i++)
29
+            bins.incrementBin(dice.rollDice(rollsOfDie));
30
+
31
+        }
32
+
33
+     public String printIt(int minIndex, int maxIndex, int numberOfRolls) {
34
+         StringBuilder sb = new StringBuilder();
35
+             sb.append("***\nSimulation of " + minIndex +" dice tossed for " + numberOfRolls + " times.\n***\n\n");
36
+        for (int y = minIndex; y <= maxIndex; y++){
37
+            int diceSum = y;
38
+            int numOfOccurrence = bins.getBins(diceSum);
39
+            sb.append(String.format(" %2d :   %7d: %.2f", diceSum, numOfOccurrence, (numOfOccurrence/(float)numberOfRolls)));
40
+            sb.append(numberOfStars((int) ((numOfOccurrence/(float)numberOfRolls)*100))+ "\n");
41
+         }
42
+
43
+         System.out.println(sb.toString());
44
+            return sb.toString();
45
+
46
+     }
47
+
48
+     public String numberOfStars(int percentOutcome){
49
+        StringBuilder sb1 = new StringBuilder();
50
+        for (int otc = 0; otc < percentOutcome; otc++) {
51
+            sb1.append("*");
52
+        }
53
+        return sb1.toString();
54
+     }
55
+
56
+
57
+
58
+    public static void main(String[] args) {
59
+        Simulation simulation = new Simulation(2,2);
60
+        simulation.runSimulation(1000000,2);
61
+        simulation.printIt(2,12,1000000);
62
+
63
+
64
+
65
+
66
+
67
+
68
+    }
69
+
70
+    }
71
+

+ 26
- 0
src/test/BinsTest.java Vedi File

@@ -0,0 +1,26 @@
1
+import org.junit.Before;
2
+import org.junit.Test;
3
+import org.junit.Assert;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class BinsTest {
8
+
9
+    @Before
10
+    public void setUp() throws Exception {
11
+        Bins bins = new Bins(2, 12);
12
+        Integer expected1 = 0;
13
+        Integer actual1 = bins.getBins(3);
14
+        Assert.assertEquals(expected1, actual1);
15
+
16
+    }
17
+
18
+    @Test
19
+    public void getBins() {
20
+
21
+    }
22
+
23
+    @Test
24
+    public void incrementBin() {
25
+    }
26
+}

+ 27
- 0
src/test/SimulationTest.java Vedi File

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