Class MarbleJar
java.lang.Object
|
+--MarbleJar
- public class MarbleJar
- extends java.lang.Object
This class simulates a marble jar and the actions of Adding and
Drawing marbles from the jar. The simulation supports two colors
(white and black).
Adapted from the C++ class written by Dave Reed
18 Sept 1998
Modified Jan 1999
Modified Jan 2001
Modified April 2002
- Author:
- Craig Miller
Constructor Summary |
MarbleJar(int black,
int white)
This constructs the marble jar initializing it with the number of
colors specified by the parameters. |
Method Summary |
void |
addMarble(java.lang.String marble)
This method simulates adding a new marble to the jar. |
java.lang.String |
drawMarble()
This method simulates drawing one marble. |
boolean |
equals(java.lang.Object obj)
This tests if a second jar has the same contents. |
boolean |
isEmpty()
This tests if the jar is empty. |
static void |
main(java.lang.String[] args)
|
Methods inherited from class java.lang.Object |
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MarbleJar
public MarbleJar(int black,
int white)
- This constructs the marble jar initializing it with the number of
colors specified by the parameters.
- Parameters:
black
- The number of black marbles.white
- The number of white marbles.
drawMarble
public java.lang.String drawMarble()
- This method simulates drawing one marble.
Precondition: the marble jar is not empty.
Postcondition: the marble jar has one less marble.
- Returns:
- The string specifying color of drawn marble
("white" or "black")
- Throws:
IllegalStateException
- Indicates that the jar is empty.
addMarble
public void addMarble(java.lang.String marble)
- This method simulates adding a new marble to the jar.
- Parameters:
marble
- This string specifies the color of the marble added
to the jar. It must be either "black" or "white".- Throws:
java.lang.IllegalArgumentException
- Indicates an attempt to add a marble other than "white" or "black".
isEmpty
public boolean isEmpty()
- This tests if the jar is empty.
- Returns:
- True if the jar is empty, false otherwise.
equals
public boolean equals(java.lang.Object obj)
- This tests if a second jar has the same contents.
- Overrides:
equals
in class java.lang.Object
- Returns:
- true if the given object is a marble jar and has the
same number of black marbles and the same number of white
marbles as this jar, false otherwise.
main
public static void main(java.lang.String[] args)