Complete the program to shuffle the deck, simulate the number of picks needed to
ID: 3548289 • Letter: C
Question
Complete the program to shuffle the deck, simulate the number of picks needed to get four cards (one from each suit), and display the four cards picked (it is possible a card may be picked many times).
use:
public class PickedCards
{
public static void main (String[] args)
{
// Create an ordered deck of 52 cards in the order of Clubs, Diamonds, Hearts, and Spades
// Assume that 13 cards of each suit are number in the following way:
// 13 cards of Clubs are numbered : 1 - 13
// 13 cards of Diamonds are numbered : 14 - 26
// 13 cards of Hearts are numbered : 27 - 39
// 13 cards of Spades are numbered : 40 - 52
int[] cards = new int[52];
for(int i=0; i < 52 ; i++)
cards[i] = i+1;
// Your program here:
// write a program that
// 1. shuffles the deck,
// 2. picks cards out of a shuffled deck of 52 cards until one from each suit was picked,
// 3. displays which four cards were picked,
// 4. and displays how many times the program picked cards.
}
}
Hint: Use Random class to create a shuffled deck and simulate the random picks.
Hint: To shuffle cards, given an ordered deck, choose two cards randomly and swap them. Repeat to swap for 30 times.
Hint: Use an array (or ArrayList) to keep the record about which card from which suit was picked so far.
Explanation / Answer
Hi,
Please find below the program.
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class PickedCards {
public static void main(String[] args) {
int[] cards = new int[52];
for (int i = 0; i < 52; i++)
cards[i] = i + 1;
Collections.shuffle(Arrays.asList(cards));// Shuffles the array
Random rnd = new Random();
int rd = 0;
int count = 0;
int prgcount = 0;
int scount = 0;int dcount = 0;int hcount = 0;int ccount = 0;
while (true) {
rd = rnd.nextInt(53);
if (rd >= 0 && rd <= 13 && ccount == 0) {
System.out.println("Card from club suite" + cards[rd]);//display the suite
ccount = ccount + 1;
count=count+1;
}
else if (rd >= 14 && rd <= 26&& dcount == 0) {
System.out.println("Card from diamond suite" + cards[rd]);//display the suite
dcount = dcount + 1;
count=count+1;
}
else if (rd >= 27 && rd <= 39 && hcount == 0) {
System.out.println("Card from hearts suite" + cards[rd]);//display the suite
hcount = hcount + 1;
count=count+1;
}
else if (rd >= 40 && rd <= 51 && scount == 0) {
System.out.println("Card from spade suite" + cards[rd]);//display the suite
scount = count + 1;
count=count+1;
}
if (count == 4)
break;
else
prgcount = prgcount + 1;
}
System.out
.println("Total number of times cards from all suite were picked"
+ prgcount);
}
}
Output 1
Card from hearts suite35
Card from club suite7
Card from spade suite43
Card from diamond suite23
Total number of times cards from all suite were picked7
Output 2
Card from club suite12
Card from hearts suite34
Card from spade suite43
Card from diamond suite22
Total number of times cards from all suite were picked7
Hope it helps.
Thanks,
Sri
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.