Create a program to allow one of the cards (e.g. card 0) to be twice as rare as the other cards. Using a rare card will make the number of boxes needed to buy much larger. Use Program 1 as a reference to create. Use Code from Program 1 Below:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
 

Computer Science

** Java Program **

Create a program to allow one of the cards (e.g. card 0) to be twice as rare as the other cards. Using a rare card will make the number of boxes needed to buy much larger. Use Program 1 as a reference to create.

Use Code from Program 1 Below:

public static void main(String[] args) {

/**
* A cereal producer puts in each box of cereals a collector card
* with player from your favorite sport.
* We would like to know which is the average number of boxes that we need to buy
* in order to get all the cards.
* To compute the number we will run simulations that will randomly pick a card
* for each bought box, until we get all the cards.
* The user will provide the number of players in the promotion
* and how many simulations we will run.
* We want to compute and print some statistics related to the performed simulations:
* - average number of boxes bought
* - minimum number of boxes bought
* - maximum number of boxes bought.
*
*
*/
public class CardCollectorSimulation {

   /**
   * @param args
   */
   public static void main(String[] args) {
       System.out.println("Card Collector Simulation");
      
       // Read the number of players in the promotion
       Scanner scanner=new Scanner(System.in);
       int numberOfPlayers;
       boolean error;
       do {
           System.out.print("Number of players: ");
           numberOfPlayers = scanner.nextInt();
           error = numberOfPlayers<2;
           if (error) {
               System.out.println("Please provide a whole number greater than 1.");
           }  
       } while(error);
              
       // Read the number of simulations
       int numberOfSimulations;
       do {
           System.out.println("Number of simulations:");
           numberOfSimulations = scanner.nextInt();
           error = numberOfSimulations < 1;
           if (error) {
               System.out.println("Please provide a whole number at least 1.");
           }
       } while (error);
      
       // Define the array for the results of simulations
       int[] numberOfBoxes = new int[numberOfSimulations];
      
       // for each simulation, run the simulation and store the results
       for (int s=1; s<=numberOfSimulations; s++) {
           // run the simulation s
          
           // initialize the marker that keeps what cards I already bought
           boolean[] bought = new boolean[numberOfPlayers];
           // no card bought
           for (int c=0; c<numberOfPlayers; c++) bought[c]=false;
           int numberOfDifferentCards = 0;
           int boxNumber=0;
          
           do {
               boxNumber++;
               int cardNumber = (int)(Math.random()*numberOfPlayers);
           if (!bought[cardNumber]) {
               numberOfDifferentCards++;
               bought[cardNumber] = true;
           }
              
           } while(numberOfDifferentCards<numberOfPlayers);
          
           System.out.printf("Simulation %5d needs %4d boxes.\n", s, boxNumber);
           numberOfBoxes[s-1]=boxNumber;
       }
          
       // process the results to compute average, minimum, maximum
       int sum = numberOfBoxes[0];
       int minimum = numberOfBoxes[0];
       int maximum = numberOfBoxes[0];
       for (int s=1; s<numberOfSimulations; s++) {
           sum += numberOfBoxes[s];
           if (numberOfBoxes[s] < minimum) minimum = numberOfBoxes[s];
           else if (numberOfBoxes[s] > maximum) maximum = numberOfBoxes[s];
       }
       double average = 1.0 * sum / numberOfSimulations;
      
       // print the results
       System.out.println("Average number of boxes: "+average);
       System.out.println("Minimum number of boxes: "+minimum);
       System.out.println("Maximum number of boxes: "+maximum);

   }

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY