/** * CS151 Fall 2007 - Project #2 solution. * Models a card from the standard deck of playing cards. * * @author Scott Russell * @version 09/30/2007 */ public class PlayingCard { // ** Instance Variables ** // the rank or face value of the card private String rank; // the suit of the card private String suit; // whether the card is face up or down private boolean faceUpStatus; // the ponit value of the card depending on the game played private int value; // * Class constants * public static final int NUM_CARDS_IN_SUIT = 13; public static final int NUM_SUITS = 4; public static final boolean DEFAULT_FACE_UP_STATUS = false; public static final int DEFAULT_VALUE = 0; // Card notation constants and their text labels public static final String ACE = "A"; public static final String TWO = "2"; public static final String THREE = "3"; public static final String FOUR = "4"; public static final String FIVE = "5"; public static final String SIX = "6"; public static final String SEVEN = "7"; public static final String EIGHT = "8"; public static final String NINE = "9"; public static final String TEN = "10"; public static final String JACK = "J"; public static final String QUEEN = "Q"; public static final String KING = "K"; public static final String CLUB = "C"; public static final String DIAMOND = "D"; public static final String HEART = "H"; public static final String SPADE = "S"; public static final String ACE_LABEL = "Ace"; public static final String TWO_LABEL = "Two"; public static final String THREE_LABEL = "Three"; public static final String FOUR_LABEL = "Four"; public static final String FIVE_LABEL = "Five"; public static final String SIX_LABEL = "Six"; public static final String SEVEN_LABEL = "Seven"; public static final String EIGHT_LABEL = "Eight"; public static final String NINE_LABEL = "Nine"; public static final String TEN_LABEL = "Ten"; public static final String JACK_LABEL = "Jack"; public static final String QUEEN_LABEL = "Queen"; public static final String KING_LABEL = "King"; public static final String CLUB_LABEL = "Clubs"; public static final String DIAMOND_LABEL = "Diamonds"; public static final String HEART_LABEL = "Hearts"; public static final String SPADE_LABEL = "Spades"; public static final String DESCRIPTION_PHRASE = " of "; public static final String UNKNOWN_CARD_PHRASE = "INVALID CARD"; // ** Constructors ** /** * Constructor for objects of class PlayingCard * @param cardCode rank and suit of card using coded notation */ public PlayingCard(String cardCode) { // initialize instance variables by calling other constructor this(cardCode.substring(0, cardCode.length() - 1), cardCode.substring(cardCode.length()-1, cardCode.length())); } /** * General constructor for objects of class PlayingCard * @param rankCode rank of desired card in coded notation * @param suitCode suit of desired card using coded notation */ public PlayingCard(String rankCode, String suitCode) { // initialize instance variables this.rank = rankCode; this.suit = suitCode; this.value = DEFAULT_VALUE; this.faceUpStatus = DEFAULT_FACE_UP_STATUS; } // ** Methods ** /** * Determines if the card is face up. * * @return TRUE if the card is face up */ public boolean isFaceUp() { return this.getFaceUpStatus(); } /** * Determines if the card is face down. * * @return TRUE if the card is face down */ public boolean isFaceDown() { //return the negation of isFaceUp() return !this.isFaceUp(); } /** * Changes the face up/down status of the card to the opposite value of what it currently is. */ public void flipOver() { //return the negation of isFaceUp() this.setFaceUpStatus(!this.getFaceUpStatus()); } /** * Gives an English language description of the card. * * @return an English language description of the card. */ public String getDescription() { String description = new String(); if (this.getRankDescription().length() != 0 && this.getSuitDescription().length() != 0) { //get the description of card's rank and suit description = this.getRankDescription() + DESCRIPTION_PHRASE + this.getSuitDescription(); } else { //set the description to unknown/invalid description = UNKNOWN_CARD_PHRASE; } return description; } public String getRank() { return rank; } public String getSuit() { return suit; } public int getValue() { return value; } public void setValue(int theValue) { this.value = theValue; } private String getRankDescription() { String description = new String(); if (getRank() == null) return description; if (getRank().equals(ACE)) description = ACE_LABEL; else if (getRank().equals(TWO)) description = TWO_LABEL; else if (getRank().equals(THREE)) description = THREE_LABEL; else if (getRank().equals(FOUR)) description = FOUR_LABEL; else if (getRank().equals(FIVE)) description = FIVE_LABEL; else if (getRank().equals(SIX)) description = SIX_LABEL; else if (getRank().equals(SEVEN)) description = SEVEN_LABEL; else if (getRank().equals(EIGHT)) description = EIGHT_LABEL; else if (getRank().equals(NINE)) description = NINE_LABEL; else if (getRank().equals(TEN)) description = TEN_LABEL; else if (getRank().equals(JACK)) description = JACK_LABEL; else if (getRank().equals(QUEEN)) description = QUEEN_LABEL; else if (getRank().equals(KING)) description = KING_LABEL; return description; } private String getSuitDescription() { String description = new String(); if (getSuit() == null) return description; if (getSuit().equals(CLUB)) description = CLUB_LABEL; else if (getSuit().equals(DIAMOND)) description = DIAMOND_LABEL; else if (getSuit().equals(HEART)) description = HEART_LABEL; else if (getSuit().equals(SPADE)) description = SPADE_LABEL; return description; } /* * Determines the face up/down status of the cardif the card is face up. * @return TRUE if the card is face up */ private boolean getFaceUpStatus() { return this.faceUpStatus; } /* * Sets the face up status of the card to specified truth value. */ private void setFaceUpStatus(boolean newFaceUpStatus) { this.faceUpStatus = newFaceUpStatus; } }