For this lab, you will be given a Card class, and you are to make a new Deck class that will hold up to 52 Card objects and manage them.
We would like to be able to write Java programs that play card games. As with any program, however, we must first be able to store the data needed for cards and, likely enough, whole decks cards. Below, I will describe the interface for the Card class, which will be given to you, and then describe the interface for the Deck classs, which you must create. I plan to use your Deck class with code of mine, and if you've written your classes to do what's required below, it will work!
Below is a set of public methods that each Card object has. Note that the details of how each card works internally (i.e. it's private members) is unknown to you. I will be giving you the already-compiled Card.class file, and not the Card.java file, so this is your only documentation on how Card objects work.
Each card has three properties: its suit (either "Heart", "Spade", "Club", or "Diamond"); its rank (a character corresponding to one of 'A', '2', '3', ..., 'Q', 'K'), and its value. The value is an integer that can be set and reset, and is used to establish the card ordering rules. For example, in some games an Ace is low (with value 1), and in other games its value is higher than all other cards (presumably 14). Similarly, in some games the face cards all count 10, and in others they have individual values. A card's suit and rank cannot change after construction, but its value can. In other words, the value is mutable, but the suit and rank are immutable.
Here is the public interface for a Card object:
public Card (String suit, char rank);
Constructs
a card with the given suit and rank (you can use any of 'A',
'a', 'J', 'j', 'Q', 'q', 'K', 'k', and numeric ranks such as
'2' and '5'. The ten is specified as 'T' or 't'). This
constructor will set default card values as follows: Ace=1,
numeric ranks with matching value, Jack=11, Queen=12, and
King=13. If an invalid value is passed to the constructor, it
throws an InvalidCardException object.
public Card (String suit, char rank, int
value);
Constructs a card with the given suit, the
given rank, and the given value. You can use this if you
want to set a card with something different from the default
value. If an invalid value is passed to the constructor, it
throws an InvalidCardException object.
public String getSuit ();
Returns the suit for this card.
public char getRank ();
Returns the rank of the card.
public int getValue ();
Returns the current value of the card.
public boolean hasHigherValue (Card other);
Returns true if this card has higher value than the
other card (which is passed as a parameter); otherwise returns
false.
public boolean hasSameValue (Card other);
Returns true if this card has the same value as the
other card; otherwise returns false.
public boolean hasSameSuit (Card other);
Returns true if this card has the same suit as the
other card; otherwise returns false.
public boolean isAce ();
Return true if this card is an ace; otherwise returns
false.
public boolean isFace ();
Returns true if this card is one of the face cards
(Jack, Queen, or King); oterhwise returns false.
public boolean isNumeric ();
Returns true if this card has a numeric rank (between
2 and 10); otherwise returns false.
public void setValue (int value);
Changes the value of this card to be the specified value.
public String toString ();
Returns a String identifying the card, like: "3 of Spades".
You must implement the Deck class yourself. Below is a set of public methods that each Deck object must have. Keep in mind that a Deck is really a container for Card objects; it is the responsibility of Deck to organize and keep track of some set of Card objects.
Each Deck contains from 0 to 52 cards. Initially, it may be empty or full. It needs to be able to shuffle itself; it also needs to allow cards to be drawn from it, and put into it. Below is the list of methods that your each Deck object must have to support these operations.
public Deck (boolean full);
Construct a new deck that can hold from 0 to 52 cards. If the
full parameter is true, then the deck should
initially contain all 52 cards, in order, where the order is
from Ace to King in each suit, and the suits are from club, to
diamond, to heart, to spade. If the full parameter
is false, the deck should contain no cards.
public Card drawTopCard ();
Draw the top card from the deck. That is, once it is returned
to the caller, the deck no longer contains that card.
public Card showTopCard ();
Show the top card of the deck. That is, return the card to
the caller, but keep the card in the deck as well.
public boolean insertCard (Card newCard);
If there is space left in the deck, insert the card provided
by the caller, and return true to indicate success.
If there is not space to hold another card in this deck,
return false to indicate failure.
public boolean isEmpty ();
Return true if this deck contains no cards;
false otherwise.
public boolean isFull ();
Return true if this deck contains 52 cards;
false otherwise.
public int getNumberCards ();
Return the number of cards currently in this deck.
public void shuffle ();
Shuffle the cards in this deck.
First, you need to obtain the classes Card and InvalidCardException, as those will be needed to construct your Deck class. You can get these files from my public directory on romulus, like so:
cp ~sfkaplan/public/cs12/Card.class .
cp ~sfkaplan/public/cs12/InvalidCardException.class .
Notice that these are pre-compiled classes. You cannot change them. Opening these files will yield what appears to be junk. You must work with the Card class specification given above.
You need to write the Deck class. Then, you must test your class to be sure that it works properly! How you test the basic functionality is up to you. I have a suite of tests that I will run on your code, so be sure that all methods work correctly.
You must also write a class called PokerTest, whose main() method performs heavily tests your Deck.shuffle() method. Specifically, it must do the following 10,000 times:
At the end of this test, the program should report the total number of each type of hand. I will post statistical expectations that would indicate that your Deck.shuffle() method is operating correctly.
Once you have debugged your code, submit your Deck.java and PokerTest.java files. Remember that I will also use your Deck class with code of my own to evaluate it -- so be sure that all of the methods listed above work as documented!
You must use the cs12-submit command to turn in your Java code. Submit this assignment as lab-3 when you use the cs12-submit command, like so:
cs12-submit lab-3 Deck.java PokerTest.java