in java pls (Simulation: coupon collector’s problem) Coupon collector is a class
ID: 3903819 • Letter: I
Question
in java pls
(Simulation: coupon collector’s problem) Coupon collector is a classic statistics problem with many practical applications. The problem is to pick objects from a set of objects repeatedly and find out how many picks are needed for all the objects to be picked at least once. You must implement a variation of the problem: pick cards from a shuffled deck of 52 cards repeatedly and find out how many picks are needed before you see one of each suit. Assume a picked card is placed back in the deck before picking another. Write a program to simulate the number of picks needed to get four cards from each suit and display the four cards picked (it is possible a card may be picked twice). Here is a sample run of the program:
Queen of Spades
5 of Clubs
Queen of Hearts
4 of Diamonds
Number of picks: 12
Explanation / Answer
import java.io.*;
import java.util.*;
class Card{
public String name;
public String suit;
public Card(String n,String s){
name =n;
suit = s;
}
}
public class Demo191{
public static void main(String[] args){
String[] suit = {"Spade", "Clubs","Diamonds", "Heart"};
String[] name = {"Ace", "2","3","4","5","6","7","8","9","10" ,"Jack","Queen","King"};
Card[] deck = new Card[52];
Card[] select = new Card[4];
Random rand = new Random();
for (int i = 0; i<52; i++){
int found = 1;
String s = "";
String n = "";
while (found == 1){
s = suit[rand.nextInt(4)];
n = name[rand.nextInt(13)];
int valid = 0;
for (int j = 0; j<i; j++){
if (deck[j].suit.equals(s) && deck[j].name.equals(n)){
valid = 1;
break;
}
}
if (valid == 0){
found = 0;
}
}
deck[i] = new Card(s,n);
}
int count = 0;
int numpicks = 0;
while(count < 4){
int found = 1;
String s = "";
String n = "";
while (found == 1){
s = deck[rand.nextInt(52)].suit;
n = deck[rand.nextInt(52)].name;
numpicks++;
int valid = 0;
for (int j = 0; j<count; j++){
if (select[j].suit.equals(s) && select[j].name.equals(n)){
valid = 1;
break;
}
}
if (valid == 0){
found = 0;
}
}
select[count] = new Card(s,n);
count++;
}
for (int i = 0; i<4; i++){
System.out.println(select[i].name + " of " + select[i].suit);
}
System.out.println("Number of picks:" + numpicks);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.