Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the De
ID: 3688171 • Letter: G
Question
Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding.
Hint: Use a test class to test above classes.
Pulic 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
==========================================================================================================
import java.security.SecureRandom;
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
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
====================================================================================
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
Explanation / Answer
public class Blackjack { public static void main(String[] args) { int dollars; // dollars the user has. int betd; // Amount user bets on a game. boolean Winner; // Did the user win the game? TextIO.putln("Welcome to play BlackJack."); TextIO.putln(); dollars = 100; // User starts with $100. while (true) { TextIO.putln("You have " + dollars + " dollars."); do { TextIO.putln("Enter your bet amount or 0 to end the game"); TextIO.put("? "); betd = TextIO.getlnInt(); if (betd < 0 || betd > dollars) TextIO.putln("Your bet amount must be between 0 and " + dollars + '.'); } while (betd < 0 || betd > dollars); if (betd == 0) break; Winner = playBlackjackgame(); if (Winner) dollars = betd + dollars; else dollars = dollars - betd; TextIO.putln(); if (dollars == 0) { TextIO.putln("Time to end the game, you are out of dollars!"); break; } } TextIO.putln(); TextIO.putln("You are lucky to hold to $" + dollars + '.'); } // end main() static boolean playBlackjackgame() { // Let the user play one game of Blackjack. // Return true if the user wins, false if the user loses. Deck deck; // A deck of cards. A new deck for each game. BlackjackHand dealHand; // The dealer's hand. BlackjackHand usersHand; // The user's hand. deck = new Deck(); dealHand = new BlackjackHand(); usersHand = new BlackjackHand(); /* Shuffle the deck, then deal two cards to each player. */ deck.shuffle(); dealHand.addCard( deck.dealCard() ); dealHand.addCard( deck.dealCard() ); usersHand.addCard( deck.dealCard() ); usersHand.addCard( deck.dealCard() ); TextIO.putln(); TextIO.putln(); /* checking if a players has Blackjack . The player who has a black jack in hand. Dealer wins when ties. */ if (dealHand.getBlackjackValue() == 21) { TextIO.putln("Dealer has the " + dealHand.getCard(0) + " and the " + dealHand.getCard(1) + "."); TextIO.putln("User has the " + usersHand.getCard(0) + " and the " + usersHand.getCard(1) + "."); TextIO.putln(); TextIO.putln("omg dealer has black jack. Dealer wins."); return false; } if (usersHand.getBlackjackValue() == 21) { TextIO.putln("Dealer has the " + dealHand.getCard(0) + " and the " + dealHand.getCard(1) + "."); TextIO.putln("User has the " + usersHand.getCard(0) + " and the " + usersHand.getCard(1) + "."); TextIO.putln(); TextIO.putln("wow! See you won with black jack in hand."); return true; } /* users drawing card, hits stand */ while (true) { /* Display user's cards, and let user decide to Hit or Stand. */ TextIO.putln(); TextIO.putln(); TextIO.putln("Your cards are:"); for ( int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.