I\'m in java one and I need help with this program. PLease follow the directions
ID: 3678689 • Letter: I
Question
I'm in java one and I need help with this program. PLease follow the directions and test you program to make sure it works as required( the code should print from day 1 through day 365 and then determine which days had the most birthdays and which days had the fewest birthdays .Thank you
The CenturyLink Center can seat 18,975 people for an event. If the arena was full and you were to poll everyone for which day of the year (1-365) they were born, determine which days had the most birthdays and which days had the fewest birthdays.
Write a program that will generate 18,975 random birthdays (numbers) between 1 and 365 and count how many people have that same birthday. Output a display of each day of the year (1 – 365) followed by the number of people with that birthday, and then a listing of the days that have the most birthdays and the days that have the fewest birthdays. You do not need to convert a number like 32 to an actual date (February 1st).
Goals
Use your knowledge of arrays and random number generation to solve a problem.
Use looping structures to determine the largest and smallest values in an array and then find each occurrence of that
value.
Points to Think About
- A solution with just a main() method would likely contain five looping structures. The first would fill an array of counters based on random numbers between 1 and 365. The next two would first identify the largest value (count) stored in the array and then go back through the array and identify the positions (days) that contained that value, outputting the positions. The last two would first identify the smallest value (count) stored in the array and then go back through the array and identify the positions (days) that contained that value, outputting the positions.
- You may not use any material from beyond chapter 6 of the Deitel book.
- You don’t need to use methods for this program, though if you’d like to practice you certainly can. It’s just as easy to write the code for this particular assignment in a slightly larger main method as it is to pass an array around to various methods.
sample program run
Days 1 through 349 of the output omitted for brevity ...( the code should print from day 1: # people through day 365: # people)
Day 350 : 43 people Day 351 : 42 people Day 352 : 50 people Day 353 : 52 people Day 354 : 48 people Day 355 : 68 people Day 356 : 41 people Day 357 : 40 people Day 358 : 41 people Day 359 : 46 people Day 360 : 60 people Day 361 : 42 people Day 362 : 65 people Day 363 : 57 people Day 364 : 54 people Day 365 : 48 people
The following days have 75 people: 147
The following days have 35 people: 143 312
Explanation / Answer
import java.util.Random;
public class BirthDay {
public static void main(String[] args) {
int birthday[] = new int[366];
Random r = new Random();
// filling array with random birthday count
for(int i=1; i<=18975 ; i++){
int rNum = r.nextInt(365) + 1 ; // random number [1-365]
birthday[rNum]++;
}
// finding max value
int max = birthday[1]; // initializing with first element
for(int i=2; i<=365; i++){
if(max < birthday[i])
max = birthday[i];
}
// printing day number with max bithday count
System.out.print("The following days have "+max+" people: ");
for(int i=1; i<=365; i++){
if( max == birthday[i])
System.out.print(i+" ");
}
System.out.println();
// finding min value
int min = birthday[1]; // initializing with first element
for(int i=2; i<=365; i++){
if(min > birthday[i])
min = birthday[i];
}
System.out.print("The following days have "+min+" people: ");
for(int i=1; i<=365; i++){
if( min == birthday[i])
System.out.print(i+" ");
}
}
}
/*
Output:
The following days have 76 people: 258
The following days have 34 people: 21 196
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.