Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone break down this code for me? Aren't boolean values initialized as false by default? Why is the while loop set to run while
Can someone break down this code for me? Aren't boolean values initialized as false by default? Why is the while loop set to run while true but by default the boolean is false? Also if we break, if hasBirthday[d], wouldnt the hasBirthday[d] = true line not run since its breaking out of the loop. Wouldn't the loop never end since we never set anything to false?
public class Birthday ( public static void main(Stringl] args) int days-Integer.parseInt (args[0])/ number of days int people0 total number of people hasBirthday[d] true if someone born on day d; false otherwise /auto-initialized to false boolean[] hasBirthday = new boolean [days]; while (true) people+ int d-(int) (daysMath.random)) I/ integer between 0 and days-1 if (hasBirthday[d]) break; hasBirthday[d] -true; 7two people with the same birthday, so break out of loop update array System.out.println (people)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started