In: Computer Science
Programing Java
The school Arena can seat 12,000 people for an event. If the arena was full and you were to poll everyone for which day of the year (between 1 and 365, inclusive) they were born, determine which days had the most birthdays and which days had the fewest birthdays.
Write a program that will generate 12,000 random birthdays (integer numbers) between 1 and 365 and count how many people have that same birthday. Output 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).
Your code will have two classes:
Goals
Sample output
The following days have 75 people:
147
The following days have 35 people:
143 312
Program Code Screenshot
Sample Output
Program Code to Copy
import java.util.ArrayList; import java.util.List; import java.util.Random; class Arena{ int people[]; int capacity; Arena(int n){ capacity = n; //Create an array of birthdays people = new int[365]; //Generate random birthdates generate(); } void generate(){ Random r = new Random(); //Generate birthdays of all people for(int i=0;i<capacity;i++){ people[r.nextInt(365)]++; } } void minDays(){ //Find day on which there are less birthdays int mn = people[0]; List<Integer> mnl = new ArrayList<>(); mnl.add(1); for(int i=0;i<people.length;i++){ if(people[i]<mn){ mn = people[i]; mnl = new ArrayList<>(); mnl.add(i+1); } else if(people[i]==mn){ mnl.add(i+1); } } System.out.println("The following days have "+mn+" people:"); for(int x : mnl){ System.out.print(x+" "); } System.out.println(); } void maxDays(){ //Find the day on which there are most birthdays int mx = people[0]; List<Integer> mxl = new ArrayList<>(); mxl.add(1); for(int i=0;i<people.length;i++){ if(people[i]>mx){ mx = people[i]; mxl = new ArrayList<>(); mxl.add(i+1); } else if(people[i]==mx){ mxl.add(i+1); } } System.out.println("The following days have "+mx+" people:"); for(int x : mxl){ System.out.print(x+" "); } System.out.println(); } } class Main{ public static void main(String[] args) { Arena arena = new Arena(12000); arena.minDays(); arena.maxDays(); } }