Using a Class - Another Marble Jar Puzzle

```class MarbleJar
{
public:
MarbleJar(int black, int white);	// initializes jar contents
apstring DrawMarble ( );		// draws and returns color
void AddMarble (apstring color);	// adds marble to jar
bool IsEmpty ( );			// returns true if empty
private:
int myNumberBlack, myNumberWhite;	// marbles in the jar
int myRandom ( );			// random number generator
};
```
Lab B: Guess the Color of the last Marble

Suppose a jar has an arbitrary mixture of black and white marbles. As long as there are at least two marbles in the jar, you are to draw two random marbles and

• if they are the same color, throw them both away and add a brand new black marble to the jar

• if they are different colors, throw away the black one and return the white marble to the jar

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?

Assignment Write a C++ program that will help you solve this puzzle. You will need` marbles.h` and `marbles.cpp`.

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.

Sample Run User input in bold italics.

```How many black marbles in the jar?    4
How many white marbles in the jar?    5
1.	I drew black and white -- replacing with a white marble.
2.	I drew white and white -- replacing with a black marble.
3.	I drew white and black -- replacing with a white marble.
4.	I drew white and black -- replacing with a white marble.
5.	I drew white and black -- replacing with a white marble.
6.	I drew white and white -- replacing with a black marble.
7.	I drew black and black -- replacing with a black marble.
8.	I drew black and white -- replacing with a white marble.
9.	Only one marble left . . it is white.
```
Analysis Once you have your program working, answer the following questions.
• Perhaps using repeated simulations as a guide, what is the answer to this puzzle? Can the process of drawing and replacing marbles go on forever? Why or why not?

• If you know the initial contents of the jar, can you predict the final outcome? Explain.

Continue to:  Unit 4  / Prev  / Next