49
Exercises
225
numbers have been entered, print out each of the six statistics that are available from
calc.
3. This problem uses the PairOfDice class from Exercise 5.1 and the StatCalc class from
(solution)
Exercise 5.2.
The program in Exercise 4.4 performs the experiment of counting how many times a
pair of dice is rolled before a given total comes up. It repeats this experiment 10000 times
and then reports the average number of rolls. It does this whole process for each possible
total (2, 3, ..., 12).
Redo that exercise. But instead of just reporting the average number of rolls, you
should also report the standard deviation and the maximum number of rolls. Use a
PairOfDice object to represent the dice. Use a StatCalc object to compute the statistics.
(You’ll need a new StatCalc object for each possible total, 2, 3, ..., 12. You can use a
new pair of dice if you want, but it’s not necessary.)
4. The BlackjackHand class fromSubsection5.5.1 is an extension of the Hand class fromSec-
(solution)
tion 5.4.TheinstancemethodsintheHandclassarediscussedinthatsection.Inaddition
to those methods, BlackjackHand includes an instance method, getBlackjackValue(),
that returns the value of the hand for the game of Blackjack. For this exercise, you will
also need the Deck and Card classes fromSection5.4.
ABlackjack hand typically contains from two to six cards. Write a program to test the
BlackjackHand class. You should create a BlackjackHand object and a Deck object. Pick
arandom number between 2 and 6. Deal that many cards from the deck and add them to
the hand. Print out all the cards in the hand, and then print out the value computed for
the hand by getBlackjackValue(). Repeat this as long as the user wants to continue.
In addition to TextIO.java, your program will depend on Card.java, , Deck.java,
Hand.java,and BlackjackHand.java.
5. Write a program that lets the user play Blackjack. The game will be a simplified version
(solution)
of Blackjack as it is played in a casino. The computer will act as the dealer. As in
the previous exercise, your program will need the classes defined inCard.java,Deck.java,
Hand.java,and BlackjackHand.java. (Thisisthelongestandmostcomplexprogramthat
has come up so far in the exercises.)
You should first write a subroutine in which the user plays one game. The subroutine
should return a boolean value to indicate whether the user wins the game or not. Return
true if the user wins, false if the dealer wins. The program needs an object of class
Deck and two objects of type BlackjackHand, one for the dealer and one for the user.
The general object in Blackjack is to get a hand of cards whose value is as close to 21 as
possible, without going over. The game goes like this.
•First, two cards are dealt into each player’s hand. If the dealer’s hand has a value of
21 at this point, then the dealer wins. Otherwise, if the user has 21, then the user
wins. (This is called a “Blackjack”.) Note that the dealer wins on a tie, so if both
players have Blackjack, then the dealer wins.
•Now, if the game has not ended, the user gets a chance to add some cards to her
hand. In this phase, the user sees her own cards and sees one of the dealer’s two
cards. (In a casino, the dealer deals himself one card face up and one card face down.
All the user’s cards are dealt face up.) The user makes a decision whether to “Hit”,