Back to 111 Main Page

Mobius strip

Assignment 17: Part 2 (Deck)

Task

Now that you have a PlayingCard, you can model a deck of playing cards.

Steps

Create a class named Deck. It should contain the following public methods. Again, the (reusable) documentation explains what each should do.

/**
 * Constructs a new deck of 52 standard playing cards with no jokers.
 * If the parameter shuffled == true, the new deck will be shuffled.
 * Otherwise, it will be in sorted order.
 */
public Deck(boolean shuffled)

/**
 * Constructs a deck of 52 shuffled playing cards.
 */
public Deck()

/**
 * Removes the top card from this deck and returns it.
 * If this deck is empty, will return null instead.
 */
public PlayingCard draw()

/**
 * Returns the number of cards currently remaining in this deck.
 */
public int getSize()

/**
 * Shuffles the cards remaining in this deck.
 */
public void shuffle()

Make sure your method signatures match these exactly. Any instance variables you have should be private. (All you probably need is an ArrayList<PlayingCard>.)

When constructing a Deck, you need to add cards to it in some sort of order so that you know you added one of each of the 52 cards. The particular order is up to you--all the Aces, then all the 2s, etc; or all of one suit and then all of the next suit.

The first Deck constructor allows the user of your code to ask for a deck either with or without shuffling. Most of the time, they'll probably want it shuffled though, so the second constructor makes the deck a little easier to use. [Hint: Writing the second constructor is very simple if you remember how to call one constructor from another.]

For the draw() method, we don't want the Deck to crash if some programmer accidentally tries to draw a card from an empty deck. There's not much we can do in this situation though--either document what exception the method will throw in this situation, or else return null. For this implementation, I chose the latter approach.

For the shuffle() method, check out the Fisher-Yates algorithm for a relatively easy, but efficient and effective shuffling method. (While this page does include some sample Java code, it uses arrays--which we haven't covered yet. Instead, look at the examples that trace through the algorithm step-by-step and see if you can figure out how to do that with an ArrayList.)

Again, you can write additional methods if you desire--such as a toString() method--but only those listed above are required.

Sample Output

Depends on how much you want to test your Deck at this point.

What to Submit

Hang onto this file for now--you're not going to submit it until after you finish A17-Part 3.

Grading

See A17-Part 3.

FAQs

So, although we can create Joker playing cards, there aren't actually going to be any in the Deck?
That is correct. Mostly, the Joker was a nice clean way to handle invalid arguments to the PlayingCard constructor (since we haven't learned how to throw exceptions yet, only catch them). It is true that, for certain games, someone might actually want Jokers included in a new deck. So we could provide a third constructor: Deck(boolean shuffled, int jokers), which would allow someone to specify exactly how many jokers they want added (since sometimes you only want one, not two). But this extra code is not really necessary at this point.
According to that Wikipedia page, Java is probably only going to be able to provide a subset of all possible deck permutations. Is this okay?
Yeah, that's fine. If you're really worried, I suppose you could have your shuffle method actually shuffle the deck multiple times, but it's not required.
How do I know if my Deck was constructed correctly?
Probably the easiest way is to print out the contents of the ArrayList that contains all the Deck's cards at the end of the constructor. This will verify that the constructor did its job. So, if your ArrayList instance variable is named cards, you can (temporarily) add this line as the last line of the Deck constructor:
  System.out.println(cards);
Then write a main method somewhere that constructs a new Deck object.

(As with printing any object, this println statement actually invokes the ArrayList's toString() method, which in turn will invoke your PlayingCard's toString() method to print each card in the list.)

On to A17, Part 3 →


~ztomasze Index : TA Details: ICS111: A17-2
http://www2.hawaii.edu/~ztomasze
Last Edited: 01 Apr 2009
©2009 by Z. Tomaszewski.