ps0.optional
Class Card

java.lang.Object
  extended by ps0.optional.Card
All Implemented Interfaces:
Comparable<Card>

public class Card
extends Object
implements Comparable<Card>

Card is a class representing single playing card consisting of a value and a suit (e.g. [Ace,Spades],[10,Clubs]). Cards are immutable; once a Card has been created with a given value and suit, that value and suit cannot be changed.


Constructor Summary
Constructor and Description
Card(CardValue aValue, CardSuit aSuit)
          Creates a new playing card.
 
Method Summary
Modifier and Type Method and Description
 int compareTo(Card c)
          Compares this card with the specified card for order.
 boolean equals(Object otherCardObject)
          Returns true if this card is equal to the other card.
 CardSuit getSuit()
           
 CardValue getValue()
           
 int hashCode()
          Returns a hashcode for this object.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Card

public Card(CardValue aValue,
            CardSuit aSuit)
Creates a new playing card.

Parameters:
aValue - the value of this card
aSuit - the suit of this card
Effects:
creates a new Card object
Modifies:
this
Method Detail

getSuit

public CardSuit getSuit()
Effects:
returns the CardSuit associated with this card

getValue

public CardValue getValue()
Effects:
returns the CardValue associated with this card

compareTo

public int compareTo(Card c)
Compares this card with the specified card for order. The purpose of being able to compare cards is to be able to sort a hand of cards.

Cards are ranked primarily by number, secondarily by suit. That means that this card is ranked lower than another card if one of these conditions is met:

EXAMPLE: [Ace,Clubs] is ranked higher than [10,Spades] because face value is higher, but is ranked lower than [Ace,Spades] because its suit is ranked lower.

Specified by:
compareTo in interface Comparable<Card>
Parameters:
c - the Card to be compared
Throws:
ClassCastException - if the specified object's type is not Card
NullPointerException - if the specified object is null
Effects:
  • If o is not an instance of Card, throws a ClassCastException
  • If o is null, throws a NullPointerException
  • Returns a negative integer, zero, or a positive integer if this Card is less than, equal to, or greater than the specified Card, respectively

equals

public boolean equals(Object otherCardObject)
Returns true if this card is equal to the other card. Two cards are equal if both their values and suits are identical.

Overrides:
equals in class Object
Parameters:
otherCardObject - the other card
Effects:
returns true if both cards are equal; in all other cases, returns false

hashCode

public int hashCode()
Returns a hashcode for this object. This hashcode is the same for all Cards equal to this one (as indicated by the equals method). Note that it is good practice to override the hashCode method when redefining the equals method.

Overrides:
hashCode in class Object
Effects:
returns a hashcode for this object; invoking this methods on two equal Cards results in the same hashcode

toString

public String toString()
Overrides:
toString in class Object
Effects:
returns a description of this card