Question: Need help writing a java Birthday Paradox code using arrays. I've posted the instructions bellow: The program The centerpiece of the code is a method,
Need help writing a java Birthday Paradox code using arrays. I've posted the instructions bellow:
The program The centerpiece of the code is a method, oneTrial, that, given a number of people as its parameter, randomly selects birthdays for those people and produces for each day how many people have selected that day as their birthdays. More formally, the method receives the number of people, nPeople, as its formal parameter and returns an array ofint whose length is 365. The method first instantiates an array of 365 elements, named theCounts. Then, it enters a for-loop that iterates 1,.. , nPeople with some iteration variable. In each iteration of the loop, the method selects a random integer between 0 and 364, and then increases the element of the array theCounts at the selected position by 1. After quitting the loop, the method returns the array theCounts. Since the elements of arrays of numbers are initialized with the value 0, explicit initialization is not needed After receiving the return value of oneTrial, we examine the elements of the array, looking for an element with the value greater than or equal to 2. Such an element indicates the existence of multiple people having the same birthdays. We write a method, hasAHt, for executing this search. The method receives an int array as its parameter and returns a boolean. Using a for-loop, themethod scans the elements of the array given as the formal parameter. On encountering an element whose value is greater than or equal to 2, the method immediately returns true, ignoring the remaining elements of the array. If this does not happen, the loop terminates. At that point, the method returns false. Using the above two methods, we write another method, experimentl. This method has two formal parameters. The first is an int named nPeople. This is the value to be passed to oneTrial as the number of people. The second is an int as well and is named nReps. This is the number of times to execute oneTrial. Use a for-loop to count the executions of oneTrial. After each execution, feed the return array of oneTrial to hasAHit. If hasAHit returns true, increase the value of a double variable, hitRate, by 1. The initial value of hitRate is 0. After completing the loop, we divide hitRate by nReps. The value of hitRate then becomes the proportion of the repetitions in which the random birthday selections generated multiple people having the same birthdays. The method experimentl reports this ratio before terminating. The main method receives the quantities for nPeople and nReps and calls experimentl with these two values as the actual parameters. To print the average, use "%.3f in printf so that exactly three digits appear after the decimal point. Here are some execution examples: % Java Birthday Enter the no. of people: 23 Enter the no. of repetitions: 100 Experiment 1 Probability Of Hits: 0.490
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
