Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to modify DeckOfCardsTest.java to deal a five-card poker hand. Then modif

ID: 3700719 • Letter: I

Question

I need to modify DeckOfCardsTest.java to deal a five-card poker hand. Then modify class DeckOfCards to include methods that determine whether a hand contains:

a) a pair

b) two pairs

c) three of a kind

d) four of a kind

e) a flush (all card of the same suit)

f) a straight

g) a full house

This will include adding methods to the Card class for getFace and getSuit. I am not allowed to use ArrayList or any other built-in devices.

Please do not copy the incorrect answers from other similar questions.

I believe that the "super" statement can be used here, but I have not figured out how.

The code for the three files is as follows:

----------------------------------------------------------------------------------------------

DeckoOfCardsTest.java

public class DeckOfCardsTest {
// execute application
public static void main(String[] args) {
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order
  
// print the hand dealt
for (int i = 1; i <= 5; i++) {
// deal and display a Card
System.out.printf("%-19s%n", myDeckOfCards.dealCard());

if (i % 5 == 0) { // output a newline after every fifth card, incase dealing 2 hands in the future
System.out.println();
}
}//end of for loop
} //end of main
}//end of class

------------------------------------------------------------------------------------

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;   
}
}

------------------------------------------------------------------------------------------------------

DeckOfCards.java

import java.security.SecureRandom;

public class DeckOfCards {
// random number generator
private static final SecureRandom randomNumbers = new SecureRandom();
private static final int NUMBER_OF_CARDS = 52; // constant # of Cards

private Card[] deck = new Card[NUMBER_OF_CARDS]; // Card references
private int currentCard = 0; // index of next Card to be dealt (0-51)

// 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"};   

// 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
}
}
}

Explanation / Answer

// UPDATED dECKoFcARDS with required functions . Please check and try to code the remaining few .

// you will learn while you code :)

//isFullHouse , isFlush , isFourOfAKind , isThreeOfAKind coded with helping function isPair & dealNewHand

package solution;

import java.security.SecureRandom;

import java.util.HashSet;

import java.util.Set;

public class DeckOfCards {

// random number generator

private static final SecureRandom randomNumbers = new SecureRandom();

private static final int NUMBER_OF_CARDS = 52; // constant # of Cards

private static final int HAND_SIZE = 5; // constant # of Cards

private Card[] deck = new Card[NUMBER_OF_CARDS]; // Card references

private int currentCard = 0; // index of next Card to be dealt (0-51)

private Card[] hand = new Card[HAND_SIZE];

// 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" };

// populate deck with Card objects

for (int count = 0; count < deck.length; count++) {

deck[count] = new Card(faces[count % 13], suits[count / 13]);

}

}

/**

* creates a new Hand object and deals HAND_SIZE cards to it

*/

public void dealNewHand() {

for (int i = 0; i < HAND_SIZE; i++)

hand[i] = dealCard();

}

/**

* Check and evaluates the hand.

*

* @return a String representing the status of the hand

*/

public boolean isPair() {

for(int kind=0; kind<deck.length; kind++) {

int kindCount = 0;

for (int i = 0; i < 5; i++){

if (hand[i].getFace().equals(""+kind)) { kindCount++; }

}

if (kindCount==2) { return true; }

}

return false;

}

/**

* isFullHouse

* @return

*/

public boolean isFullHouse() {

if (isThreeOfAKind() == true && isPair() == true)

return true;

return false;

}

public boolean isFlush() {

String temp;

for (int i = 0; i < 5; i++) {

temp = hand[i].getSuit();

for (int j = 0; j < 5; j++) {

if (i != j)

if (!(temp.equals(hand[j].getSuit())))

return false;

}

}

return true;

}

public boolean isFourOfAKind() {

Set<String> s = new HashSet<String>();

for (int i = 0; i < 5; i++) {

s.add(hand[i].getFace());

}

if (s.size() == 2)

return true;

return false;

}

public boolean isThreeOfAKind() {

Set<String> s = new HashSet<String>();

for (int i = 0; i < 5; i++) {

s.add(hand[i].getFace());

}

if (s.size() == 3)

return true;

return false;

}

// 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

}

}

}