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)
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, Sets, or any other built-in devices. This means if you have to import anything other than the random number generator, it is not allowed.
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 } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started