Browse Source

Completed Simulation and text output

Trinh Tong 6 years ago
parent
commit
3eaf011465
3 changed files with 47 additions and 0 deletions
  1. 9
    0
      src/main/java/Main.java
  2. 37
    0
      src/main/java/Simulation.java
  3. 1
    0
      src/test/java/BinsTest.java

+ 9
- 0
src/main/java/Main.java View File

@@ -1,2 +1,11 @@
1
+import java.io.FileNotFoundException;
2
+
1 3
 public class Main {
4
+
5
+    public static void main(String[] args) throws FileNotFoundException {
6
+
7
+        Simulation mainSimulation = new Simulation(2, 1000000);
8
+
9
+        mainSimulation.printToFile();
10
+    }
2 11
 }

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

@@ -1,3 +1,7 @@
1
+import java.io.File;
2
+import java.io.FileNotFoundException;
3
+import java.io.PrintStream;
4
+
1 5
 public class Simulation {
2 6
 
3 7
     int dice = 0;
@@ -20,4 +24,37 @@ public class Simulation {
20 24
             }
21 25
         }
22 26
     }
27
+
28
+    public void printToFile() throws FileNotFoundException {
29
+        // Output to file
30
+
31
+        PrintStream file = new PrintStream(new File("trinhResults.md"));
32
+        PrintStream console = System.out;
33
+
34
+        System.setOut(file);
35
+
36
+        StringBuilder resultsOutput = new StringBuilder();
37
+
38
+        resultsOutput.append("***\nSimulation of " + this.dice + " dice tossed for " + this.tosses + ".\n***\n\n");
39
+
40
+        for (int key: simulationBins.allKeys) {
41
+            int value = simulationBins.results.get(key);
42
+            resultsOutput.append(String.format("%3d : %8d: %4.2f ", key, value, ((double) value / (double) this.tosses)));
43
+            int starCounter = (int) Math.floor((double) ( value / ( this.tosses / 100)));
44
+            resultsOutput.append(repeatString("*", starCounter) + "\n");
45
+        }
46
+
47
+        System.out.println(resultsOutput.toString());
48
+
49
+    }
50
+
51
+    public String repeatString(String stringToRepeat, int numberOfTimes){
52
+        String returnString = "";
53
+
54
+        for (int i = 0; i < numberOfTimes; i++) {
55
+            returnString += stringToRepeat;
56
+        }
57
+
58
+        return returnString;
59
+    }
23 60
 }

+ 1
- 0
src/test/java/BinsTest.java View File

@@ -61,3 +61,4 @@ public class BinsTest {
61 61
         Assert.assertEquals(max, actual);
62 62
     }
63 63
 }
64
+