date[][][] to set1[][], set2[][]....

With 9 in mind, we let the program guess this birthday. Responding to five questions when the user is prompted, s/he enters 1 for yes and 0 for no.

run:
Is your birthday in set1?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31

Enter 0 for No and 1 for Yes:1
Is your birthday in set2?
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31

Enter 0 for No and 1 for Yes:0
Is your birthday in set3?
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31

Enter 0 for No and 1 for Yes:0
Is your birthday in set4?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31

Enter 0 for No and 1 for Yes:1
Is your birthday in set5?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31

Enter 0 for No and 1 for Yes:0
Your birthday is 9
BUILD SUCCESSFUL (total time: 26 seconds)
==========
The attached code works and the correct birthday is guessed successfully..

Question: How can I change the code in the main() section such that it will still work after I replace the date[][][] array with five set1[][], set2[][], set3[][], set4[][]. and set5[][]:

    private static int [][]set1={
         {1,    3,  5,  7},
         {9,    11, 13, 15},
         {17,   19, 21, 23},
         {25,   27, 29, 31}};
    private static int [][]set2={
         {2,    3,  6,  7},
         {10,   11, 14, 15},
         {18,   19, 22, 23},
         {26,   27, 30, 31}};
    private static int [][]set3={
         {4,    5,  6,  7},
         {12,   13, 14, 15},
         {20,   21, 22, 23},
         {28,   29, 30, 31}};
    private static int [][]set4={
         {8,    9,  10, 11},
         {12,   13, 14, 15},
         {24,   25, 26, 27},
         {28,   29, 30, 31}};
    private static int [][]dates={
         {16,   17, 18, 19},
         {20,   21, 22, 23},
         {24,   25, 26, 27},
         {28,   29, 30, 31}};
               

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+"\n");
         }
}

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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I think you want to replace thie method get value with this:

 public static int getValue(int setNo, int k, int j){
    if(setNo == 0)
        return set1[k][j];
 if(setNo == 1)
        return set2[k][j];
 if(setNo == 2)
        return set3[k][j];
 if(setNo == 3)
        return set4[k][j];
 if(setNo == 4)
        return set5[k][j];





    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
This works for me:

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+"\n");
         }
}

class GuessDate{
    private static int [][]set1={
            {1,    3,  5,  7},
            {9,    11, 13, 15},
            {17,   19, 21, 23},
            {25,   27, 29, 31}};
       private static int [][]set2={
            {2,    3,  6,  7},
            {10,   11, 14, 15},
            {18,   19, 22, 23},
            {26,   27, 30, 31}};
       private static int [][]set3={
            {4,    5,  6,  7},
            {12,   13, 14, 15},
            {20,   21, 22, 23},
            {28,   29, 30, 31}};
       private static int [][]set4={
            {8,    9,  10, 11},
            {12,   13, 14, 15},
            {24,   25, 26, 27},
            {28,   29, 30, 31}};
       private static int [][]set5={
            {16,   17, 18, 19},
            {20,   21, 22, 23},
            {24,   25, 26, 27},
            {28,   29, 30, 31}};
       /*

    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){
    if(setNo == 0)
        return set1[k][j];
 if(setNo == 1)
        return set2[k][j];
 if(setNo == 2)
        return set3[k][j];
 if(setNo == 3)
        return set4[k][j];
 if(setNo == 4)
        return set5[k][j];



     return 0;

    }
}

Open in new window


Is your birthday in set1?
1 3 5 7 
9 11 13 15 
17 19 21 23 
25 27 29 31 

Enter 0 for No and 1 for Yes:1
Is your birthday in set2?
2 3 6 7 
10 11 14 15 
18 19 22 23 
26 27 30 31 

Enter 0 for No and 1 for Yes:1
Is your birthday in set3?
4 5 6 7 
12 13 14 15 
20 21 22 23 
28 29 30 31 

Enter 0 for No and 1 for Yes:0
Is your birthday in set4?
8 9 10 11 
12 13 14 15 
24 25 26 27 
28 29 30 31 

Enter 0 for No and 1 for Yes:0
Is your birthday in set5?
16 17 18 19 
20 21 22 23 
24 25 26 27 
28 29 30 31 

Enter 0 for No and 1 for Yes:1
Your birthday is 19

Open in new window

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
This means I have to study the code and understand it first. Having done this, possibly, I could see the solution.

Thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

for_yanCommented:
But it is all in that method - you just need to change it teh way I suggested in the beginning,
and jsut aadd return 0; at the very end, otheriwse comiler would compalain - and then it works -  no changes
in the main class are required
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Just as an aside, an even easier way would be to not use arrays at all, as in the following...

 
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 = 1; j <= 31; j++) {
                if ((j & (1 << i)) != 0) {
                    System.out.print(j + " ");
                }
            }
            System.out.println();

            System.out.print("\nEnter 0 for No and 1 for Yes:");
            answer = input.nextInt();

            date += (answer << i);
        }

        System.out.print("Your birthday is " + date + "\n");
    }
}

Open in new window

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
My problem was not understanding binary system application in this question explained at:

http://www.experts-exchange.com/Programming/Languages/Java/Q_27409196.html

Thank you for the responses.

Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.