Seth 6 lat temu
rodzic
commit
487df59138

BIN
.DS_Store Wyświetl plik


+ 6
- 1
pom.xml Wyświetl plik

@@ -7,6 +7,11 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>Dicey-Lab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
-
10
+    <dependency>
11
+        <groupId>junit</groupId>
12
+        <artifactId>junit</artifactId>
13
+        <version>4.12</version>
14
+        <scope>test</scope>
15
+    </dependency>
11 16
 
12 17
 </project>

BIN
src/.DS_Store Wyświetl plik


BIN
src/main/.DS_Store Wyświetl plik


+ 32
- 0
src/main/java/Bins.java Wyświetl plik

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

+ 18
- 0
src/main/java/Dice.java Wyświetl plik

@@ -1,4 +1,22 @@
1
+import java.util.Random;
2
+
1 3
 public class Dice {
2 4
 
5
+    private int dice;
6
+
7
+    public Dice(int dice) {
8
+
9
+        this.dice = dice;
10
+    }
11
+
12
+    public int tossAndSum(){
13
+        int sum = 0;
14
+        //Random random = new Random();
15
+        for (int i = 0; i <= dice ; i++) {
16
+            sum += (int) (Math.ceil(Math.random() * 6));
17
+
18
+        }
19
+        return sum;
20
+    }
3 21
 
4 22
 }

+ 10
- 0
src/main/java/Main.java Wyświetl plik

@@ -0,0 +1,10 @@
1
+public class Main {
2
+
3
+
4
+    public static void main(String[] args) {
5
+        Simulation run = new Simulation(2, 1000000);
6
+
7
+        run.runSimulation();
8
+        System.out.println(run.printResults());
9
+    }
10
+}

+ 60
- 0
src/main/java/Simulation.java Wyświetl plik

@@ -1,5 +1,65 @@
1
+import com.sun.javafx.binding.StringFormatter;
2
+
3
+import java.util.Formatter;
4
+
1 5
 public class Simulation {
2 6
 
7
+    private Dice dice;
8
+    private Bins diceBin;
9
+    private int tosses;
10
+    private int diceNum;
11
+
12
+    public Simulation(int diceNum, int tosses) {
13
+
14
+          this.dice = new Dice(diceNum);
15
+          this.diceBin = new Bins(diceNum);
16
+          this.tosses = tosses;
17
+          this.diceNum = diceNum;
18
+
19
+
20
+    }
21
+
22
+
23
+    public void runSimulation() {
24
+        for (int i = 0; i < tosses ; i++) {
25
+            diceBin.incrememtBin(dice.tossAndSum());
26
+
27
+        }
28
+
29
+    }
30
+
31
+
32
+    public String printResults() {
33
+        runSimulation();
34
+        StringBuilder simulation = new StringBuilder();
35
+        Formatter adjust = new Formatter();
36
+
37
+        simulation.append("```\n***\nSimulation of ");
38
+        simulation.append(diceNum);
39
+        simulation.append(" dice tossed for ");
40
+        simulation.append(tosses);
41
+        simulation.append(" times.\n***\n\n");
42
+
43
+        for (int i = 2; i <(diceNum * 6); i++) {
44
+            int timesThrown = diceBin.getBin(diceNum);
45
+            simulation.append(i + " : ");
46
+            adjust.format("%10d", diceBin.getBin(i) );
47
+            simulation.append(": ");
48
+            adjust.format("%.2f", (double)timesThrown/(double)tosses);
49
+                for(int j=0; j<(((double)timesThrown/(double)tosses)); j++) {
50
+
51
+                    {simulation.append(" *");}
52
+
53
+                }
54
+            simulation.append("\n```");
55
+
56
+        }
57
+
58
+            String result = simulation.toString();
59
+            return result;
60
+
61
+    }
62
+
3 63
 
4 64
 
5 65
 }