Assignment #4: Classes
From our design work in class we decided that the poker program
should have three classes - one to represent a card, one to represent
a hand of cards, and one to represent a deck of cards. For the
assignment you need
to complete the Card class, the Hand class, and the Deck class
and write a driver program to play a game between the dealer and a person.
In developing your classes it would be wise to have a test program
that tests each method as you write a class.
Card class: This class represents a playing card.
A card has two attributes -
a suit and a rank. Hence, the Card class should have two instance
variables:
- int suit - Represents the suit. (for example,
1 represents Spades, 2 represents Hearts,
3 represents Diamond, and 4 represents Clubs)
- int rank - Represents the rank. (numbers 2 - 10
represent the number, 11 represents Jack, 12 represents
Queen, 13 represents King, and 14 represents Ace)
Provide the following public methods:
- Card(int rank, int suit) -- a constructor that initializes the instance
variables to the specified values
- int getSuit() -- Returns the suit.
- int getRank() -- Returns an integer representing the card's rank (2 - 14).
- String toString() -- Returns a string representing the card e.g.,
"3♥" or "K♠". You can use the
unicode characters for card suits by using string escape sequences.
(♠ = "\u2660", ♣ = "\u2663", ♥ = "\u2665", ♦ = "\u2666")
Hand class: This class represents a Poker hand. It should have
the following instance data:
- Card[] cards - Represents the cards in the hand.
Some of the methods the Hand class needs (add others if needed):
- Hand() -- A constructor that initializes the Card array to be of size 5.
- void setCard(int index, Card card) -- Sets the card at the specified index
in the hand to the specified card.
- int getRank() -- Returns the rank of the hand where four of a kind is 6
and nothing is 0.
- int getSum() -- Returns the sum of the cards in the hand.
- int compareTo(Hand hand) -- Compares this hand to the specified hand and returns
1 if this hand is better, -1 if the specified hand is better, and 0 if
the two hands are the same.
- String toString() -- Returns a string representing the hand.
The following private methods will make the getRank() method easier to write:
- int[] getHistogram() -- Computes a histogram of the cards that are in the hand.
- boolean isFourOfAKind(int[] hist) -- Returns whether there are four cards of
same rank given a histogram of the hand.
- boolean isFullHouse(int[] hist)
- boolean isFlush()
- boolean isThreeOfAKind(int[] hist)
- boolean isTwoPairs(int[] hist)
- boolean isOnePair(int[] hist)
Deck class: This class represents a deck of playing cards. It
should have the following instance data:
- Card[] cards - Represents the cards in the deck.
The methods the class should provide include:
- Deck() -- A constructor that initializes and fills the card array to contain
one of every card.
- void shuffle() -- A method that shuffles the order of the cards in the deck.
This can be accomplished by swapping every card in the deck with a another
card in the deck that is randomly selected.
- void swap(int index1, int index2) -- A private method that swaps the cards located
at the specified indices. (useful for shuffling)
- Card getTopCard() -- A method that removes the card that is on the top
of the deck and returns it.