package lab.games; /** * This class represents a playing card. * * @author Franck van Breugel */ public class Card implements Comparable { private int suit; private int number; /** * A jack. */ public static final int JACK = 11; /** * A queen. */ public static final int QUEEN = 12; /** * A king. */ public static final int KING = 13; /** * An ace. */ public static final int ACE = 14; /** * The suit hearts. */ public static final int HEARTS = 1; /** * The suit spades. */ public static final int SPADES = 2; /** * The suit diamonds. */ public static final int DIAMONDS = 3; /** * The suit clubs. */ public static final int CLUBS = 4; /** * Initializes this card with the given suit and number. * * @param suit the suit of this card. * @pre. suit == Cards.HEARTS || suit == Card.SPADES || suit == Card.DIAMONDS || suit == Card.CLUBS * @param number the number of this card. * @pre. 2 <= number && number <= Card.ACE */ public Card(int suit, int number) { this.setSuit(suit); this.setNumber(number); } /** * Returns the suit of this card. * * @return the suite of this card. */ public int getSuit() { return this.suit; } /** * Returns the number of this card. * * @return the number of this card. */ public int getNumber() { return this.number; } /** * Sets the suit of this card to the given suit. * * @param suit the suit of this card. * @pre. suit == Cards.HEARTS || suit == Card.SPADES || suit == Card.DIAMONDS || suit == Card.CLUBS */ public void setSuit(int suit) { this.suit = suit; } /** * Sets the number of this card to the given number. * * @param number the number of this card. * @pre. 2 <= number && number <= Card.ACE */ public void setNumber(int number) { this.number = number; } /** * Checks whether this card is equal to the given object, * that is, the given object is a card with the same * suit and number as this card. * * @param object an object. * @return true if this card is equal to the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Card other = (Card) object; equal = this.getSuit() == other.getSuit() && this.getNumber() == other.getNumber(); } else { equal = false; } return equal; } // Since I overrode equals I should have overridden hashCode as well /** * Returns a string representation of this card. * The representation consists of two or three characters: * the first (and second) character represents the number of this card * and the second (or third) character is the Unicode character of * the suit of this card. Jack, queen, king and ace * are represented by the characters J, Q, K and A. * All other numbers are represented by itself. * * @return a string representation of this card. */ public String toString() { String representation; switch (this.getNumber()) { case Card.JACK : representation = "J"; break; case Card.QUEEN : representation = "Q"; break; case Card.KING : representation = "K"; break; case Card.ACE : representation = "A"; break; default : representation = "" + this.getNumber(); break; } switch (this.getSuit()) { case Card.HEARTS : representation += "\u2665"; break; case Card.CLUBS : representation += "\u2663"; break; case Card.DIAMONDS : representation += "\u2666"; break; case Card.SPADES : representation += "\u2660"; break; default : // we should never get here so do nothing } return representation; } /** * Compares this card with the other given card. * This card is considered smaller if either * the suit of this card is smaller than the suit * of the other card, or the suit of this card is * the same as the suit of the other card and the * number of this card is smaller than the number * of the other card. * * @param other another card. * @return a negative integer if this card is smaller * than the other card, zero if this card is equal * to the other card, a positive integer is this card * is greater than the other card. * @throws NullPointerException if other is null. */ public int compareTo(Card other) { int difference; if (this.getSuit() == other.getSuit()) { difference = this.getNumber() - other.getNumber(); } else { difference = this.getSuit() - other.getSuit(); } return difference; } }