Solved

Posted on 2011-10-21

User is shown set of days and asked to enter 1 if her/his birthday is in the list and enter 0 if not.

The process is repeated 5 time each time for different list of days. Then, the program guesses what the birthday is.

Please add comments and/or explain how this works.

Thank you.

The process is repeated 5 time each time for different list of days. Then, the program guesses what the birthday is.

Please add comments and/or explain how this works.

Thank you.

```
import java.util.Scanner;
public class UseGuessDateClass{
public static void main(String[] args){
int date=0;
int answer;
Scanner input=new Scanner(System.in);
for (int i=0;i<5;i++){
System.out.println("Is your birthday in set"+(i+1)+"?");
for(int j=0;j<4;j++){
for(int k=0;k<4;k++)
System.out.print(GuessDate.getValue(i,j,k)+" ");
System.out.println();
}
System.out.print("\nEnter 0 for No and 1 for Yes:");
answer=input.nextInt();
if(answer==1)
date+=GuessDate.getValue(i,0,0);
}
System.out.print("Your birthday is "+date);
}
}
class GuessDate{
private final static int [][][]dates={
{{1, 3, 5, 7},
{9, 11, 13, 15},
{17, 19, 21, 23},
{25, 27, 29, 31}},
{{2, 3, 6, 7},
{10, 11, 14, 15},
{18, 19, 22, 23},
{26, 27, 30, 31}},
{{4, 5, 6, 7},
{12, 13, 14, 15},
{20, 21, 22, 23},
{28, 29, 30, 31}},
{{8, 9, 10, 11},
{12, 13, 14, 15},
{24, 25, 26, 27},
{28, 29, 30, 31}},
{{16, 17, 18, 19},
{20, 21, 22, 23},
{24, 25, 26, 27},
{28, 29, 30, 31}}};
private GuessDate(){
}
public static int getValue(int setNo, int k, int j){
return dates[setNo][k][j];
}
}
```

8 Comments

Mike

Question: What is the criteria in designing these groups. Number of group 5 required you have explained. But if we were to guess 1 to 50, for example, how do we determine the numbers in each group?

This is very exiting and new to me.

Mike

Write 0 through 31 down on a piece of paper in both decimal and binary.

00 00000

01 00001

02 00010

03 00011

04 00100

...

31 11111

Now write down all the decimal numbers that have a 1 in the right most bit of the binary number in a row. (These are also all odd numbers).

Then write down all the decimal numbers that have a 1 in the 2nd most right bit of the binary number (also in a row).

Repeat for all 5 digits.

Compare the sets you wrote to the sets in your program.

Hopefully, if you learning style is visual, read/write or kinesthetic, you'll "get it" now. If your learning style is auditory (which is unlikely for programmers), perhaps you can have someone read it to you.

00000 = 0

00001 = 1 ( I am in Group 1 )

00010 = 2 ( I am in Group 2 )

00011 = 3 ( I am in Groups 1 and 2 )

00100 = 4 ( I am in Group 3)

00101 = 5 ( I am in Group 1 and 3)

00110 = 6 ( I am in Group 2 and 3)

00111 = 7 ( I am in Group 1, 2 and 3)

01000 = 8 ( I am in Group 4)

01001 = 9 ( I am in Group 4 and 1)

We can go as high as we like and the number of groups is always equal to the power of 2 above the highest number you need to choose. Each time the person answers the question 0 or 1 they are eliminiating 1/2 of the possible values.

This problem is logically equivalent to something called binary search. If you have a large ordered list, you can search very quickly if you can go to the middle and decide if what you want is above or below the value in the middle. You then take the list that is left and repeat by going to its middle. You examine a very small fraction of the items in the list to find your match.

Title | # Comments | Views | Activity |
---|---|---|---|

sameEnds challenge | 25 | 57 | |

factory design pattern vs abstract factoy design pattern | 2 | 63 | |

wordsFront challenge | 8 | 48 | |

triangle challenge | 4 | 38 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!