# The Birthday Problem in Java

UAE

Everyone must have heard of the famous "Birthday Problem" (Can refer to Wikipedia). I need to write a method (type of double) for it in Java which takes 2 parameters as the "size" and "count". I got a code from somewhere but it does not work on the grader.

public double calculate(int size, int count)

I need to get this method working.

I have tried the following code, but no success.

public double calculate(int size, int count){
double x[] = new double[size];
int match = 0;
for(int i=0;i<count;i++){
for(int j=0;j<size;j++){
x[j] = (int)(Math.random()*365)+1;
}
for(int j=0;j<size;j++){
for(int k=j+1;k<size;k++){
if(x[j]==x[k]){
match++;
}
}}}
double prob = ((double)match/count)*100;
return prob;
}

As I am new student of CS, detailed explanation would be highly appreciated. (This is for my android app development course)

• Budapest

I am a beginner, too, also I'm not sure what a grader is so don't expect too much
What I'm missing from the code is the main method calling the calculate method, and the whole code should be inside a public class, e.g.:

public class Calc {

public static void main(String[] args){
Calc myCalc = new Calc();
System.out.println(myCalc.calculate(23,3));
}

}

• India

Interesting observation.. It would be helpful if @itsabk can link the associated wiki page here

@Louis_Hon said:
I am a beginner, too, also I'm not sure what a grader is so don't expect too much
What I'm missing from the code is the main method calling the calculate method, and the whole code should be inside a public class, e.g.:

public class Calc {

public static void main(String[] args){
Calc myCalc = new Calc();
System.out.println(myCalc.calculate(23,3));
}

}

• India

For others: Birthday Problem aims to calculate the probability that two people in the room have same birthday, maximum being 365 people (because at 366, assuming each one has separate birthdays, we will still have at least one pair of matching birthdays).

Have marked the purpose of each statement.

@itsabk said:
Everyone must have heard of the famous "Birthday Problem" (Can refer to Wikipedia). I need to write a method (type of double) for it in Java which takes 2 parameters as the "size" and "count". I got a code from somewhere but it does not work on the grader.

public double calculate(int size, int count)

I need to get this method working.

I have tried the following code, but no success.

public double calculate(int size, int count){
double x[] = new double[size];
int match = 0;
for(int i=0;i<count;i++){

// 1ST INNER LOOP - To add random numbers into the array each indicating
// that a person was born on this day

for(int j=0;j<size;j++){
x[j] = (int)(Math.random()*365)+1;
}

**// 2ND INNER LOOP - this method will check if any paid of random numbers are
// matching with each other **

for(int j=0;j<size;j++){
for(int k=j+1;k<size;k++){
if(x[j]==x[k]){
match++;
}
}}}

**// THIS statement calculates the probability in percentage **

double prob = ((double)match/count)*100;
return prob;
}

As I am new student of CS, detailed explanation would be highly appreciated. (This is for my android app development course)