Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need to use the Adapter Design pattern in Java for the two incompatible interface Card and Ka (Card in Chinese language). Here is the
I need to use the Adapter Design pattern in Java for the two incompatible interface Card and Ka (Card in Chinese language). Here is the code for both the interfaces and the classes implementing these interfaces:
Card interface:
public interface Card { /** * Gets rank. * * @return the rank */ Rank getRank(); /** * Gets suit. * * @return the suit */ Suit getSuit(); }
CardImpl class:
public class CardImpl implements Card{ private RankEnum rank; private SuitEnum suit; /** * Instantiates a new Card. * * @param rank the rank * @param suit the suit */ public CardImpl(RankEnum rank, SuitEnum suit){ this.rank = rank; this.suit = suit; } /** * Its a getter method for the Rank. */ @Override public RankEnum getRank() { return rank; } /** * Its a setter method for the Rank. */ @Override public SuitEnum getSuit() { return suit; } }
Ka interface:
public interface Ka { int getRank(); int getSuit(); }
kaShiXia class:
public class kaShiXia implements Ka { public kaShiXia(KaDengJEnum kaHao, int suit) { if (kaHao == null) { throw new IllegalArgumentException("ill-formed ka"); } validateSuit(suit); this.kaHao = kaHao.value; this.suit = suit; } /** * private method to determine if a suit is within range * * @param suit */ private void validateSuit(int suit) { if ((suit < 0) || (suit > 4)) { throw new IllegalArgumentException("ill-formed ka"); } } /** * Instantiates a new ka shi xia. * * @param kaHao the ka hao * @param suit the suit */ public kaShiXia(int kaHao, int suit) { validateSuit(suit); if ((kaHao < 0) || (kaHao > 12)) { throw new IllegalArgumentException("ill-formed card"); } this.kaHao = kaHao; this.suit = suit; } /* * @see diErBao.Ka#getRank() */ public int getRank() { return kaHao; } /* * @see diErBao.Ka#getSuit() */ public int getSuit() { return suit; } @Override public String toString() { return kaHao + " of " + KaPianTaoZhuangEnum.getKaPianTaoZhuang(getSuit()); } public boolean equals(Object object) { if (object instanceof Ka) { kaShiXia ka = (kaShiXia) object; return (ka.kaHao == this.kaHao && ka.suit == this.suit); } else return super.equals(object); } @Override public int hashCode() { return Objects.hash(kaHao, suit); } /** * Enum of card suits */ public enum KaPianTaoZhuangEnum { xin, tieQiao, zuanShi, juLeBu; // hearts, spades, diamonds, clubs private static KaPianTaoZhuangEnum[] list = KaPianTaoZhuangEnum.values(); public static KaPianTaoZhuangEnum getKaPianTaoZhuang(int i) { return list[i]; } } /** * Enum of card ranks */ public enum KaDengJEnum { TWO(0b0101), THREE(0b1001), FOUR(0b1101), FIVE(0b0100), SIX(0b1000), SEVEN(0b1100), EIGHT(0b0011), NINE(0b0111), TEN(0b1011), JACK(0b0010), QUEEN(0b0110), KING(0b1010), ACE(0b0001); private int value; private KaDengJEnum(int value) { this.value = value; } public static KaDengJEnum lookupEnumName(int correspondingValue) { for (KaDengJEnum kaRank : KaDengJEnum.values()) { if (correspondingValue == kaRank.value) return kaRank; } return null; } } private final int kaHao; // card number private final int suit; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started