CS 132: Intro to Computer Science II

Spring 1999

Assignment 3

The Marble Jar: Using Loops and Java Classes

Due Monday February 15

In this assignment, you will make use of a previously defined Java class in order to perform two different simulations. The simulations involve repetition, counting, and the use of abstraction in accessing the method of the provided class. The point of the simulations is to help you solve two different brain-teaser puzzles involving jars containing black and white marbles. Puzzles such as these are common, especially among statisticians and discrete mathematicians.

  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?

To help in solving these kinds of puzzles, it will be useful to have a class which simulates the operations allowed on a jar of marbles. Such a class is defined in the file MarbleJar.java and it is documented online. The source code for the MarbleJar class is available in ~miller/cs132/lab3. You will need to copy it to your own lab3 directory.

It is important to note that this class provides the necessary operations for manipulating a jar and only those operations. For many puzzles, allowing the person to peek inside the jar would tarnish the result. As such, the data fields which store the number of marbles are hidden in the private section of the class. The person is only allowed to remove a random marble, add a marble of a specific color, and know when the jar is empty (equivalent to picking up the jar and shaking it to see if there is anything inside).

Part 1

Using the MarbleJar class, write a Java program called Puzzle1.java that will help you solve the first marble puzzle described above. Your program should prompt the user for the initial number of marbles of each color. Then it should repeatedly draw two marbles, noting when they are the same color and then returning them to the jar. It should do this some constant number of times (say 1000) and then output the number and percentage of each type of result. For example, below is a sample run (with user input in bold):
How many marbles of each color in the jar?  10

Out of 1000 trials,
    same colors drawn 479 times
    different colors drawn 521 times

That means they were the same 47.9% of the time.
ANALYSIS

Once you have your program working, answer the following questions:

PART 2

Similarly, write a Java program called Puzzle2.java that will help you solve the second puzzle. Your program should prompt the user for the initial contents of the jar and create a corresponding MarbleJar object. It should then repeatedly draw two marbles from the jar and perform the appropriate action based on the colors of the drawn marbles. If the process ends (with less than two marbles in the jar), then the final contents of the jar should be displayed. For example:

How many black marbles in the jar?  4
How many white marbles in the jar?  5

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.
5:  I drew white and black -- replacing a white marble.
6:  I drew white and white -- replacing a black marble.
7:  I drew black and black -- replacing a black marble.
8:  I drew black and white -- replacing a white marble.
9:  Only one marble left... it is white.
ANALYSIS

Once you have your program working, answer the following questions:

Submission

Turn in hardcopies of Puzzle1.java and Puzzle2.java. Also provide scripts of you running and testing your programs. Finally, submit your answers to the questions created with either a text editor or a word processor.