Part 1 (Card Shuffling and Dealing). Modify the application of Fig. 7.11 in the
ID: 3652396 • Letter: P
Question
Part 1(Card Shuffling and Dealing). Modify the application of Fig. 7.11 in the Deitel text to deal a five-card poker hand. Then modify class DeckOfCards of Fig. 7.10 to include methods that determine whether a hand contains:
a) a pair
b) two pairs
c) three of a kind (e.g., three jacks)
d) four of a kind (e.g., four aces)
e) a flush (i.e., all five cards of the same suit)
f) a straight (i.e., five cards of consecutive face values)
g) a full house (i.e., two cards of one face value and three cards of another face value)
[Hint: Add methods getFace and getSuit to class Card of Fig. 7.9.]
Part 2
(Card Shuffling and Dealing). Use the methods developed in Part 1 to write an application that deals two five-card poker hands, evaluates each hand and determines which is better.
//Fig 7.9 Card.java
public class Card
{
private String face;
private String suit;
public Card( String cardFace, String cardSuit )
{
face = cardFace;
suit = cardSuit;
}
public String toString()
{
return face + " of " + suit;
}
}
//Fig 7.10 DeckOfCards.java
import java.util.Random;
public class DeckOfCards
{
private Card[] deck;
private int currentCard;
private static final int NUMBER_OF_CARDS = 52;
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" };
deck = new Card[ NUMBER_OF_CARDS ];
currentCard = 0;
for ( int count = 0; count < deck.length; count++ )
deck[ count ] =
new Card( faces[ count % 13 ], suits[ count / 13]);
}
public void shuffle()
{
currentCard = 0;
for ( int first = 0; first < deck.length; first++)
{
int second = randomNumbers.nextInt( NUMBER_OF_CARDS );
Card temp = deck[ first ];
deck[ first ] = deck [ second ];
deck[ second ] = temp;
}
}
public Card dealCard()
{
if ( currentCard < deck.length )
return deck[ currentCard++ ];
else
return null;
}
}
//Fig 7.11: DeckOfCardsTest.java
public class DeckOfCardsTest
{
public static void main( String[] args )
{
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle();
for ( int i = 1; i <+ 52; i++ )
{
System.out.printf( "%-19s", myDeckOfCards.dealCard() );
if ( i % 4 == 0 )
System.out.println();
}
}
}
Explanation / Answer
as