1711 Laboratory 2:
The Craps class - using
logic in a class
- Write a simple class, test it, and incorporate it into a larger
- Learn to use predefined classes and static methods of
- Use logic in a class
- Design a class that contains objects
- Use loops in a class
- Increase your skill in writing Java programs and in defining
Hand-in Requirements (Deliverables):
All projects and laboratories will be submitted electronically through
Zip up your entire project directory to submit.
(Right click on the project folder and follow the SendTo link.)
- Dice.java (2 pts)
- Craps.java (Parts 1 and 2: 8 pts)
- Craps.java (Part 3: 4 pts)
- CrapsTester.java (6 pts)
Craps is a game played with two dice. There are many variations of craps,
but the variation that we are going to start with in this laboratory is
played as follows:
The player rolls the two dice.
If the sum of the resulting die values is 7 or 11, the player wins.
If the sum is 2, 3, or 12, the player loses.
If something else is rolled, the player has to roll again to determine
If the sum of the second roll is the same as what the player rolled
first time, the player wins. Otherwise the player loses.
(You might get a pair of dice and play a few rounds to try it.)
Start by creating a new project called lab02.
You will be using three classes, Dice, Craps and
All of these classes will be in the games package.
- Create an empty Dice class and copy the code from
- The Craps class has accessor methods that return the
of games played, the number of games won, and whether the
most recent game played was a win or a loss (true or
- The Craps class has a toString method.
- The Craps class has a modifier method called
that plays one game of craps. The play returns
if the game was won and false otherwise.
- As part of your implementation, write a private helper method called nextSum that rolls both dice
and returns the sum of their upward faces.
- Also keep the first sum rolled and the last sum rolled for the current game as attributes.
Include them as part of the toString. If the game did not require that the dice be rolled twice, set the
last roll to -1. Include accessor methods for these.
Part I: Developing and testing the Craps class
- Create a Craps class with a constructor but no main method
in the games package of laboratory 2.
- Create a CrapsTester class with a main method but
no constructor in the games package. Add a statement to output
"Laboratory 2 written by YOURNAME".
- Add the fields, constructor and toString
method of the Craps class. Build and run after each addition.
- Write client code (in the main method of CrapsTester)
to create a Craps object called crapsGame and print
out the object. Build and run.
- Add the play method for Craps.
Build and run.
- Ask crapsGame to play a game of craps and
output the crapsGame object.
Part II: Completing the Craps class (on your own)
- Fill in the remaining methods of Craps
and test each method by writing the appropriate client code.
- Ask crapsGame to play 4 more games of craps
and output the crapsGame object after each game.
Do not use a loop.
- Create a new Craps object called
crapsGame2. Play 5 games and keep track of the number won by
incrementing a counter in CrapsTester.
Use a loop in this part of the assignment.
Do not do any printing inside of the loop.
After the loop is done, print crapsGame2 and the value of the
counter from CrapsTester.
Make sure that the gamesWon in Craps agrees with the
counter in CrapTester.
Also print out the probability of winning, the ratio of the number of games
won to the number of games played. This should be a number between 0 and 1.
- When you are convinced that the above is working correctly, change the number
of games played from 5 to 10000 and run it again.
The probability of winning should be about .3.
Part III: Modifying the Craps class to play craps in the standard
In the standard craps game, you still win if you get a 7 or 11 on the first roll.
If your first roll is a 2, 3, or 12, you lose.
If your first roll is a 4, 5, 6, 8, 9, or 10,
you roll again until either you get a 7 (you lose) or you get the number
from your first roll.
- Write a method called playNew that will play by these rules.
The playNew method will be like play but
will now contain a loop.
Use a while loop, testing to see if the game is over after each
- Now modify Craptester. Create a new Craps object called
Use a loop to play 100000 games of craps using PlayNew and
count the number of wins and losses. Make sure that there is no output
generated by each iteration of the loop.
Run your program again, and then make sure that
the count for the number of games won agrees with the
counter in CrapTester.
Also output the probability of winning (the number of games won divided by the
number of games played).
- Note: You should create a variable to represent the number of games
to play (100000) and use this variable to control the loop and calculate the
winning probability. For testing, you should start this variable at a small
value, say 10.
You can try values larger than 100000 if you are running on a fast machine.
- Note: You should get a winning probability of slightly less than 1/2.
Think about what this means.
What would happen if the rules allowed you to roll
again if you got a 3 on the first roll?