Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Hint: Use a test class to test above classes. Include a

Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game.

Hint:

Use a test class to test above classes. Include a UML based problem solving process in your submission.

1. Card.java

public class Card {

private final String face; // face of card ("Ace", "Deuce", ...) private final String suit; // suit of card ("Hearts", "Diamonds", ...)

// two-argument constructor initializes card's face and suit public Card(String cardFace, String cardSuit) { this.face = cardFace; // initialize face of card this.suit = cardSuit; // initialize suit of card } // return String representation of Card

public String toString() { return face + " of " + suit; }

} // end class Card

2. DeckOfCards.java

import java.security.SecureRandom;

public class DeckOfCards {

private Card[] deck; // array of Card objects private int currentCard; // index of next Card to be dealt (0-51) private static final int NUMBER_OF_CARDS = 52; // constant # of Cards // random number generator private static final SecureRandom randomNumbers = new SecureRandom();

// constructor fills deck of Cards public DeckOfCards() {

String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects currentCard = 0; // first Card dealt will be deck[0] // populate deck with Card objects for (int count = 0; count < deck.length; count++) { deck[count] = new Card(faces[count % 13], suits[count / 13]); }

}

// shuffle deck of Cards with one-pass algorithm public void shuffle() { // next call to method dealCard should start at deck[0] again currentCard = 0;

// for each Card, pick another random Card (0-51) and swap them for (int first = 0; first < deck.length; first++) { // select a random number between 0 and 51 int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

// swap current Card with randomly selected Card Card temp = deck[first]; deck[first] = deck[second]; deck[second] = temp; } }

// deal one Card public Card dealCard() { // determine whether Cards remain to be dealt if (currentCard < deck.length) { return deck[currentCard++]; // return current Card in array } else { return null; // return null to indicate that all Cards were dealt } } } // end class DeckOfCards

3. DeckOfCardsTest.java

// Fig. 7.11: DeckOfCardsTest.java // Card shuffling and dealing. public class DeckOfCardsTest { // execute application

public static void main(String[] args) { DeckOfCards myDeckOfCards = new DeckOfCards(); myDeckOfCards.shuffle(); // place Cards in random order

// print all 52 Cards in the order in which they are dealt for (int i = 1; i <= 52; i++) { // deal and display a Card System.out.printf("%-19s", myDeckOfCards.dealCard());

if (i % 4 == 0) // output a newline after every fourth card { System.out.println(); } } } } // end class DeckOfCardsTest

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Describe and compare approaches to managing an organization

Answered: 1 week ago

Question

3. Evaluate a Web-based training site.

Answered: 1 week ago