Class Card

java.lang.Object
  |
  +--Card
All Implemented Interfaces:
java.lang.Comparable

public class Card
extends java.lang.Object
implements java.lang.Comparable

Representation of a single playing card. A card consists of a suit value (e.g. hearts, spades), a rank value (e.g. ace, 7, king), and an image of the front of the card. A card object is immutable; once instantiated, the values cannot change.

Version:
1.0
Author:
John K. Estell

Constructor Summary
Card(Suit suit, Rank rank, javax.swing.ImageIcon cardFace)
          Creates a new playing card.
 
Method Summary
 int compareTo(java.lang.Object otherCardObject)
          Compares two cards for the purposes of sorting.
 javax.swing.ImageIcon getCardImage()
          Returns the graphic image of the card.
static java.lang.String getFilename(Suit suit, Rank rank)
          Generates the filename associated with the card.
 Rank getRank()
          Returns the rank of the card.
 Suit getSuit()
          Returns the suit of the card.
 boolean isSameAs(Card card)
          Compares two cards to determine if they have the same value.
 java.lang.String rankToString()
          Returns a description of the rank of this card.
static void setRankMajorSort()
          Specifies that cards are to be sorted in rank-major order.
static void setSuitMajorSort()
          Specifies that cards are to be sorted in suit-major order.
 java.lang.String suitToString()
          Returns a description of the suit of this card.
 java.lang.String toString()
          Returns a description of this card.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Card

public Card(Suit suit,
            Rank rank,
            javax.swing.ImageIcon cardFace)
Creates a new playing card.
Parameters:
suit - the suit value of this card.
rank - the rank value of this card.
cardFace - the face image of this card.
Method Detail

getFilename

public static java.lang.String getFilename(Suit suit,
                                           Rank rank)
Generates the filename associated with the card. getFilename assumes that all of the standard card images are stored in individual files using filenames in the form of: RS.gif where R is a single character used to represent the rank value of the card and S is a single character used to represent the suit value of the card.

The characters used for R are: 'a' (ace), '2', '3', '4', '5', '6', '7', '8', '9', 't' (10), 'j' (jack), 'q' (queen), and 'k' (king).

The characters used for S are: 'c' (clubs), 'd' (diamonds), 'h' (hearts), and 's' (spades).

Two other cards are also available: "b.gif" (back of card) and "j.gif" (joker).

Parameters:
suit - the suit value of the card.
rank - the rank value of the card.
Returns:
a string containing the filename of the card.

getSuit

public Suit getSuit()
Returns the suit of the card.
Returns:
a Suit constant representing the suit value of the card.

getRank

public Rank getRank()
Returns the rank of the card.
Returns:
a Rank constant representing the rank value of the card.

getCardImage

public javax.swing.ImageIcon getCardImage()
Returns the graphic image of the card.
Returns:
an icon containing the graphic image of the card.

toString

public java.lang.String toString()
Returns a description of this card.
Overrides:
toString in class java.lang.Object
Returns:
the name of the card.

rankToString

public java.lang.String rankToString()
Returns a description of the rank of this card.
Returns:
the rank value of the card as a string.

suitToString

public java.lang.String suitToString()
Returns a description of the suit of this card.
Returns:
the suit value of the card as a string.

setRankMajorSort

public static void setRankMajorSort()
Specifies that cards are to be sorted in rank-major order. Cards are ordered first by their rank value; cards of the same rank are then ordered by their suit value.

setSuitMajorSort

public static void setSuitMajorSort()
Specifies that cards are to be sorted in suit-major order. Cards are ordered first by their suit value; cards of the same suit are then ordered by their rank value.

compareTo

public int compareTo(java.lang.Object otherCardObject)
Compares two cards for the purposes of sorting. Cards are ordered first by their suit value, then by their rank value.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
otherCardObject - the other card
Returns:
a negative integer, zero, or a positive integer is this card is less than, equal to, or greater than the referenced card.

isSameAs

public boolean isSameAs(Card card)
Compares two cards to determine if they have the same value. This is not the same as the use of equals which compares two objects for equality.
Parameters:
card - the other card
Returns:
true if the two cards have the same rank and suit values, false if they do not.