Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>Dicey-Lab</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
30 changes: 30 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
import java.beans.Transient;
import java.util.Map;
import java.util.TreeMap;

public class Bins {
private Integer binLow;
private Integer binHigh;
private Map<Integer, Integer> binMap = new TreeMap<>();

public Bins(Integer binLow, Integer binHigh) {
this.binLow = binLow;
this.binHigh = binHigh;

for (int i = binLow; i <= binHigh; i++) {
binMap.put(i, 0);
}
}

public Integer getBin(Integer binNum) {
return binMap.get(binNum);
}

public Map<Integer, Integer> getBinMap() {
return binMap;
}

public void incrementBin(Integer binNum) {
Integer qty = binMap.containsKey(binNum) ? binMap.get(binNum) : 0;
qty++;
binMap.put(binNum, qty);
}


}
22 changes: 22 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;


public class Dice {
private Integer numberOfDie;


public Dice(int numberOfDie) {
this.numberOfDie = numberOfDie;
}

public Integer tossAndSum(){
int random;
Integer sum = 0;
for (int i = 0; i < numberOfDie; i++) {
random = ThreadLocalRandom.current().nextInt(1,7);
sum += random;
}
return sum;
}

public Integer getNumberOfDie() {
return numberOfDie;
}
}
50 changes: 50 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
import java.util.Map;

public class Simulation {
private Integer numberOfDies;
private Integer numberOfTosses;
private Bins bins;

public Simulation(Integer numberOfDies, Integer numberOfTosses) {
this.numberOfDies = numberOfDies;
this.numberOfTosses = numberOfTosses;
}

public Bins runSimulation() {
Dice dice = new Dice(numberOfDies);
bins = new Bins(numberOfDies, numberOfDies * 6);
Integer total;

for (int i = 0; i < numberOfTosses; i++) {
total = dice.tossAndSum();
bins.incrementBin(total);
}
return bins;
}

public void printResults() {
System.out.println("***");
System.out.println("Simulation of " + numberOfDies + " dice tossed for " + numberOfTosses + " times.");
System.out.println("***\n");
for (Map.Entry value : bins.getBinMap().entrySet()) {
Integer val = (Integer) value.getValue();
Double percent = Double.valueOf(val) / numberOfTosses;

String stringFormat = String.format("%2d : %8d : %.2f", value.getKey(), value.getValue(), percent);
System.out.println(stringFormat + " " + printStars(val));

}
}

public String printStars(Integer number) {
Integer dividedByTenThousand = number / 10000;
String stars = "";
for (int i = 0; i < dividedByTenThousand; i++) {
stars += "*";
}
return stars;
}

public Integer getNumberOfDies() {
return numberOfDies;
}

public Integer getNumberOfTosses() {
return numberOfTosses;
}
}
8 changes: 8 additions & 0 deletions src/main/java/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class main {
public static void main(String[] args) {
Simulation simulation = new Simulation(2, 1_000_000);
simulation.runSimulation();
simulation.printResults();

}
}
20 changes: 20 additions & 0 deletions src/test/java/SimulationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import org.junit.Assert;
import org.junit.Test;

public class SimulationTest {
@Test
public void constructorTest(){
//given
Simulation simulation = new Simulation(2, 1000000);
Integer expectedNumberOfDies = 2;
Integer expectedNumberOfTosses = 1000000;
//when
Integer actualNumberOfDies = simulation.getNumberOfDies();
Integer actualNumberOfTosses = simulation.getNumberOfTosses();
//then
Assert.assertEquals(expectedNumberOfDies, actualNumberOfDies);
Assert.assertEquals(expectedNumberOfTosses, actualNumberOfTosses);
}


}
32 changes: 32 additions & 0 deletions src/test/java/TestBins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.junit.Assert;
import org.junit.Test;

public class TestBins {

@Test
public void constructorTest(){
//given
Bins bins = new Bins(2, 12);
Integer expectedSize= 11;
//when
Integer actualSize = bins.getBinMap().size();
//then
Assert.assertEquals(expectedSize, actualSize);
}

@Test
public void incrementBinTest(){
//given
Bins bins = new Bins(2, 12);
Integer expected = 1;
//when
bins.incrementBin(2);
Integer actual = bins.getBin(2);
//then
Assert.assertEquals(expected, actual);

}



}
28 changes: 28 additions & 0 deletions src/test/java/TestDice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.junit.Assert;
import org.junit.Test;

public class TestDice {


@Test
public void constructorTest(){
//given
Dice dice = new Dice(5);
Integer expected = 5;

//when
Integer actual = dice.getNumberOfDie();
//then
Assert.assertEquals(expected, actual);
}

@Test
public void tossAndSumTest(){
//given
Dice dice = new Dice(2);
//when
Integer actual = dice.tossAndSum();
//then
Assert.assertTrue(actual <= 12); // max sum of toss is 12 bec 2 dice * 6
}
}