2 Error checking - part 1: In the original program, it asked the user for a ques
ID: 3529506 • Letter: 2
Question
2 Error checking - part 1: In the original program, it asked the user for a question and then "shook" the 8 ball to get an answer. It checks to see if the question was too long. In this variant, you will not end the program, but instead ask the user to enter a shorter question. If the String is longer than 60, print out an error message that indicates that the String is too long. Re-prompt the user and read in the question again. Continue checking for the length until the user enters a question less or equal to 60 characters. THINK very carefully about how you will set up the loop and how you will handle the repetition. Will you use a pre or post condition loop? Decide this before you code. Note: your error message should be specific...if the problem is that the String is too long, state that. Once it has passed this error, output the question and the answer as in the original lab. 3 Error checking - part 2: Once part 1 is working correctly, add this variant that will also make sure that we have something there for the question. Recall that the nextLine() method can read in an empty String. Follow these guidelines to check for that error. Your question should not be the null (or empty) String. (Hint: The String class has a length method that will tell you how many characters are in the string). If the String is null (empty), you should tell the user that the null string is not allowed and then prompt for and read in the question again. You should do this until the user enters a valid String. This should be a second decision in the loop you wrote above. After you have read in the proper question, echo the question with a suitable label, "shake" the 8 ball, and return the answer as before. 4 Error checking - part 3: Once part 2 is working correctly, add in a check to make sure that the String is in the form of a question. If the String does not end with the question mark symbol, '?', you should print an appropriate error message and prompt the user and read in the question again. NOTE: This will not be a new loop, but will be part of the loop you created in parts 1 and 2 above. Continue to check the length and check for a question mark before continuing. You should look at the String methods to figure out how you might figure out if the last character is a '?'. Once it has passed the three errors, echo the question with a suitable label, "shake" the 8 ball and return the answer as before. 5 Continue to play - part 4: Once part 3 is working correctly, add a new loop surrounding the original code which will execute the loop until the user says that they no longer wish to continue. You are already prompting for and receiving a yes/no response to the question "Do you want to ask a question(yes/no)? ". Once they have asked a question and received their first answer, you should prompt to see if they want to play again with something like "Ask another question?" Again, if they respond with "YES", "Yes", or "yes" return to the prompt for the question and play again. Continue doing this until the user enters something other than a "yes". THIS IS THE PROGRAM: import java.util.Scanner; import java.util.Random; /********************************************************************** * Class purpose: Simulate an 8-ball * * @author Archer Harris * @version 10/4/05 *********************************************************************/ public class EightBall { /****************************************************************** * Method purpose: simulate a Magic 8-ball * * @param args Command line arguments, ignored *****************************************************************/ public static void main(String [] args) { Scanner stdin; // Input Stream String wantToAsk; // User input: want to ak a question String questionStr; // User input: the question int seed; // Userr input: the random generator seed Random answerPicker; // The random generator object int answerIndex; // The index value returned by random generator String answerStr; // The program's answer stdin = new Scanner(System.in); System.out.printf("Magic 8 Ball "); System.out.print("Do you want to ask a question(yes/no)? "); wantToAsk = stdin.nextLine(); System.out.println(); if (!wantToAsk.equalsIgnoreCase("Yes")) { System.out.print("Goodbye "); System.exit(1); } //***************** // Get the 3 inputs //***************** System.out.printf("What is your question? "); questionStr = stdin.nextLine(); if(questionStr.length() > 60) { System.out.print("Your question is too long. Be more concise. Goodbye "); System.exit(2); } // Get a seed value. System.out.printf("Type a large positive integer: "); if (stdin.hasNextInt()) seed = stdin.nextInt(); else seed = Integer.MAX_VALUE; System.out.printf(" "); //*************** // Pick an answer //*************** answerPicker = new Random(seed); answerIndex = answerPicker.nextInt(20) + 1; // Use answerIndex to condition a switch statement. Print the // result of the answerIndex assignment to be sure you understand what // numbers will be generated by the nextInt() method. switch (answerIndex) { case 1: answerStr = "Signs point to yes."; break; case 2: answerStr = "Yes."; break; case 3: answerStr = "Reply hazy, try again."; break; case 4: answerStr = "Without a doubt."; break; case 5: answerStr = "My sources say no."; break; case 6: answerStr = "As I see it, yes."; break; case 7: answerStr = "You may rely on it."; break; case 8: answerStr = "Concentrate and ask again."; break; case 9: answerStr = "Outlook not so good."; break; case 10: answerStr = "It is decidedly so."; break; case 11: answerStr = "Better not tell you now."; break; case 12: answerStr = "Very doubtful."; break; case 13: answerStr = "Yes - definitely."; break; case 14: answerStr = "It is certain."; break; case 15: answerStr = "Cannot predict now."; break; case 16: answerStr = "Most likely."; break; case 17: answerStr = "Ask again later."; break; case 18: answerStr = "My reply is no."; break; case 19: answerStr = "Outlook good."; break; case 20: answerStr = "Don't count on it."; break; default: answerStr = "HUH?"; break; } //*************** // Output results //*************** System.out.printf("Question: %s ", questionStr); System.out.printf(" Answer: %s ", answerStr); } }Explanation / Answer
This is the corrected code .Check it out..
It coded as per your requirments.Plz rate it ..
Code begins here..
/////////////////////////////////////////////////////////
import java.util.Scanner;
import java.util.Random;
/**********************************************************************
* * Class purpose: Simulate an 8-ball *
* @author Archer Harris * @version 10/4/05
* *********************************************************************/
public class EightBall {
/******************************************************************
* * Method purpose: simulate a Magic 8-ball
* * * @param args Command line arguments, ignored **
* **************************************************************
* */
public static void main(String [] args)
{ Scanner stdin;
// Input Stream
String wantToAsk;
// User input: want to ak a question
String questionStr;
// User input: the question
int seed;
// Userr input: the random generator seed
Random answerPicker;
// The random generator object
int answerIndex;
// The index value returned by random generator
String answerStr;
// The program's answer
stdin = new Scanner(System.in);
System.out.printf("Magic 8 Ball ");
System.out.print("Do you want to ask a question(yes/no)? ");
wantToAsk = stdin.nextLine(); System.out.println();
if (!wantToAsk.equalsIgnoreCase("Yes"))
{ System.out.print("Goodbye "); System.exit(1);
}
//*****************
// Get the 3 inputs
//*****************
do{
Scanner sc1=new Scanner(System.in);
do{
System.out.printf("What is your question? ");
questionStr = sc1.nextLine();
if(questionStr.length() > 60)
{
System.out.print("Your question is too long. Be more concise.");
System.out.println("Please enter a short que");
}
if(questionStr.isEmpty())
{
System.out.println("Null String questions are not allowed");
}
if(!questionStr.endsWith("?"))
{
System.out.println("Not a valid question.PLease End the question with a '?'");
}
}while(questionStr.length() >60 || questionStr.isEmpty() || questionStr==null || !questionStr.endsWith("?"));
// Get a seed value.
System.out.printf("Type a large positive integer: ");
if (sc1.hasNextInt())
seed = sc1.nextInt();
else
seed = Integer.MAX_VALUE;
System.out.printf(" ");
//*************** // Pick an answer //***************
answerPicker = new Random(seed);
answerIndex = answerPicker.nextInt(20) + 1;
// Use answerIndex to condition a switch statement. Print the
// result of the answerIndex assignment to be sure you understand what
// numbers will be generated by the nextInt() method.
switch (answerIndex)
{
case 1:
answerStr = "Signs point to yes.";
break;
case 2:
answerStr = "Yes."; break;
case 3:
answerStr = "Reply hazy, try again."; break;
case 4:
answerStr = "Without a doubt."; break;
case 5:
answerStr = "My sources say no."; break;
case 6:
answerStr = "As I see it, yes.";
break;
case 7:
answerStr = "You may rely on it.";
break;
case 8:
answerStr = "Concentrate and ask again.";
break;
case 9:
answerStr = "Outlook not so good."; break;
case 10:
answerStr = "It is decidedly so.";
break;
case 11:
answerStr = "Better not tell you now.";
break;
case 12:
answerStr = "Very doubtful.";
break;
case 13:
answerStr = "Yes - definitely.";
break;
case 14:
answerStr = "It is certain.";
break;
case 15:
answerStr = "Cannot predict now.";
break;
case 16:
answerStr = "Most likely.";
break;
case 17:
answerStr = "Ask again later.";
break;
case 18:
answerStr = "My reply is no.";
break;
case 19:
answerStr = "Outlook good.";
break;
case 20:
answerStr = "Don't count on it."; break;
default:
answerStr = "HUH?";
break;
}
//*************** // Output results //***************
System.out.printf("Question: %s ", questionStr);
System.out.printf(" Answer: %s ", answerStr);
System.out.println(" Ask Another Question . Type Yes if want else No");
Scanner sc=new Scanner(System.in);
wantToAsk=sc.nextLine();
}while(wantToAsk.equalsIgnoreCase("Yes"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.