Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

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

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
  • 2
2 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now