Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

--------------------------------------------------------------------------------

ID: 3596288 • Letter: #

Question

-------------------------------------------------------------------------------------

SHELL:

Problem: Guessing birthday You can find out your friend's birthday by asking 5 questions. You program should provide 5 different sets of numbers. Then your program should ask the user if her his birthday appears in any of the given sets. Five different sets have been provided below For example, if your friend's birthday is on the 19th,then the value 19th is in the set 1, set 3 and set 5. Your friend's birthday will be the sum of the first number that appears in the sets 1, 3, and 5. Therefore, your friend's bithday is 1 +2+ 16-19 A Java file has been provided for you. For each method a description has been provided. You need to fill in the code for each method. Here are the sets of the numbers that you must use in your program: Set 1-, 3, 5,7,9, 11, 13, 15, 17, 19, 21, 23, 25, 27,29,31) Set 2 (2, 3, 6,7, 10, 11, 14, 15, 18, 19, 22, 23, 26,27, 30, 31) Set 3 -(4, 5, 6,7,12, 13, 14, 15, 20, 21, 22, 23, 28, 29,30, 31) Set 4 - (8,9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29,30, 31) Set 5 (16, 17, 18, 19,20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31) Sample output: Welcome to Birthday Guessing webaite by your name You will be prompted to answer five questions Then computer wil1 guess your birthday How many times do you to use this software? 3

Explanation / Answer

Hi,

Below is the outcome and code for it. For sample run, I took Mary's birthday as 21 and Alex's bday as 16.

Kindly let me know if you have any doubts! As the outcome was too big, couldn't attach its sceenshot!

Outcome:

****************************************************

Welcome to Birthday Guessing website by your name

You will be prompted to answer five questions

The computer will guess your birthday

****************************************************

How many times do you want to use this software?

3

What is your name?Mary

Hi Mary answer the following questions so I can guess your birthday.

Is your birthday in this set?

1 3 5 7

9 11 13 15

17 19 21 23

25 27 29 31

Enter yes or no answers: yes

Is your birthday in this set?

2 3 6 7

10 11 14 15

18 19 22 23

26 27 30 31

Enter yes or no answers: no

Is your birthday in this set?

4 5 6 7

12 13 14 15

20 21 22 23

28 29 30 31

Enter yes or no answers: yes

Is your birthday in this set?

8 9 10 11

12 13 14 15

24 25 26 27

28 29 30 31

Enter yes or no answers: no

Is your birthday in this set?

16 17 18 19

20 21 22 23

24 25 26 27

28 29 30 31

Enter yes or no answers: yes

Your birthday is on the: 21

Did I guess it right?yes/no: yes

Yesss I guessed it right !!!

Hit enter to try again.

What is your name?Alex

Hi Alex answer the following questions so I can guess your birthday.

Is your birthday in this set?

1 3 5 7

9 11 13 15

17 19 21 23

25 27 29 31

Enter yes or no answers: yes

Is your birthday in this set?

2 3 6 7

10 11 14 15

18 19 22 23

26 27 30 31

Enter yes or no answers: yes

Is your birthday in this set?

4 5 6 7

12 13 14 15

20 21 22 23

28 29 30 31

Enter yes or no answers: no

Is your birthday in this set?

8 9 10 11

12 13 14 15

24 25 26 27

28 29 30 31

Enter yes or no answers: yes

Is your birthday in this set?

16 17 18 19

20 21 22 23

24 25 26 27

28 29 30 31

Enter yes or no answers: no

Your birthday is on the: 11

Did I guess it right?yes/no: no

You must have entered wrong answers. Try again.

Hit enter to try again.

What is your name?Alex

Hi Alex answer the following questions so I can guess your birthday.

Is your birthday in this set?

1 3 5 7

9 11 13 15

17 19 21 23

25 27 29 31

Enter yes or no answers: no

Is your birthday in this set?

2 3 6 7

