Question: import java.util.Scanner; public class Craps { private static final int MIN_ROLL = 2; // minimum value of a roll private static final int MAX_ROLL =
import java.util.Scanner;
public class Craps { private static final int MIN_ROLL = 2; // minimum value of a roll private static final int MAX_ROLL = 12; // maximum value of a roll private static final int MAX_LENGTH = 36; // maximum length of bars
public static void main(String[] args) { int[] counts = new int[MAX_ROLL + 1];
Die myDie1 = new Die(); Die myDie2 = new Die();
Scanner scan = new Scanner(System.in); System.out.println("How many dice rolls do you want?"); int total = scan.nextInt();
for (int z = 0; z < total; z++){ int x = myDie1.roll(); int y = myDie2.roll(); int dicesum = x + y; counts[dicesum] = counts[dicesum] + 1; }
System.out.println("Win: " + ((counts[7] + counts[11])/(float) total) ); System.out.println("Lose: " + ((counts[2] + counts[3] + counts[12])/(float)total) ); System.out.println("Roll Again: " + ((counts[4]+counts[5]+counts[6]+counts[8]+counts[9]+counts[10]))/(float) total + " ");
Histogram histo = new Histogram(counts, 2, 14, MAX_LENGTH); histo.drawHor(); histo.drawVer(); } }
******************************************************************
public class Die { private final int MAX = 6;
private int faceValue;
public Die() { faceValue = 1; }
public int roll() { faceValue = (int)(Math.random() * MAX) + 1;
return faceValue; }
public void setFaceValue (int value) { faceValue = value; }
public int getFaceValue() { return faceValue; }
public String toString() { String result = Integer.toString(faceValue);
return result; } }
*********************************************************************
public class Histogram { private int [] values; private int minIndex; private int maxIndex; private int maxLength;
public Histogram(int [] values, int minIndex, int maxIndex, int maxLength) { this.values = new int [11]; this.minIndex = minIndex; this.maxIndex = maxIndex; this.maxLength = maxLength;
int maxValue = 0; for (int x : values){ if (x > maxValue){ maxValue = x; } } int m = 0; for (int x : values){ if (x == 0) continue; int z = (x * maxLength) / maxValue; this.values[m] = z; m++; } }
public void drawHor() { String star = ""; int m = 1; for (int x : this.values){ m++; for (int i = 0; i < x; i++){ star += "*"; } if (m < 10) System.out.println("Value " + m + ": " + star + x); else System.out.println("Value " + m + ": " + star + x); star = ""; } System.out.println(); }
public void drawVer() { for (int i = 36; i >= 1; i--){ if (i > 9) System.out.print("Count " + i); else System.out.print("Count " + i); for (int x : this.values){ if (x >= i) System.out.print(" * "); else System.out.print(" "); } System.out.print(" "); if (i == 1) System.out.print("Value: 2 3 4 5 6 7 8 9 10 11 12"); } }
}
Questions:
1) When we declared the counts array why did we need to add one to MAX_ROLL for the size of the array? What happens if you remove the + 1 from the declaration of the counts array size and run the program?
2) Each time you run the program with a large number of dice rolls (e.g. >=1000), you get a distinctive distribution and the distribution shows very similar values from one run to the next. What happens when you run the program several different times with a small number of dice rolls (e.g. <= 100 or <= 10)? Can you explain what is happening?
3) Why do we not want to use any names based on the game of Craps in the Histogram class code?
4) In the Histogram constructor, we made a scaled copy of each element of the data supplied in the original parameter array. Why did we do that and what would be the consequences if we did not?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
