public final class PlayingCard extends Object implements Comparable<PlayingCard>
Constructor and Description |
---|
PlayingCard(String rank,
String suit)
Create a playing card with a given rank and suit.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(PlayingCard other)
Compares two playing cards by their ranks and suits.
|
boolean |
equals(Object obj)
Compares this playing card to the specified object.
|
static Set<String> |
getAllRanks()
Returns a set of the strings that can be used to specify the rank of a
playing card.
|
static Set<String> |
getAllSuits()
Returns a set of the strings that can be used to specify the suit of a
playing card.
|
String |
getRank()
Gets the rank of the card.
|
String |
getSuit()
Gets the suit of the card.
|
int |
hashCode()
Return a hash code for this playing card.
|
static void |
main(String[] args) |
String |
toString()
Returns a string representation of this playing card.
|
public PlayingCard(String rank, String suit)
Valid ranks are "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" (the number cards 2 through 10, and the face cards jack, queen, king, and ace).
Valid suits are "H", "D", "C", "S" (the suits hearts, diamonds, clubs, and spades).
IllegalArgumentException
- if the rank or the suit is
not valid.rank
- The rank of the card.suit
- The suit of the card.public String getRank()
PlayingCard(String, String)
.PlayingCard(String, String)
public String getSuit()
PlayingCard(String, String)
.PlayingCard(String, String)
public int hashCode()
public boolean equals(Object obj)
true
if and only if the argument is a PlayingCard
with the same rank and suit as this card.public int compareTo(PlayingCard other)
The cards are first compared by their ranks. If their ranks are equal then the cards are compared by their suits.
The ordering of ranks from smallest to largest is 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A (i.e., 2 is considered the smallest rank and A (ace) is considered the largest rank).
The order of suits from smallest to largest is S, C, D, H (i.e., spades < clubs < diamonds < hearts).
compareTo
in interface Comparable<PlayingCard>
other
- The PlayingCard
to be compared against.0
if argument card is equal to this card; a
value less than zero if this card is smaller than the other card; a
value greater than zero if this card is greater than the other
card.public String toString()
The returned string is the rank of the card followed by " of "
followed by the suit of the card. For example, the card representing the
ace of hearts has the string representation "A of H"
.
public static Set<String> getAllRanks()
PlayingCard(String, String)
.public static Set<String> getAllSuits()
PlayingCard(String, String)
.public static void main(String[] args)