Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

How this works...Guess Birthday

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.
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];
    }
}

Open in new window

Avatar of WalterH
WalterH
Flag of United States of America image

The various sets correspond to the powers of 2. All of the dates in set one have a one bit in position 2**0 in their binary value, all of the dates in set two have a one bit in position 2**1 in their binary value and so on. The questions asked could be rephrased, does your birthday have a 1 in position n when expressed in binary. Once the 5 questions have been answered, the powers of 2 are added to give the actual value. Note that the first value in each set is the value of the power of 2 that is being identiied.
Avatar of Mike Eghtebas

ASKER

Thank you for the explanations. If you or someone else could take a day like 10 and show how the program figures out the answer that will be a grate help. Meanwhile I will read your post again and again to finally understand it fully.

Mike
10 = 2+8 or 2**1 + 2**3 - therefore day 10 will be listed in the groups starting with 2 and 8 and is listed in no other groups. If I convert 10 to binary, it is 01010. Looking at this from right to left, this tells me that it will be in groups 2 and 4 but not in groups 1, 3 or 5. Does this help? Note that the numbers 1-31 can always be expressed in binary using just 5 bits, thus the need for 5 groups to uniquely identify any number.
How about trying the date 18? If I convert that to binary it is 10010, so 18 will be listed in groups 2 and 5 and no other group. The number 10010 means 1* 2**4 + 0*2**3 + 0*2**2 + 0*2**2 + 1*2**1 + 0*2**0 which is 16+2 or 18.
Thanks for the detailed info. I am sure this will do it but I have to go through it to understand it.

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
If you still don't understand, I'm going to try to help you by applying learning theory.

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.
ASKER CERTIFIED SOLUTION
Avatar of WalterH
WalterH
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you