Skip to content

Completed Dice, Bins and Simulation, achieved desired printout #42

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 1 commit 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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<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>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>


</project>
83 changes: 83 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,87 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class Bins {
private List<Integer> results = new ArrayList<>();
private Integer min;
private Integer max;

public Bins(Integer min, Integer max){
this.min = min;
this.max = max;
List<Integer> resultsList = this.getResults();
for(int i = min; i <= max; i++){
resultsList.add(0);
}
}

public List<Integer> getResults() {
return results;
}

public Integer getBin(Integer binNumber){

return this.getResults().get(binNumber);
}

public void incrementBin(Integer binNumber){
if(binNumber >= this.min && binNumber <= this.max){
Integer binIndex = binNumber - this.min;
Integer currentBinResult = this.getBin(binIndex);
this.results.set(binIndex, currentBinResult + 1);
}


}

public List<Double> tallyResults(Integer numberOfTosses){
List<Double> percentages = new ArrayList<Double>();
for(int i = 0; i < results.size(); i++){
double percentage = (double)results.get(i) / numberOfTosses;
BigDecimal bigDecimal = new BigDecimal(Double.toString(percentage));
bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_UP);
percentages.add(bigDecimal.doubleValue());
}
return percentages;
}

public void printResults(Integer numberOfTosses){
List<Double> talliedResultList = tallyResults(numberOfTosses);
Integer count = this.min;
Integer i = 0;
String printout = "";
int numberOfStars = 0;
for(Double element : talliedResultList) {
numberOfStars = (int)(element * 100);
i = 1;
printout += String.format("%3d", count) + ": " + String.format("%7d", this.getBin(count - 2)) +
" " + String.format("%1.2f", element) + " ";
while(i < numberOfStars){
printout += "*";
i++;
}
count++;
printout += "\n";
}
System.out.println(printout);
}

public Integer getMin() {
return min;
}

public void setMin(Integer min) {
this.min = min;
}

public Integer getMax() {
return max;
}

public void setMax(Integer max) {
this.max = max;
}

}
67 changes: 67 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
import java.util.ArrayList;
import java.util.List;

public class Dice {
private List<Integer> diceContainer = new ArrayList<Integer>();
private Integer numberOfDice;
private Integer sumOfDice;

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

public Dice(){
this.numberOfDice = 2;
}

public Integer getNumberOfDice(){
return this.numberOfDice;
}

public void setNumberOfDice(Integer numberOfDice){
this.numberOfDice = numberOfDice;
}

public List<Integer> getDiceList(){
return this.diceContainer;
}

public Integer getSumOfDice(){
return this.sumOfDice;
}

public void initializeDiceList(){
Integer numberOfDice = this.getNumberOfDice();
List<Integer> diceList = this.getDiceList();
for(int i = 0; i < numberOfDice; i++){
diceList.add(0);
}
}



public void tossDice(){
Integer min = 1;
Integer max = 7;
List<Integer> diceList = this.getDiceList();
Integer length = diceList.size();
for(int i = 0; i < length; i++){
diceList.set(i, (int)(Math.random() * (max - min)) + min);
}
}

public Integer tossAndSum(){
Integer sum = 0;
this.tossDice();
List<Integer> diceList = this.getDiceList();

for(int i = 0; i < diceList.size(); i++){
sum += diceList.get(i);
}
this.sumOfDice = sum;
return sum;
}

public void printDice(){
List<Integer> diceList = this.getDiceList();
for(int i = 0; i < diceList.size(); i++){
System.out.println((i + 1) + ": " + diceList.get(i));
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import java.util.List;

public class Simulation {
public static void main(String[] args) {
runSimXTimes(2, 1000000);

}

public static void runSimXTimes(Integer numberOfDice, Integer numberOfTosses){
Dice testDice = new Dice(numberOfDice);
Bins testBin = new Bins(testDice.getNumberOfDice(), testDice.getNumberOfDice() * 6);
testDice.initializeDiceList();

for(int i = 0; i < numberOfTosses; i++){
testBin.incrementBin(testDice.tossAndSum());
}
testBin.printResults(numberOfTosses);
System.out.println("testline");

}


}