Skip to content

Instantly share code, notes, and snippets.

@jriano
Created September 2, 2011 10:13
Show Gist options
  • Save jriano/1188318 to your computer and use it in GitHub Desktop.
Save jriano/1188318 to your computer and use it in GitHub Desktop.
PlayingCard
//Class to repesent a playing-card
// Any card has a SUIT (Clubs, Diamonds, Hearts, Spades)
// Any card has a RANK (2, 3, .. 19, Jack, Queen, King, Ace)
//
public class PlayingCard implements Comparable
{
//Instance Variables
private CardSuit suit;
private CardRank rank;
//Constructor:
//Creates a new PlayingCArd with specified suit and rank
public PlayingCard(CardSuit suit, CardRank rank)
{
this.suit = suit;
this.rank = rank;
}
//Constructor:
//Creates a new PlayingCard corresponding to a given image, "2S", "KD", etc
// Precondition: 1st character of the image is a valid CardRank symbol
// Precondition: 2nd character of the image is a valid CardSuit symbol
public PlayingCard(String image)
{
this(CardSuit.suit(image.charAt(1)), CardRank.rank(image.charAt(0)));
}
//Accessor
public CardRank getRank()
{
return this.rank;
}
//Accessor
public CardSuit getSuit()
{
return this.suit;
}
//Returns a duplicate of a PlayingCard
public PlayingCard copy()
{
// incomplete
return null;
}
//Returns a printable 2-character image of a PlayingCard
// Postcondition: the image is a character string of length 2, e.g. 3H
// Postcondition: 1st character of the image is a valid CardRank symbol
// Postcondition: 2nd character of the image is a valid CardSuit symbol
@Override public String toString()
{
return this.getRank().toString()+this.getSuit().toString();
}
//Implements the Comparable interface for PlayingCard's
public int compareTo(Object other)
{
// incomplete
PlayingCard that = (PlayingCard)other;
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment