ThuyKhong vor 6 Jahren
Ursprung
Commit
ad07245114
3 geänderte Dateien mit 36 neuen und 21 gelöschten Zeilen
  1. 4
    1
      src/main/java/Bins.java
  2. 6
    0
      src/main/java/BinsTest.java
  3. 26
    20
      src/main/java/Simulation.java

+ 4
- 1
src/main/java/Bins.java Datei anzeigen

@@ -3,19 +3,22 @@ import java.util.Random;
3 3
 public class Bins {
4 4
 
5 5
     private int max;
6
+    private int min;
6 7
     int[] bins;
7 8
 
8 9
     public Bins(int min, int max){
9 10
         this.max = max;
11
+        this.min = min;
10 12
         bins = new int[max + 1];
11 13
     }
12 14
 
13 15
     public int getBin(int binNumber){
14
-        return 0;
16
+        return bins[binNumber];
15 17
     }
16 18
 
17 19
     public void incrementBin(int binNumber){
18 20
 
21
+        bins[binNumber] = bins[binNumber] + 1;
19 22
     }
20 23
 
21 24
     public int[] getBins() {

+ 6
- 0
src/main/java/BinsTest.java Datei anzeigen

@@ -0,0 +1,6 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+public class BinsTest {
5
+
6
+    }

+ 26
- 20
src/main/java/Simulation.java Datei anzeigen

@@ -1,21 +1,29 @@
1
+import java.util.Arrays;
2
+
1 3
 public class Simulation {
2
-private int numberOfDies;
3
-private int numberOfTosses;
4
+    private int numberOfDies;
5
+    private int numberOfTosses;
4 6
 
5
-public Simulation(){
6
-}
7
+    public static void main(String[] args) {
8
+        Simulation simulation = new Simulation(2, 1000000);
9
+        Bins bins = simulation.runSimulation();
10
+        simulation.printResult(bins);
11
+    }
7 12
 
8
-public Simulation (int numberOfDies, int numberOfTosses){
9
-    this.numberOfDies = numberOfDies;
10
-    this.numberOfTosses = numberOfTosses;
11
-}
13
+    public Simulation(){
14
+    }
15
+
16
+    public Simulation (int numberOfDies, int numberOfTosses){
17
+        this.numberOfDies = numberOfDies;
18
+        this.numberOfTosses = numberOfTosses;
19
+    }
12 20
 
13 21
 
14
-    public Bins getArrayOfSum(int numberOfToss) {
22
+    public Bins runSimulation() {
15 23
         Dice dice = new Dice(this.numberOfDies);
16 24
         Bins bins = new Bins(this.numberOfDies, this.numberOfDies * 6);
17 25
 
18
-        for (int i = 0; i < numberOfToss; i++) {
26
+        for (int i = 0; i < this.numberOfTosses; i++) {
19 27
             int sum = dice.tossAndSum();
20 28
             bins.incrementBin(sum);
21 29
         }
@@ -23,18 +31,16 @@ public Simulation (int numberOfDies, int numberOfTosses){
23 31
         return bins;
24 32
     }
25 33
 
26
-    public String buildStats(Bins bins){
27
-        for (int i = numberOfDies; i <= numberOfDies * 6; i++){
34
+    public void printResult(Bins bins){
35
+        for (int i = numberOfDies; i <= (numberOfDies * 6); i++){
28 36
             int number = i;
29 37
             int count = bins.getBin(number);
30
-
31
-            System.out.println(bins.getBin(i));
38
+            double fraction = ((double) count)/ numberOfTosses;
39
+            System.out.print(String.format("%3d :%9d: %3.2f%% ", number,  count, fraction));
40
+            for (int j = 0; j <= (fraction * 100); j++) {
41
+                System.out.print("*");
42
+            }
43
+            System.out.println();
32 44
         }
33
-
34
-    }
35
-
36
-    public static void main(String[] args) {
37
-        Simulation simulation = new Simulation(2, 1000000);
38
-
39 45
     }
40 46
 }