I need help with this!! (Card Shuffling and Dealing). Modify the application of
ID: 3551569 • Letter: I
Question
I need help with this!!
(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
Explanation / Answer
the code for dealing the cards.
import java.util.Random;
public class DeckOfCards {
private Card[] deck;
private int currentCard;
private static final int NUMBER_OF_CARDS = 52; // constant # of Cards
// random number generator
private static final Random randomNumbers = new Random();
// 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];
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;
}
}//end shuffle method
public Card dealCard(){
if(currentCard < deck.length)
return deck[currentCard++];
else
return null;
}
//public int toInt(String )
//return number of pairs found
public int findPair(Card[] arr){
int count=0;
for(int i=0; i<arr.length; i++){
for(int j=i+1; j<arr.length; j++){
//System.out.println(arr[i].getFace() + "... " + arr[j].getFace() );
if(arr[i].getFace().equalsIgnoreCase(arr[j].getFace())){
count++;
//System.out.println("Pair num="+count);
}
}
}
return count;
}
public boolean flush(Card[] arr){
String suitValue = arr[0].getSuit();
for(int i=0; i<arr.length; i++){
if(!(arr[i].getSuit().equalsIgnoreCase(suitValue))){
return false;
}
}
return true;
}
/*public boolean straight(Card[] arr){
}*/
public boolean royalFlush(Card[] arr){
//first all cards should be of same suit
if(flush(arr)){
//then check for face values
return true;
}
else
return false; //because suit is not same
}
}//end of class