Question

In: Computer Science

In Java Solve the following problem using arrays: Past A: Coupon collector is a classic statistic...

In Java

Solve the following problem using arrays:

Past A: Coupon collector is a classic statistic problem with many practical applications. The problem is to pick objects from a set of objects repeatedly and determine how many picks are needed for all the objects to be picked at least once. A variation of the problem is to 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 total of four cards from each different suit and display the four cards picked (it is possible that 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

Sample run explanation: As you see in the above run, 12 picks are made to get the four cards from different suits. The other 8 picks (12-4=8) were from the same previously picked suits, so they are not printed. So we continue picking a card until we see at least one card from each of the for suits.

Note: The card pick is with replacement, meaning that when you pick a card from the deck of 52 card, you put it back in the deck. There is chance to see the previously selected card again.

Part B: Put part A in a for loop and repeat it 10,000 times and report the average number of total picks we should have to see 4 cards from different suits.

Solutions

Expert Solution

  PART A Code Below
==========================================================================
public class Card {

    private String suit;
    private String face;

    public Card(String suit, String face) {
        this.suit = suit;
        this.face = face;
    }

    public String getSuit() {
        return suit;
    }

    public String getFace() {
        return face;
    }


    @Override
    public String toString() {
        return getFace() + " of " + getSuit();
    }

}

==========================================================================

import java.util.Random;

public class CouponCollector {


    public static void main(String[] args) {


        String ranks[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
        String suits[] = {"Spades", "Diamonds", "Clubs", "Hearts"};

        Card deck[] = new Card[ranks.length * suits.length];

        int index = 0;
        for (String rank : ranks) {
            for (String suit : suits) {
                deck[index++] = new Card(suit, rank);
            }
        }

        Random random = new Random();
        int randomCard = 0;
        boolean allSuitPicked[] = {false, false, false, false};
        int attempts = 0;
        while (!allSuitPicked[0] || !allSuitPicked[1] || !allSuitPicked[2] || !allSuitPicked[3]) {
            attempts += 1;
            randomCard = random.nextInt(deck.length);
            Card card = deck[randomCard];
            System.out.println(card);
            if (card.getSuit().equals("Spades")) allSuitPicked[0] = true;
            else if (card.getSuit().equals("Diamonds")) allSuitPicked[1] = true;
            else if (card.getSuit().equals("Clubs")) allSuitPicked[2] = true;
            else if (card.getSuit().equals("Hearts")) allSuitPicked[3] = true;
        }
        System.out.println("Number of picks: "+attempts);
    }
}

==========================================================================

For PART B , Card.class will be the same, have updated the CouponCollector.java class only below is the updated class

import java.util.Random;

public class CouponCollector {


    public static void main(String[] args) {


        String ranks[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
        String suits[] = {"Spades", "Diamonds", "Clubs", "Hearts"};

        Card deck[] = new Card[ranks.length * suits.length];

        int index = 0;
        for (String rank : ranks) {
            for (String suit : suits) {
                deck[index++] = new Card(suit, rank);
            }
        }

        Random random = new Random();
        int totalAttempts = 0;
        for (int experiment = 1; experiment <= 10000; experiment++) {
            int randomCard = 0;
            boolean allSuitPicked[] = {false, false, false, false};
            int attempts=0;
            while (!allSuitPicked[0] || !allSuitPicked[1] || !allSuitPicked[2] || !allSuitPicked[3]) {
                attempts += 1;
                randomCard = random.nextInt(deck.length);
                Card card = deck[randomCard];
                //System.out.println(card);
                if (card.getSuit().equals("Spades")) allSuitPicked[0] = true;
                else if (card.getSuit().equals("Diamonds")) allSuitPicked[1] = true;
                else if (card.getSuit().equals("Clubs")) allSuitPicked[2] = true;
                else if (card.getSuit().equals("Hearts")) allSuitPicked[3] = true;
            }
            System.out.println("Experiment "+experiment+", Attempts count: "+attempts);
            totalAttempts+=attempts;

        }
        System.out.println("Average number of picks: " + totalAttempts/10000.0);
    }
}


Related Solutions

java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem....
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. Recursion may appear in various contexts and in different forms. For fast implementation, we should always aim at transforming recursions into a simpler form of computation. In this assignment, the task is to evaluate X(·), which is defined as follows:               |0,if m = 0 or n = 0               | X(m,n−1),if n is odd and m is even X(m,n) = | X(m−1,n),if m...
For this question we will be using arrays and classes in Java to compute the min,...
For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below: -Create methods for min, max, and average and call them from main to print out their values. -Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add...
Solve this problem in C. Suppose a county tax collector collects property taxes on the assessed...
Solve this problem in C. Suppose a county tax collector collects property taxes on the assessed value of each piece of property in their county. Here are the tax collector's rules for taxes: Assessed value is calculated at the rate of 75% of the property's actual value. Homeowners who live at their property are provided with a $10,000 homeowner exception that further reduces their assessed value below the 75% level. Homeowners are allowed to be billed in quarterly installments if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT