/** * Class that simulates drawing two marbles from a jar. * @author Dave Reed * @version 4/7/08 */ public class PuzzleOne{ private int numPairs; /** * Constructs a MarbleTester object. * @param exp the number of marble drawing experiments to conduct. */ public PuzzleOne(int exp) { this.numPairs = exp; } /** * Repeatedly draws two marbles and keeps track of how often they are the same color. * @param black the number of black marbles in the jar * @param white the number of white marbles in the jar * @return the percentage of times that the drawn marbles were the same color */ public double simulate(int black, int white) { if (black + white < 2 || this.numPairs == 0) { return 0.0; } else { MarbleJar jar = new MarbleJar(black, white); int numSame = 0; for (int i = 0; i < numPairs; i++) { String marble1 = jar.drawMarble(); String marble2 = jar.drawMarble(); if (marble1.equals(marble2)) { numSame++; } jar.addMarble(marble1); jar.addMarble(marble2); } return 100.0*numSame/this.numPairs; } } }