tags:

views:

1528

answers:

4

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.

+1  A: 

The exercise is simple enough if you know some basics:

  • A deck of cards has 52 cards (54 if counting Jokers)
  • A deck contains 4 suits - Diamonds, Hearts, Clubs and Spades
  • Each Suit contains number cards (2-10), a Jack, a Queen, a King and an Ace
  • Ace's generally count to be "1" (but lots of edge cases for specific games)

So, some simple tests:

assert(deck.count == 52); assert(deck.suits.count == 4); assert(deck.suits.contains("Diamonds")); assert(deck.suits.contains("Hearts")); assert(deck.suits.contains("Clubs")); assert(deck.suits.contains("Spades")); assert(deck.suits["Diamonds"].contains("Ace")); //repeat for 2-10, Jack, Queen, King

or something like that.

Cory Foy
A: 

Did you look at the Wikipedia article on playing cards? That should include everything you need to know about cards to do the exercise. Basically a card has a number from 1 to 13 (the rank) and one of four suits, spades, clubs, hearts, or diamonds (but you can call them A,B,C,D if you want). 13x4 gives 52 cards in a deck.

David Zaslavsky
+2  A: 

Based on the description above, it doesn't really seem to require knowing how to play cards, just knowing how they look. As you mentioned above, this means that the Card class will have 2 properties (defined by Enums) rank and suit, where rank is one of:

{ ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king }

And suit is one of:

{ spades, hearts, diamonds, and clubs }

A Deck would just be a collection structure holding every combination of rank and suite.

Abdullah Jibaly
+4  A: 

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.

Zach Scrivena
Thanks. Whoever invented this game should've known better than to make spades look like hearts :p
Click Upvote
Thanks, I did it with your help! :p
Click Upvote