10 11 14 15

18 19 22 23

26 27 30 31

Enter yes or no answers: no

Is your birthday in this set?

4 5 6 7

12 13 14 15

20 21 22 23

28 29 30 31

Enter yes or no answers: no

Is your birthday in this set?

8 9 10 11

12 13 14 15

24 25 26 27

28 29 30 31

Enter yes or no answers: no

Is your birthday in this set?

16 17 18 19

20 21 22 23

24 25 26 27

28 29 30 31

Enter yes or no answers: yes

Your birthday is on the: 16

Did I guess it right?yes/no: yes

Yesss I guessed it right !!!

Good bye

Code:

import java.util.Scanner;

public class BirthdayShell

{

/*create a Scanner object

Call the method description

ask the user how many times to repeat this program

create a loop

call the method play*/

public static void main(String[] args )

{

Scanner sc=new Scanner(System.in);

description();

System.out.println("How many times do you want to use this software?");

int n=Integer.parseInt(sc.nextLine());

for(int i=0;i<n;i++) {

if(i!=0) {

System.out.println(" Hit enter to try again.");

sc.nextLine();

}

play(sc);

}

System.out.println("Good bye");

}

/*Since this method calls all the other methods, it must be implemented last.

This method asks the user's name followed by asking five questions .

To ask each question you must call the method askedQuestion by passing two parameters to it,

this method will return an integer value that you will be adding it to the variable called birthday.

At the end output the variable birthday and messages similar to the provided output.

*/

public static void play(Scanner kb)

{

int birthday=0;

System.out.print("What is your name?");

String name= kb.nextLine();

System.out.println("Hi "+ name+ " answer the following questions so I can guess your birthday.");

birthday+=askQuestion(set1(),kb);

birthday+=askQuestion(set2(),kb);

birthday+=askQuestion(set3(),kb);

birthday+=askQuestion(set4(),kb);

birthday+=askQuestion(set5(),kb);

System.out.println("Your birthday is on the: "+birthday);

System.out.print("Did I guess it right?yes/no: ");

String yes= kb.nextLine();

if(yes.equalsIgnoreCase("yes")) {

System.out.print("Yesss I guessed it right !!! ");

}else {

System.out.print("You must have entered wrong answers. Try again. ");

}

}

/*The input parameter is the given set, and a Scanner object

This method outputs the set on the screen and ask the user if the

birthday is in the given set, if it is this method

returns the first number in the set otherwise it will return zero.  

remember that each set is stored as a String, to get the first number in the set,

you must get the first token in the set and convert it to Integer. for example if String s = "19"

then we can convert this string to an Integer with the following code int a = Integer.parseInt(s)

*/

public static int askQuestion(String set, Scanner kb )

{

System.out.println(" Is your birthday in this set? ");

  

System.out.println(set);

System.out.print("Enter yes or no answers: ");

String userInput=kb.nextLine();

if(userInput.equalsIgnoreCase("yes")) {

int index=set.indexOf(' ');

return Integer.parseInt(set.substring(0, index));

}

else

return 0;

}  

/*this method creates a String representing the set 1 and returns the string that was cretaed

look at the sample output to cretae the string. It must be exactly like the provided output*/   

public static String set1()

{

return "1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 ";

}

/*This method cretaes the set 2*/

public static String set2()

{

return "2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 ";

}

/*This method cretaes the set 3*/

public static String set3()

{  

return "4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 ";

}

/*This method cretaes the set 4*/

public static String set4()

{   

return "8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 ";

}

/*this method cretaes the set 5*/

public static String set5()

{   

return "16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ";

}

/*this method outputs the description of this program*/

public static void description()

{

System.out.println("****************************************************");

System.out.println("Welcome to Birthday Guessing website by your name");

System.out.println("You will be prompted to answer five questions");

System.out.println("The computer will guess your birthday");

System.out.println("****************************************************");

}   

  

}