HOW TO ADAPT CARD (below) with enums to POKERCARD, with adapter enum Suit { CLUB
ID: 3730759 • Letter: H
Question
HOW TO ADAPT CARD (below) with enums to POKERCARD, with adapter
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES; }
enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING, ACE ;}
public class Card {
private Rank rank;
private Suit suit;
public Card(Rank rank, Suit suit){
this.rank = rank;
this.suit = suit;
}
public Rank getRank(){
return this.rank;
}
public Suit getSuit(){
return this.suit;
}
}
(START OF ADAPTER?)
public class CardAdapter extends PokerCard {
PokerCard theCard;
public CardAdapter(int theValue, int theSuit) {
super(theValue, theSuit);
}
}
(Allow card to work as a pokercard through adapter)
public class PokerCard {
public final static int SPADES = 0; // Codes for the 4 suits, plus Joker.
public final static int HEARTS = 1;
public final static int DIAMONDS = 2;
public final static int CLUBS = 3;
public final static int ACE = 14; // Codes for the non-numeric cards.
public final static int JACK = 11; // Cards 2 through 10 have their
public final static int QUEEN = 12; // numerical values for their codes.
public final static int KING = 13;
private final int suit;
private final int value;
public PokerCard(int theValue, int theSuit) {
if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS &&
theSuit != CLUBS)
throw new IllegalArgumentException("Illegal playing card suit");
if ((theValue < 2 || theValue > 14))
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() {
switch ( value ) {
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";
case 13: return "King";
default: return "Ace";
}
}
public String toString() {
return getValueAsString() + " of " + getSuitAsString();
}
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof PokerCard))
return false;
PokerCard that = (PokerCard)obj;
return (this.suit == that.suit && this.value == that.value);
}
} // end class PokerCard
Explanation / Answer
Solution :
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.