You are to develop a program which emulates a full deck of playing cards. That i
ID: 3584388 • Letter: Y
Question
You are to develop a program which emulates a full deck of playing cards. That is 4 suits (Clubs, Spades, Hearts, and Diamonds) and 13 ranks (Ace, 2, 3, 4, 5, 6, 7, 8, 9, Jack, Queen, King) in each suit. This of course makes for a total of 52 playing cards in the deck. Mandatory methods: public static void initDeck(int[] deck) // set the values of deck to a sequence of random // numbers, each in the range [0, 51] and no // two the same - no duplicates. public static void printCard(int card) // given a card (an int in the range 0 to 51) prints // an appropriate repressentation of this card based // on a 1-1 and onto mapping of the set [0, 51] to // the cards described above. Rules and Requirements: Your main method must end with the following block of code, which can not be modified. int cardsDealt = 0; int[] myDeck = new int[52]; final int cardsPerRow = 8; int cardsThisRow = 0; int myCard; initDeck(myDeck); System.out.println(" Here is a shuffled deck ... "); while (cardsDealt < 52) { myCard = myDeck[cardsDealt++]; ++cardsThisRow; if (cardsThisRow <= cardsPerRow) { printCard(myCard); System.out.print(" "); } else { System.out.println(""); cardsThisRow = 1; printCard(myCard); System.out.print(" "); } } System.out .println(' ');Explanation / Answer
Please Read ALL answers before rating. I'm not sure why there's so much increase in BS answers(or should I say random copying/pasting) here
public static void printCard(int card){
// given a card (an int in the range 0 to 51) prints
// an appropriate repressentation of this card based
// on a 1-1 and onto mapping of the set [0, 51] to
// the cards described above
//ranks : (Clubs, Spades, Hearts, and Diamonds)
int rank=card/13; //get rank (0,1,2,3)
card%=13;//get card value (0-12) where 0=A , (1-9)=(2-10), 10=J, 11=Q, 12=K
//printing
System.out.print(" "+Rank[rank]);//print a lead space
if(card>0&&card<10){
System.out.print(card);
}
else if(card==0){ System.out.print("A");}
else if(card==10){ System.out.print("J");}
else if(card==11){ System.out.print("Q");}
else (card==12){ System.out.print("K");}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.