CSC 221: Computer Programming I
Spring 2008

HW7: Marble Jar Simulations


In this assignment, you will make use of previously defined Java classes to help solve two brain-teaser puzzles. These puzzles, which are common among statisticians and discrete mathematicians, involve drawing black and white marbles from a jar.

  1. Suppose a jar contains the same number of black and white marbles. If you reach in and pull out two random marbles, are they more likely to be the same color or different colors?

  2. Suppose a jar has an arbitrary mixture of black and white marbles. As long as there are at least two marbles in the jar, draw two random marbles and: Can the marble drawing process go on forever? Furthermore, if you are told the initial contents of the jar, can you predict the final outcome?

PART 1: PuzzleOne

The MarbleJar and PuzzleOne classes have been provided for you.

PART 2: PuzzleTwo

Design and implement a class named PuzzleTwo that can be used to simulate the steps in the second puzzle. The user should be able to somehow specify the number of black and white marbles in the jar, then view the steps in the simulation. As each pair of marbles is drawn, a message describing the action taking place (including its number in the process), should be printed. The simulation should stop if fewer than two marbles remain in the jar.

For example, the output produced by the method should look like the following:

1: I drew black and white -- replacing a white marble. 2: I drew white and white -- replacing a black marble. 3: I drew white and black -- replacing a white marble. 4: I drew white and black -- replacing a white marble. . . . 18: I drew black and white -- replacing a white marble. 19: Only one marble left... it is white.