--------------------------------------------------------------------------------
ID: 3595749 • 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? 3Explanation / Answer
I hope that you know about string tokenizer class as it will be used to create tokens of strings which we are parsing to return int from the method askquestion.
If not please do a little bit research about it as it is used in the following answer.
Please do upvote and leave a nice comment.Thank u
// Header information goes here
import java.util.Scanner;
import java.util.StringTokenizer;
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 in =new Scanner(System.in);
description();
System.out.println("How many times do you want to use this software?");
int t=in.nextInt();
while(t-->0)
{
play(in);
if(t>0)
System.out.println("Hit enter to play again");
}
}
/*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.println("What is your name?");
String s=kb.next();
System.out.println("Hi "+s+ " 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:"+birthday);
System.out.println("Did i guess it right? yes/no:");
String a=kb.next();
if(a.equalsIgnoreCase("yes"))
System.out.println("Yessssss i guessed it right!!!");
if(a.equalsIgnoreCase("no"))
System.out.println("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();
System.out.println(set);
System.out.println("Enter yes or no answers");
System.out.println();
String s=kb.next();
StringTokenizer st=new StringTokenizer(set);
if(s.equalsIgnoreCase("yes"))
return Integer.parseInt(st.nextToken());
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()
{
int[] set1= {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31};
String s="";
for(int i=1;i<=set1.length;i++)
{
s=s+set1[i-1]+" ";
if(i%4==0 )
s+=' ';
}
return s;
}
/*This method cretaes the set 2*/
public static String set2()
{
int[] set2= {2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31};
String s="";
for(int i=1;i<=set2.length;i++)
{
s=s+set2[i-1]+" ";
if(i%4==0)
s+=" ";
}
return s;
}
/*This method cretaes the set 3*/
public static String set3()
{
int[] set3= {4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31};
String s="";
for(int i=1;i<=set3.length;i++)
{
s=s+set3[i-1]+" ";
if(i%4==0)
s+=" ";
}
return s;
}
/*This method cretaes the set 4*/
public static String set4()
{
int[] set4= {8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31};
String s="";
for(int i=1;i<=set4.length;i++)
{
s=s+set4[i-1]+" ";
if(i%4==0)
s+=" ";
}
return s;
}
/*this method cretaes the set 5*/
public static String set5()
{
int[] set5= {16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
String s="";
for(int i=1;i<=set5.length;i++)
{
s=s+set5[i-1]+" ";
if(i%4==0)
s+=" ";
}
return s;
}
/*this method outputs the description of this program*/
public static void description()
{
System.out.println("***********************************");
System.out.println();
System.out.println("Welcome to Birthday Guessing website by your name You will be prompted to answer five questions Then computer will guess your birthday");
System.out.println("***********************************");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.