/** This is the playing card class. Its 2 attributes are denomination * and suit, which are both char. * We could simply have kept the Poker version and added what we need, * but we'd be cluttered with many functions that are never used. */ public class Card { private char denom; private char suit; /** This initial-value constructor takes a single integer in the range * 1-52 and converts it to a Card having a denomination and suit based on * this number. */ public Card(int i) { int denomNum = i % 13; int suitNum = (i-1) / 13; switch(denomNum) { case 1 : denom = 'A'; break; case 0 : denom = 'K'; break; case 12 : denom = 'Q'; break; case 11 : denom = 'J'; break; case 10 : denom = 'T'; break; default : denom = (char) (denomNum + '0'); break; } switch(suitNum) { case 0 : suit = 'c'; break; case 1 : suit = 'd'; break; case 2 : suit = 'h'; break; case 3 : suit = 's'; break; } } /** We may find it convenient to have a copy constructor when it comes * time to deal cards. */ public Card(Card c) { denom = c.denom; suit = c.suit; } /** Here's a nice way to print cards, using a phrase such as "four of clubs" */ public String toString() { String denomName = new String(); String suitName = new String(); switch(denom) { case '2': denomName = "two"; break; case '3': denomName = "three"; break; case '4': denomName = "four"; break; case '5': denomName = "five"; break; case '6': denomName = "six"; break; case '7': denomName = "seven"; break; case '8': denomName = "eight"; break; case '9': denomName = "nine"; break; case 'T': denomName = "ten"; break; case 'J': denomName = "jack"; break; case 'Q': denomName = "queen"; break; case 'K': denomName = "king"; break; case 'A': denomName = "ace"; break; } switch (suit) { case 'c': suitName = "clubs"; break; case 'd': suitName = "diamonds"; break; case 'h': suitName = "hearts"; break; case 's': suitName = "spades"; break; } return denomName + " of " + suitName; } /** shortForm() -- Sometimes we just want to print a shortened form * of the name of this card like J-c instead of "jack of clubs". */ public String shortForm() { return denom + "-" + suit; } }