Week1Mock1FRQ1_JessieZeng

Run Settings
LanguageJava
Language Version
Run Command
// import java.util.*; public class Main { public static void main(String[] args) { // Jessie Zeng, CH, Week 1, Mock 1, PR_PracticeTest_1.pdf // AP CS A // FRQ 1, DiceSumlation DiceSimulation s1 = new DiceSimulation(10, 6); // int roll = s1.roll(); // System.out.println(roll); int result = s1.runSimulation(); System.out.println(result); // Output: ______ } }
public class DiceSimulation { /** Sample size of simulation */ private int numSampleSize; /** Number of faces on each die */ private int numFaces; /** Constructs a DiceSimulation where sampleSize is the number of rolls to be simulated and * faces is the number of faces on each dice (some dice have many more or fewer than 6 faces) */ public DiceSimulation(int numSamples, int faces) { numSampleSize = numSamples; numFaces = faces; } /** Returns an integer from 1 to the number of faces to simulate a die roll */ public int roll() { // to be implemented in part (a) int roll = (int) (Math.random()*numFaces)+1; return roll; } /** Simulates rolling two dice with the number faces given, for the number of sample size * rolls. Returns the percentage of matches that were rolled * as an integer (e.g., 0.50 would be 50) */ public int runSimulation() { // to be implemented in part (b) int matches = 0; for (int i = 0; i < numSampleSize; i++) { int roll1 = roll(); int roll2 = roll(); // System.out.println(roll1 + " " + roll2); if (roll1 == roll2) { matches++; } } // System.out.println(matches); // The following line is Jessie's code. // return (int) (double) matches/numSampleSize*100); // The line below is from the canonical solution. return (int) ((1.0 * matches / numSampleSize) * 100); } }
Editor Settings
Theme
Key bindings
Full width
Lines