Assignment 15: Part 1 (PlayingCard)

Task

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

Concepts: Review of class-related concepts

Steps

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 may reuse this documentation if you want to, so that you don't have to write your own.)

/**
 * Constructs a new card with the given face value and suit.
 *
 * The given card 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 must 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(20, 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));

Note that this is not a complete battery of tests. For example, does your PlayingCard perform correctly if you build one with a legal value but an illegal suit? (Such a card should print out as "Joker", with both getSuit() and getValue() returning your JOKER value.)

What to Submit

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

Grading

See A15-Part 3.

FAQs

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 in A08 when you had to translate from a day (number) to a weekday (String) or in A13 when you had to translate an inputted month (number) to the name of that month (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;  //gives "4"
OR:
  String s = new Integer(n).toString();
OR:
  String s = Integer.toString(n);
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 A15, Part 2 →