[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How this works...Guess Birthday

Posted on 2011-10-21
8
Medium Priority
?
1,305 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Mike Eghtebas
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:WalterH
ID: 37007884
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.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37008928
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
0
 
LVL 5

Expert Comment

by:WalterH
ID: 37009108
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:WalterH
ID: 37009151
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.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37009322
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
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37009346
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.
0
 
LVL 5

Accepted Solution

by:
WalterH earned 2000 total points
ID: 37009388
You need to understand binary numbers to understand the problem. If you have 5 bits, the numbers go from 0 to 2**5-1 assuming the number is unsigned. 2**5 (that is 2 raised to the power 5) is 32, so any number from 0 to 31 can be expressed in 5 bits. the next power (32*2) is 64 so with 6 bits we can express any number from 0 to 63. Try counting in binary and looking at the values. I will give you a few
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.
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 37009647
thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question