A Card Deck A Deck consists of 52 unique cards. The cards are divided into four
ID: 3797919 • Letter: A
Question
A Card Deck A Deck consists of 52 unique cards. The cards are divided into four suits, each with 13 cards. The suits are Clubs Diamonds, Hearts and Spades. Each card also has a rank, which can be either a number or a face. The numbers range from 2 to 10 (inclusive), and the faces are Jack, Queen, King and Ace. The order of the ranks is that a 2 is the lowest and Ace the highest. The entire ordering for rank is lowest rightarrow 2 3 4 5 6 7 8 9 10 Jack Queen King Ace left arrow highest If two cards have the same rank, then the suit determines the higher card. The lowest suit is Clubs, and the highest Spades The entire ordering of suits is lowest rightarrow Clubs Diamonds Hearts Spades leftarrow highest Some examples are a 3 of any suit is higher than a 2 of any suit a 2 of Spades is higher than a 2 of Clubs, Diamonds or Hearts Jack of any suit is higher than any numbered card) of any suit King of any suit is higher than a Queen of any suit an Ace of any suit is higher than a King of any suit a King of Hearts than a King of Clubs or DiamondsExplanation / Answer
public class Card {
public final static int SPADES = 0;
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;
public final static int JOKER = 4;
public final static int ACE = 1;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;
private final int suit;
private final int value;
public Card() {
suit = JOKER;
value = 1;
}
public Card(int theValue, int theSuit) {
if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS &&
theSuit != CLUBS && theSuit != JOKER)
throw new IllegalArgumentException("Illegal playing card suit");
if (theSuit != JOKER && (theValue < 1 || theValue > 13))
throw new IllegalArgumentException("Illegal playing card value");
value = theValue;
suit = theSuit;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String getSuitAsString() {
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "Joker";
}
}
public String getValueAsString() {
if (suit == JOKER)
return "" + value;
else {
switch ( value ) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
default: return "King";
}
}
}
public String toString() {
if (suit == JOKER) {
if (value == 1)
return "Joker";
else
return "Joker #" + value;
}
else
return getValueAsString() + " of " + getSuitAsString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.