Assignment 16: Part 1 (PlayingCard)


Model a single playing card (to eventually be used in a larger program).

Concepts: Review of class-related concepts


For Assignment 16, you are going to write a game-like program that deals out cards. However, to avoid confusion about what code goes where, you're going to write this program one separate piece at a time. The first piece is just the playing card.

Create a class named PlayingCard.

In that class, define public static final int constants named JOKER, CLUBS, SPADES, HEARTS, and DIAMONDS. Assign each a different value; I recommend 0 to 4. (You may also want to define constants for ACE, JACK, QUEEN, and KING; but that is not required.)

You must then write the following public methods. I have already documented them for you, explaining what each should do. (You can reuse this documentation if you want to.)

 * Constructs a new card with the given face value and suit.
 * The given value must be between 1 (Ace) and 13 (King).
 * The suit must be one of the CLUBS, SPADES, HEARTS, or DIAMONDS constant values.
 * If either the value or the suit is out of range, both will be set to JOKER.
public PlayingCard(int value, int suit)

 * Returns the face value of this card: a value between 1 and 13, inclusive,
 * or else JOKER.
public int getValue()

 * Returns the suit of this card.
public int getSuit()

 * Returns a String of this card of the form "4 of Diamonds",
 * where "4" here is the value, and "Diamonds" is the suit.
 * Aces and face card (Jack, Queen, King) names are spelled out.
 * If a card's suit or value is not in the normal range (that is,
 * if either of them contain the value JOKER), the String "Joker"
 * is returned instead.
public String toString()

You may write additional methods if you desire. PlayingCards must be immutable once created, so all your instance variables should be private and another class should not be able to set/change the suit or value once the card is created.

Sample Output

Now, it's usually a good idea to test each class after you write it. Here are some sample tests. If you put this code in a main method (either in the PlayingCard class or in a different tester class), it should compile and run as is. The output given in each line should match exactly with the expected values given in [brackets].

    PlayingCard card = new PlayingCard(3, PlayingCard.CLUBS);
    System.out.println("New card [3 of Clubs]: " + card.toString());
    System.out.println("Value [3]: " + card.getValue());
    System.out.println("Suit == CLUBS? [true]: " +
                       (card.getSuit() == PlayingCard.CLUBS));

    card = new PlayingCard(0, PlayingCard.HEARTS);
    System.out.println("New card [Joker]: " + card);
    System.out.println("Value == JOKER? [true]: " +
                       (card.getValue() == PlayingCard.JOKER));
    System.out.println("Suit == JOKER? [true]: " +
                       (card.getSuit() == PlayingCard.JOKER));

What to Submit

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


See A16-Part 3.


We went over some demo code in lab this week...
Here it is:

Note that you do not need to deal with any static methods for this assignment, though. The only static variables will be your playing card constants--sort of like HighScore's MAX_SCORE variable.

How do I translate a card's numerical value and suit to Strings? For example, 1 to "Ace", 13 to "King", HEARTS to "Hearts", etc.
You have to do this manually--just as for A07 when you had to translate from a day (number) to a weekday (String). You can use a series of conditionals or a switch in the toString() method to do this.
How do I convert an int to a String?
  int n = 4;
  String s = "" + n;
  String s = new Integer(n).toString();
Java supports Unicode, which contains card suit characters, as in: ♦ ♥ ♠ ♣. Couldn't we use those in toString()?
Theoretically, yes. They do show up correctly in a Java GUI pane. However, they do not appear on the command line (as that rarely uses a font that can actually represent them), so please don't use them for this assignment.
On to A16, Part 2 →