/** * Class that models a jar containing black and white marbles. * @author Dave Reed * @version 4/9/08 */ public class MarbleJar { private int numBlack; // number of black marbles in the jar private int numWhite; // number of white marbles in the jar /** * Constructs a marble jar. * @param black number of black marbles initially in the jar * @param white number of while marbles initially in the jar */ public MarbleJar(int black, int white) { this.numBlack = black; this.numWhite = white; } /** * Simulates drawing (and removing) a random marble from the jar. * @return the color of the drawn marble ("BLACK" or "WHITE") */ public String drawMarble() { if (this.isEmpty()) { return "JAR IS EMPTY"; } else { Die d = new Die(this.numBlack + this.numWhite); if (d.roll() <= this.numBlack) { this.numBlack--; return "BLACK"; } else { this.numWhite--; return "WHITE"; } } } /** * Determines if the jar is empty. * @return true if no marbles in the jar; else false */ public boolean isEmpty() { return (this.numBlack + this.numWhite == 0); } /** * Adds a marble to the jar. * @param color the color of the added marble ("BLACK" or "WHITE") */ public void addMarble(String color) { if (color.equals("BLACK")) { this.numBlack++; } else if (color.equals("WHITE")) { this.numWhite++; } } }