Skip to content

Completed dicey #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
<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.12</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
24 changes: 24 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import java.util.HashMap;
import java.util.Map;

public class Bins {
//map<key,value>
Map<Integer, Integer> mapOfBins = new HashMap<>();
private Integer minimumNumber;
private Integer maximumNumber;


public Bins(int minimumNumber, int maximumNumber) {
this.minimumNumber = minimumNumber;
this.maximumNumber = maximumNumber;
for (int i = minimumNumber; i <= maximumNumber; i++) {
mapOfBins.put(i, 0);
}
}
public Integer getBin(int binNumber) {
return mapOfBins.get(binNumber);
}
public void incrementBin(Integer binNumber) {
if (binNumber >= this.minimumNumber && binNumber <= this.maximumNumber) {
Integer currentBinResult = this.getBin(binNumber);
this.mapOfBins.put(binNumber, currentBinResult + 1);//
}
}

}
20 changes: 20 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@


public class Dice {
private Integer numberOnDice;

public Dice(Integer numberOnDice) {
this.numberOnDice = numberOnDice;
}

public int tossAndSum(){
int sum = 0;
for (int i = 0; i < numberOnDice; i++) {
sum += (int) (Math.random() * 6) + 1;
}
return sum;

}

public Integer getNumberOnDice() {
return numberOnDice;
}


}
45 changes: 45 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
public class Simulation {
private Integer numberOfDice;
private Integer numberOfTosses;




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

public static void main(String[] args) {
Simulation simulation = new Simulation(2, 10000);
simulation.printResults(simulation.runSimulation());
}

private Bins runSimulation() {
Dice dice = new Dice(numberOfDice);
Integer maxValue = numberOfDice * 6;
Bins bin = new Bins(numberOfDice, maxValue);
for (int i = 0; i < numberOfTosses; i++) {
bin.incrementBin(dice.tossAndSum());//increments to random numbers between start and end
}
return bin;
}
private void printResults(Bins bin) {
Integer maxValue = numberOfDice * 6;
//loop through results array and divide by the total number of tosses
for (int i = numberOfDice; i <= maxValue ; i++) {
System.out.print(String.format("%2d : %4d : %1.2f ", i, bin.getBin(i), binPercent(bin.getBin(i))));
printStars(binPercent(bin.getBin(i)));
System.out.println();
}
}
public void printStars(Double value) {
int numberOfStars = (int) (value * 100);
for (int i = 1; i <= numberOfStars; i++) {
System.out.print("*");
}
}

public double binPercent(Integer value) {
return Double.valueOf(value) / numberOfTosses;
}




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


public class BinsTest {
@Test
public void getBin() {
//given
Bins bins = new Bins(2, 12);
//When
int actual = bins.getBin(4);

//Then
Assert.assertEquals(0, actual);
}
@Test
public void incrementTest(){
Bins bin = new Bins(2,12);
Integer expected = 2;
bin.incrementBin(10);
bin.incrementBin(10);

Assert.assertEquals(expected, bin.getBin(10));
}
}
19 changes: 19 additions & 0 deletions src/test/java/DiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import junit.framework.TestCase;
import org.junit.Assert;

public class DiceTest extends TestCase {

public void testTossAndSum() {
Dice dice = new Dice(1);
int result = dice.tossAndSum();
//Assert.assertEquals(2,result);
Assert.assertTrue(result>0 && result <7);
}

public void testGetNumberOnDice() {
Dice dice = new Dice(2);
Integer expected = dice.getNumberOnDice();
Integer actual = 2;
Assert.assertEquals(expected, actual);
}
}