[ Exercises | Chapter Index | Main Index ]

## Solution for Programmming Exercise 5.4

This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.

### Exercise 5.4:

The BlackjackHand class from Subsection 5.5.1 is an extension of the Hand class from Section 5.4. The instance methods in the Hand class are discussed in that section. In addition 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 from Section 5.4.

A Blackjack 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 a random 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.

Discussion

This problem is mostly a warm-up for the next one. It uses objects of three different types, Card, Deck, and BlackjackHand. The Hand class is used indirectly, as the superclass of BlackjackHand. To use these objects, you need to know what methods are available in each class, so you should review the information that you have about the classes before beginning the program.

An algorithm for the program is

    Create a deck
    repeat while user wants to continue:
        Shuffle the deck
        Create a new BlackjackHand
        Decide the number of cards in the hand
        Deal cards from the deck into the hand, and print them out
        Display the value of the hand

Some variation is possible. You could use just one BlackjackHand object, and remove all the cards from it between hands. The Hand class includes an instance method, clear(), that could be used for this purpose. Similarly, you could create a new Deck object each time through the loop. Or, you might want to use one deck and shuffle it only when the number of cards in the deck gets too small. You could say:

    if (deck.cardsLeft() < 6)
          deck.shuffle();

Since we always want to do at least one hand, we can use a do..while statement for the loop. Putting in some variable names, we can refine the algorithm to

    deck  =  new Deck();
    do:
        deck.shuffle();
        hand  =  new BlackjackHand();
        cardsInHand = a random number between 2 and 6
        Deal cards from deck into hand, and print them out.
        Display hand.getBlackjackValue()
        Ask if use wants to go again
    while user wants to go again

The number of cards in the hand is supposed to be a random number between 2 and 6. There are five possible values. The expression "(int)(Math.random()*5)" has one of the 5 possible values 0, 1, 2, 3, or 4. Adding 2 to the result gives one of the values 2, 3, 4, 5, or 6. So, the number of cards can be computed as "2 + (int)(Math.random()*5)".

Once we know the number of cards, we can use a for loop to deal cards into the hand, one at a time. The function call deck.dealCard() gets a card from the deck. Once we have a card, we can add it to the hand with the subroutine call hand.addCard(card). This allows us to refine the algorithm to

    deck  =  new Deck();
    do:
        deck.shuffle();
        hand  =  new BlackjackHand();
        cardsInHand = 2 + (int)(Math.random()*5)
        for i = 0 to cardsInHand:
            card  =  deck.dealCard()
            hand.addCard(card)
            Display the card
        Display hand.getBlackjackValue()
        Ask if use wants to go again
    while user wants to go again

This algorithm can be translated pretty directly into the main() routine of the program, which is shown below.

The Solution

    /**
     * Creates random blackjack hands, with 2 to 6 cards,
     * and prints out the blackjack value of each hand.
     * The user decides when to stop.
     */

    public class TestBlackjackHand {

       public static void main(String[] args) {

          Deck deck;            // A deck of cards.
          Card card;            // A card dealt from the deck.
          BlackjackHand hand;   // A hand of from two to six cards.
          int cardsInHand;      // Number or cards in the hand.
          boolean again;        // Set to true if user wants to continue.

          deck = new Deck();    // Create the deck.

          do {
             deck.shuffle();
             hand = new BlackjackHand();
             cardsInHand = 2 + (int)(Math.random()*5);
             System.out.println();
             System.out.println();
             System.out.println("Hand contains:");
             for ( int i = 1; i <= cardsInHand; i++ ) {
                    // Get a card from the deck, print it out,
                    //   and add it to the hand.
                card = deck.dealCard();
                hand.addCard(card);
                System.out.println("    " + card);
             }
             System.out.println("Value of hand is " + hand.getBlackjackValue());
             System.out.println();
             System.out.print("Again? ");
             again = TextIO.getlnBoolean();
          } while (again == true);

       }

    }  // end class TestBlackjackHand

[ Exercises | Chapter Index | Main Index ]