Question: This assignment is intended to provide practice with arrays, loops and conditionals (if-else statements), all three of which are core topics in CS-1111. The assignment
This assignment is intended to provide practice with arrays, loops and conditionals (if-else statements), all three of which are core topics in CS-1111. The assignment has two parts:
Part I: Consider a group of 10 random people. What are the chances that the group has two people that share a birthday (same date but not year). Thus, if say the 2nd person and 7th person in the group both have birthdays on March 15, then the group has such a pair. Now, it could be that three people share a birthday, which still satisfies the property (at least two people share a birthday. The goal is to evaluate the probability that a group of N people has at least two people that share a birthday.
Part II: This is a slight variation of the above problem in which we want to see if a group of N people have birthdays on successive days. Thus, we could have a shared birthday if there are two people with birthdays on March 14 and March 15, or if the group has two people with birthdays on December 31 and January 1.
Some hints for Part I (Part II is similar enough): To estimate a probability, you are going to generate thousands of random groups (of N people). Suppose we generate 1000 groups with which to test. In each group that you generate, you will check to see whether the group has a shared birthday. Then divide the number of cases with a shared birthday by 1000, and that gives a probability. For example, if 151 groups have a shared birthday, the probability is 151/1000 = 0.151. How do you generate a group of N birthdays randomly? Assume 365 days in a year and think about using an array. We have provided a random integer generator for you. It works like this. When you call the method random(K). with an integer K, the method makes a random number between 0 and K-1 and returns that.
Write all your code in BirthdayProblems.java : // YOUR NAME: import java.util.*; public class BirthdayProblems { public static void main (String[] argv) { // This is there just for you to compile and run, to see // how a random number generator is used. testRandom (); int N = 10; double p = sharedBirthdayProbability (N); System.out.println (p); // Experiment with N to see what value of N gets p closest to 0.5. int M = 10; double q = consecutiveBirthdayProbability (M); System.out.println (q); // Experiment with M to see what value of M gets q closest to 0.5. } static double sharedBirthdayProbability (int N) { // WRITE YOUR CODE HERE. // A particular case or instance is when N people have // been assigned birthdays randomly. We are interested in // whether there are any two people with a shared birthday. // Make 1000 such cases and identify in how many cases you find // two people with a shared birthday. For example, if in 151 cases // you find such shared birthdays, then the probability of a // shared birthday among N people is 151/1000 = 0.151 (which is // what you return). Assume 365 days in a year. // Temporarily (just so that it will compile): return 0; } static double consecutiveBirthdayProbability (int N) { // WRITE YOUR CODE HERE. // A slight variation of the shared birthday problem is to look // for cases where there exist two people have birthdays // on consecutive days. You also have to account for two people who // could have birthdays on the first and last days of the year. // Temporarily: return 0; } static void testRandom () { // Make 10 random choices picking between 0 and 5. for (int i=0; i<10; i++) { int k = random (6); // If we want between 0 and 5 inclusive. System.out.println (k); } } static int random (int K) { // Return a random integer between 0 to K-1 (0 and K-1 inclusive). Random rand = new Random (); int m = rand.nextInt (K); return m; } }
which has everything you need including the random(K). method. Write your name at the top of the program in a comment. As mentioned above, write your code in the file BirthdayProblems.java. Then, play around with N (change it and re-run) to answer the following questions: What is the shared-birthday probability for N=10 people? What is the smallest value of N where the probability is over 0.5? What is consecutive birthday probability for M=10 people? What is the smallest value of M where the probability is over 0.5? Answer these questions in a PDF or Word document. Put your name at the top of the document.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
