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
In that class, define
public static final int constants named
DIAMONDS. Assign each a different value; I recommend 0 to 4. (You may also want to define constants for
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.
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 : " + 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.)
Hang onto this file for now--you're not going to submit it until after you finish A16-Part 3.
See A16-Part 3.
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--which are sort of like HighScore's
HEARTSto "Hearts", etc.
toString()method to do this.
int n = 4; String s = "" + n; //gives "4"OR:
String s = new Integer(n).toString();