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...
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
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
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...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves from an initial board...
Solve the following problem using the simplex method. If the problem is two dimensional, graph the...
Solve the following problem using the simplex method. If the problem is two dimensional, graph the feasible region, and outline the progress of the algorithm. Max               Z = 5X1 + 3X2 + 2X3 Subject to    4X1 + 5X2 + 2X3 + X4≤ 20                      3X1 + 4X2 - X3 + X4≤ 30                       X1, X2, X3, X4 ≥ 0   
Write a complete Java Program to solve the following problem. February 18 is a special date...
Write a complete Java Program to solve the following problem. February 18 is a special date as this is the date that can be divisible by both 9 and 18 Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT