Class Hand

java.lang.Object
  |
  +--Hand
All Implemented Interfaces:
java.lang.Comparable
Direct Known Subclasses:
DumbGameHand

public abstract class Hand
extends java.lang.Object
implements java.lang.Comparable

Represents the basic functionality of a hand of cards. Extensions of this class will provide the definition of what constitutes a hand for that game and how hands are compared to one another by overriding the compareTo method.

Version:
1.0
Author:
John K. Estell

Constructor Summary
Hand()
           
 
Method Summary
 void addCard(Card card)
          Adds a card to this hand.
 int compareTo(java.lang.Object otherHandObject)
          Compares two hands.
 boolean containsCard(Card card)
          Determines whether or not the hand contains the specified card.
 void discardHand()
          Removes all the cards from the hand, leaving an empty hand.
abstract  int evaluateHand()
          Evaluates the hand.
 int findCard(Card card)
          Searches for the first instance of the specified card in the hand.
 Card getCard(int index)
          Obtains the card stored at the specified location in the hand.
 int getNumberOfCards()
          The number of cards held in the hand.
 boolean isEmpty()
          Checks to see if the hand is empty.
 Card removeCard(Card card)
          Removes the specified card from the current hand.
 Card removeCard(int index)
          Removes the card at the specified index from the hand.
 boolean replaceCard(Card oldCard, Card replacementCard)
          Replaces the specified card with another card.
 void sort()
          Sorts the card in the hand.
 java.lang.String toString()
          Returns a description of the hand.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Hand

public Hand()
Method Detail

addCard

public void addCard(Card card)
Adds a card to this hand.
Parameters:
card - card to be added to the current hand.

getCard

public Card getCard(int index)
Obtains the card stored at the specified location in the hand. Does not remove the card from the hand.
Parameters:
index - position of card to be accessed.
Returns:
the card of interest, or the null reference if the index is out of bounds.

removeCard

public Card removeCard(Card card)
Removes the specified card from the current hand.
Parameters:
card - the card to be removed.
Returns:
the card removed from the hand, or null if the card was not present in the hand.

removeCard

public Card removeCard(int index)
Removes the card at the specified index from the hand.
Parameters:
index - poisition of the card to be removed.
Returns:
the card removed from the hand, or the null reference if the index is out of bounds.

discardHand

public void discardHand()
Removes all the cards from the hand, leaving an empty hand.

getNumberOfCards

public int getNumberOfCards()
The number of cards held in the hand.
Returns:
number of cards currently held in the hand.

sort

public void sort()
Sorts the card in the hand. Sort is performed according to the order specified in the Card class.

isEmpty

public boolean isEmpty()
Checks to see if the hand is empty.
Returns:
true is the hand is empty.

containsCard

public boolean containsCard(Card card)
Determines whether or not the hand contains the specified card.
Parameters:
card - the card being searched for in the hand.
Returns:
true if the card is present in the hand.

findCard

public int findCard(Card card)
Searches for the first instance of the specified card in the hand.
Parameters:
card - card being searched for.
Returns:
position index of card if found, or -1 if not found.

compareTo

public int compareTo(java.lang.Object otherHandObject)
Compares two hands.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
otherHandObject - the hand being compared.
Returns:
< 0 if this hand is less than the other hand, 0 if the two hands are the same, or > 0 if this hand is greater then the other hand.

evaluateHand

public abstract int evaluateHand()
Evaluates the hand. Must be defined in the subclass that implements the hand for the game being written by the client programmer.
Returns:
an integer corresponding to the rating of the hand.

toString

public java.lang.String toString()
Returns a description of the hand.
Overrides:
toString in class java.lang.Object
Returns:
a list of cards held in the hand.

replaceCard

public boolean replaceCard(Card oldCard,
                           Card replacementCard)
Replaces the specified card with another card. Only the first instance of the targeted card is replaced. No action occurs if the targeted card is not present in the hand.
Returns:
true if the replacement occurs.