Search code examples
javaoop

Stuck on Card/Deck exercise from Java official tutorial


I'm stuck on a portion of the Java tutorial, specifically this exercise. The exercise asks you to:

1- Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Be sure to keep your solution as you will be asked to rewrite it in Enum Types.

Hint: You can use the assert statement to check your assignments. You write:

assert(boolean expression to test);  

If the boolean expression is false, you will get an error message. For example,

assert toString(ACE) == "Ace"; 

should return true, so there will be no error message.

2- Write a class whose instances represent a full deck of cards. You should also keep this solution.

3- Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and displaying its cards.

I'd really like to do this exercise, but the problem is I have never played cards so I have no idea at all how to create this program and what properties the cards should have, etc. I looked this up on wikipedia but got very limited knowledge, which will never enable me to build the required classes: Card.java , Deck.java , and the program DisplayDeck.java.

What will be a good alternate exercise to do for someone with no knowledge of cards, but which will test the same concepts that the above mentioned exercise would? (Probably static and instance variables and such)

Thanks.

By the way, this not a homework question, i'm learning Java for a commercial project.


Solution

  • I think it's worth the time to be familiar with the example, since it's frequently used to describe programming concepts. Let me try to distill the description of playing cards for you:

    First, keep this picture of the deck (the whole collection of cards) open for reference.

    Going vertically down the rows, you have {Spades ♠, Diamonds ◇, Clubs ♣, Hearts ♡} = 4 suits.

    Going horizontally across the columns, you have {2, 3, ..., 10, Jack, Queen, King, Ace} = 13 ranks.

    Altogether, there are 4 x 13 = 52 cards in the deck.

    Each card is identified by the pair (rank, suit), e.g. (Ace, Spades ♠) and (10, Diamonds ◇) which we read as "Ace of Spades" and "Ten of Diamonds", respectively.