CS 12 -- Lab 3

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.


Cards and Decks

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!


The Card class

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:


The Deck class

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.


Your assignment

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:

  1. Create a new Deck object and shuffle it.
  2. Draw 10 five-card hands.
  3. For each of those hands, test how many contain four of a kind, a full house, a flush, and a royal flush.

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.


What you must submit

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

This assignment is due on Monday, March 1st, at 11:59 pm.

Scott F. Kaplan
Last modified: Sun Nov 5 22:48:52 EST 2000