Creating a Birthday Application in Java

Problem:

Assume that each month consists of 30 days. One year, then, consists of 30×12 = 360 days, i.e., there are 360 different possible birthdays. Write three programs named Problem3a.java, Problem3b.java and Problem3c.java to answer the questions below. Each of your programs should simulate choosing people at random and checking their birthdays.

a. In Problem3a.java, generate 100 birthdays at random (a number from 0 to 359), and store them in an array of size 100. The program asks the user to enter the current date (day [any number between 1 and 30] and month [any number between 1 and 12]) and determines whether any of the people have their birthday today (i.e. on the entered date). If so, display a Happy Birthday message to the found person; otherwise display the message “No one has his birthday today”. Example: if person 14 was found to have his birthday on the date entered by the user, the program displays the message: Happy Birthday to P14!

b. In Problem3b.java, you have to find how many random people you have to select before you find three people who share the same birthday? To answer this question, you have to generate birthdays at random (a number from 0 to 359) and keep track of how many people have been found with each birthday. An algorithm for this program is shown below:

Let count = 0 //count the number of people/birthdays generated

Repeat:

Select a birthday at random

Add one to count

Add one to the number of times that birthday has been found

If this is the third time that birthday has occurred:

break out of the loop

Output the count

c. In Problem3c.java, you have to find out how many different people you have to check before you find at least one person with a birthday on each of the 360 days of the year?


Output:

Not needed.

Solution:

/** ----------------------------Problem3a--------------------------- */
import java.util.*;
import java.math.*;
import java.util.Random;
public class Problem3a 
{
 public static void main(String args[])
 {
  int a[] = new int[100];
  int found = -1;
  
  Random ran = new Random();
  Scanner scan = new Scanner(System.in);
  
  for(int i = 0; i < 100; i++)
  {
   a[i] = ran.nextInt(360);
   //System.out.println(a[i]); 
//If you want to print the array of the random numbers generated
  }
  
  System.out.println("Enter day:");
  int day = scan.nextInt();
  System.out.println("Enter month:");
  int month = scan.nextInt();
  
  int val = 30 * (month - 1) + (day - 1);
  
  for(int i = 0; i < 100; i++)
  {
   if(a[i] == val)
   {
    found = i;
    break;
   }
  }
  
  if(found == -1)
  {
   System.out.println("No one has his birthday today");
  }
  else
  {
   System.out.println("Happy birthday P" + found);
  }
 }
    
}
/** ----------------------------Problem3b--------------------------- */
import java.util.*;
import java.math.*;
import java.util.Random;


public class Problem3b 
{
 public static void main(String args[])
 {
  int a[] = new int[360];
  int count = 0;
  Random ran = new Random();
  
  for(;;)
  {
   int r = ran.nextInt(360);
   count++;
   a[r]++;
   if(a[r] >= 3)
   {
    break;
   }
  }
  
  System.out.println("The count is: " + count);
 }  
}

/** ----------------------------Problem3c--------------------------- */
import java.util.*;
import java.math.*;
import java.util.Random;


public class Problem3c 
{
 public static void main(String args[])
 {
  int a[] = new int[360];
  int count = 0;
  Random ran = new Random();
  int done = 0;
  
  for(;;)
  {
   int r = ran.nextInt(360);
   count++;
   
   if(a[r] == 0)
    done++;
    
   a[r]++;
   
   if(done == 360)
    break; 
  }
  
  System.out.println("The count is: " + count);
 }  
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact