/** I think it would be instructive to look at a Card constructor, * that creates a card object, using just an integer as a parameter. * This constructor would be useful when we want to shuffle cards, * because a random number generator can give us a number 1-52, and * we need to be able to determine from this number what the denomination * and suit should be. * * To make this procedure more efficient, we create a denom number and * suit number. And these 2 numbers will correspond to the denomination * and suit we want. */ public class Card { private char denom; private char suit; /** We assume that i is in the range 1-52. * Because of the behavior of the % and / operators, * the denom number will be: 1=ace, 2=two, ..., 12=queen, 0=king * and the suit number will be 0=clubs up to 3=spades. * We need to say (i-1) for calculating the suit number because * we want the outcome to be in the range 0-3, and the king of spades, * being card # 52, would have been 52/13 = 4. The other kings would have * been wrong too: 13 (king of clubs) would have been called a diamond. */ public Card(int i) { int denomNum = i % 13; int suitNum = (i-1) / 13; switch(denomNum) { case 1 : denom = 'A'; break; case 10 : denom = 'T'; break; case 11 : denom = 'J'; break; case 12 : denom = 'Q'; break; case 0 : denom = 'K'; 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; } } public String toString() { return denom + "-" + suit; } }