BirthdayProblemDemo - and output the number of people who...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* How many random people do you have to select before you find a duplicate birthday (that is, two peole who were born on the same day of the same month, but not necessarily in the same year). This program simulates the process. (It ignores the possibility of people born on leap day.) */ * public class BirthdayProblemDemo { p public static void main(String[] args) { TextIO.putln("This program simulates taking people at random"); TextIO.putln("until two have been found who were born on the"); TextIO.putln("same day of the year.\n"); do { birthdayProblem(); TextIO.put("\nAgain? "); } while ( TextIO.getlnBoolean() ); TextIO.putln("\n\nOK. Goodbye."); } // end main() static void birthdayProblem() { // Simulate choosing people at random and checking the // day of the year they were born on. If the birthday // is the same as one that was seen previously, stop,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // and output the number of people who were checked. boolean used; // For recording the possible birthdays // that have been seen so far. A value // of true in used[i] means that a person // whose birthday is the i-th day of the // year has been found. int count; // The number of people who have been checked. used = new boolean[365]; // Initally, all entries are false. count = 0; while (true) { // Select a birthday at random, from 0 to 364. // If the birthday has already been used, quit. // Otherwise, record the birthday as used. int birthday; // The selected birthday. birthday = (int)(Math.random()*365); count++; if ( used[birthday] ) break; used[birthday] = true; } TextIO.putln("A duplicate birthday was found after " + count + " tries."); } // end birthdayProblem() } // end class BirthdayProblemDemo...
View Full Document

This note was uploaded on 01/15/2010 for the course CSC 115 taught by Professor Jackson during the Spring '09 term at University of Victoria.

Page1 / 2

BirthdayProblemDemo - and output the number of people who...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online