Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

On this lab, you will be creating a Java program that asks the user their birthday (month, day, and year) and computes the birthday in

On this lab, you will be creating a Java program that asks the user their birthday (month, day, and year) and computes the birthday in Unix Epoch time format. The program will then ask the user for a number of students, and will proceed to generate and display that many random dates from the users birth year, highlighting any collisions/duplicates. The program will also display the approximate probability of a repeated birthday in a class that size.

If the user ever enters a bad input (e.g., invalid date, negative number of students, etc), then the program should immediately stop with a goodbye message.

all methods should be public and static. You may not write any additional classes for this lab, and you may not use any existing Java classes that deal with dates and times. You should only have import statements for Random, Scanner, and possibly Math.

You should store a date in an integer array of size 3. Index 0 of the array will contain the month number, index 1 the day number, and index 2 the year. The methods described below should all assume this date format. You should write and correctly use the following methods:

Let p(n) be the probability that two or more students in a random collection of n have the same birthday. It is possible to exactly compute p(n), but a good approximation that we will use on. ()1^((1)/730)

The program, when executed, should welcome the user to the Birthday Fun program and separately ask for the users birth month, day, and year. The program should use the Scanner class to read in these three inputs as integers. If the date entered is not a valid date (i.e., a date that did not occur or will not occur), the program should tell the user that and then immediately exit without any further interaction.

If the date entered is valid, the program should print what 12:00:00am UTC on that day is when converted to Unix Epoch time, which is the number of seconds elapsed since 12:00:00am UTC on January 1, 1970. Dates before 1970 should result in negative numbers.

Next, the program should ask the user to enter a number of students and again use Scanner to read in this integer. If the user enters any number less than 1, the program should tell the user this is invalid and immediately terminate. If the user enters a positive integer, then the program should generate that many random birthdays from the users birth year. Each day of the users birth year should be equally likely, so if the birth year is a leap year then every day (including February 29) should have a 1/366 chance of being chosen each time, while if the birth year is not a leap year each day should have a 1/365 chance of being chosen.

The program should then print out the randomly generated dates, one per line, in the format month/day/year. Every date that is repeated (meaning two students in the class have the same birthday) should have the characters --> in front of it (see examples below) to indicate to the user that a collision occurred. The program should also print out an approximation of the probability of a collision with that many birthdays, using the approximation formula above.

-isLeapYear should take a year as input and return boolean true if it is a leap year and false otherwise.

-isValidDate should take a date (integer array of size 3) as input and return boolean true if the date is valid and false otherwise.

-nextDay should take a date and return the next valid date

-compareDates should take two dates, date1 and date2, and return -1 if date1 comes before date2, return 1 if date1 comes after date2, and return 0 if date1 is the same date as date2.

-dateToDayNumber should take a date as input and return which day of the year that date was (i.e., a number between 1 and 366).

-dayNumberToDate should take a day number (1-366) and a year and return which date that day number corresponds to. For example, dayNumberToDate(32, 2018) should return array [2,1,2018], since Feb. 1 was the 32nd day of 2018.

-daysBetweenDates should take two dates as input and return how many days elapse from the start of the first date to the start of the second date. This function will be helpful for computing Epoch time, since there are a fixed number of seconds in a day.

-dateToEpochTime should take a date (integer array of size 3) as input and return a long representing 00:00:00 UTC on that date converted to epoch time. This function needs to return a long to avoid the "Year 2038 Problem".

-computeApproximation should take a number of students as input and return a double with an estimate of the probability of a collision in birthdays with that many students, using the formula given above.

-dateToString should take a date (array of size 3) and return a string representation of the date in the form month/day/year.

-randomDate should take a year as input and return a random date from that year, with each day equally likely. This function will be useful in writing generateBirthdayList.

-generateBirthdayList should take a number of students n and a birth year and return an array of n random birthdays from that year. Since a birthday is itself an array of size 3, this returned array should actually be a 2d array.

-countDate should take an array of dates and a specific date and return how many times the specific date occurs in the array. This function will help to detect duplicate dates in a class.

An array with the number of days in each month may be useful in some of the above methods. Also, the nextDay function is especially helpful for some of the more complicated methods.

Output:

Welcome to Birthday Fun!

Enter your birth month: 5

Enter your birth day: 25

Enter your birth year: 1990

Your birthday in Unix Epoch time is 643593600

Enter a number of students: 25

There is about a 56.04121995072768% chance of a collision

Generating random birthdays from 1990...

12/16/1990

6/6/1990

4/16/1990

5/31/1990

--> 6/15/1990

11/9/1990

4/11/1990

2/22/1990

2/23/1990

12/17/1990

6/27/1990

8/10/1990

2/6/1990

11/23/1990

8/4/1990

3/10/1990

6/7/1990

10/31/1990

3/27/1990

6/4/1990

--> 6/15/1990

9/30/1990

1/6/1990

12/26/1990

4/28/1990

Welcome to Birthday Fun!

Enter your birth month: 2

Enter your birth day: 31

Enter your birth year: 1990

You entered an invalid birthday.

Goodbye.

Welcome to Birthday Fun!

Enter your birth month: 2

Enter your birth day: 16

Enter your birth year: 2000

Your birthday in Unix Epoch time is 950659200

Enter a number of students: 30

There is about a 69.63200177219244% chance of a collision

Generating random birthdays from 2000...

6/19/2000

9/17/2000

8/30/2000

12/14/2000

--> 9/3/2000

4/22/2000

10/13/2000

7/5/2000

--> 9/3/2000

5/19/2000

12/18/2000

3/15/2000

3/16/2000

4/13/2000

7/19/2000

4/15/2000

6/4/2000

9/14/2000

3/7/2000

7/27/2000

4/7/2000

6/8/2000

5/10/2000

12/4/2000

8/6/2000

--> 9/3/2000

3/10/2000

6/1/2000

12/31/2000

10/28/2000

Welcome to Birthday Fun!

Enter your birth month: 1

Enter your birth day: 5

Enter your birth year: 1970

Your birthday in Unix Epoch time is 345600

Enter a number of students: 3

There is about a 0.8185492989120857% chance of a collision

Generating random birthdays from 1970...

1/29/1970

3/4/1970

2/7/1970

Welcome to Birthday Fun!

Enter your birth month: 11

Enter your birth day: 29

Enter your birth year: 1890

Your birthday in Unix Epoch time is -2495836800

Enter a number of students: 1

There is about 0.0 % chance of collision

Generating random birthdays from 1980...

11/28/1890

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the concept of the first,follow, and predict sets

Answered: 1 week ago