5 public class DeckOfCards 6 { 7 8 private int currentCard; // index of next Car
ID: 3822215 • Letter: 5
Question
5 public class DeckOfCards 6 { 7 8 private int currentCard; // index of next Card to be dealt (0-51) 9 private static final int NUMBER_OF_CARDS = 52; // constant # of Cards 10 // random number generator 11 private static final SecureRandom randomNumbers = new SecureRandom(); 12 13 // constructor fills deck of Cards 14 public DeckOfCards() 15 { 16 17 18 19 20 21 currentCard = 0; // first Card dealt will be deck[0] 22 23 24 25 26 27 } 28 29 // shuffle deck of Cards with one-pass algorithm 30 public void shuffle() 31 { 32 // next call to method dealCard should start at deck[0] again 33 currentCard = 0; 34 35 // for each Card, pick another random Card (0-51) and swap them 36 for (int first = 0; first < deck.length; first++) 37 { 38 // select a random number between 0 and 51 39 int second = randomNumbers.nextInt(NUMBER_OF_CARDS); 40 41 // swap current Card with randomly selected Card 42 43 44 45 } 46 } 47 48 // deal one Card 49 public Card dealCard() 50 { 51 // determine whether Cards remain to be dealt 52 if () 53 return deck[currentCard++]; // return current Card in array 54 else 55 return null; // return null to indicate that all Cards were dealt 56 } 57 } // end class DeckOfCards Fig. 7.10 |
Then modify class DeckOfCards of fig 7.10 to include methods that determine whether a hand contains
a pair
two pairs
three of a kind (e.g three jacks)
four of a kind (e.g four aces)
a flush (i-e all five cards of the same suite)
a straight (i-e, five cards of consecutive face values)
a full house (i-e, two cards of one face value & three cards of another face value)
[Hint: Add methods getFace and getSuit to class Card of Fig. 7.9.]
Explanation / Answer
public class DeckOfCards
{
deck = new Card[ NUMBER_OF_CARDS ]; //creating an array for the deck
private int currentCard; //next card index
private static final int NUMBER_OF_CARDS = 52; // generates random cards
private static final Random randomNumbers = new Random();
public DeckOfCards()
{
String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
currentCard = 0; //current card sets so that it dealt
for ( int count = 0; count < deck.length; count++ )
deck[ count ] = new Card( faces[ count % 13 ], suits[ count / 13 ] );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.