/** * Class for representing a standard playing card, with rank and suit. * @author Dave Reed * @version 11/15/09 */ public class Card { private String cardStr; /** * Creates a card with the specified rank and suit. * @param cardStr a two character String, where the first char is the rank * ('2', '3', '4', ..., '9', 'T', 'J', 'Q', 'K', or 'A') and * the second char is the suit ('S', 'H', 'D', or 'C') */ public Card(String cardStr) { this.cardStr = cardStr.toUpperCase(); } /** * Accessor method for the card's rank. * @return the rank of the card (e.g., '5' or 'J') */ public char getRank() { return this.cardStr.charAt(0); } /** * Accessor method for the card's suit. * @return the suit of the card (e.g., 'S' or 'C') */ public char getSuit() { return this.cardStr.charAt(1); } /** * Determines whether this card and the other match. * @param other the card to compare this one with * @return true if other is a Card with the same rank OR suit, else false */ public boolean matches(Card other) { return (this.getRank() == other.getRank() || this.getSuit() == other.getSuit()); } /** * Determines whether this card and the other are identical. * @param other the card to compare this one with * @return true if other is a Card with the same rank AND suit, else false */ public boolean equals(Object other) { return (this.getRank() == ((Card)other).getRank() && this.getSuit() == ((Card)other).getSuit()); } /** * Returns a string representation of the card. * @return a string consisting of rank followed by suit (e.g., "2H" or "QD") */ public String toString() { return cardStr; } }