Mike Eghtebas
asked on
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}};
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];
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This means I have to study the code and understand it first. Having done this, possibly, I could see the solution.
Thanks
Thanks
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My problem was not understanding binary system application in this question explained at:
https://www.experts-exchange.com/questions/27409196/How-this-works-Guess-Birthday.html
Thank you for the responses.
Mike
https://www.experts-exchange.com/questions/27409196/How-this-works-Guess-Birthday.html
Thank you for the responses.
Mike
Open in new window
Open in new window