1. Error checking - beginning:
In your original program, you asked the user for a question and then
"shook" the 8 ball to get an answer. You did a check 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 as follows.
- In the original lab, you had a method that checked the question for length. This method would then abort the program if it was too long after printing the error message. This time, we want to change that method to return a valid question. That means that this method should not abort if wrong, but instead loop and return a valid question.
- If the String is longer than
60, print out an error message that
indicates that the String is too long.
Reprompt
the
user and read in the question again. Continue checking for
the
length until the user enters a question less than 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.
2 Error checking - refinement:
Once item 1 is working correctly, add this variant that will
also make sure that we have something there for the question...in other words, the question is not too short.
Follow these
guidelines to check for a too short question.
- Your question should not be
too short. If the String is less than 3 characters long, you should tell
the
user that a short 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.
- 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.
- Note also that you should combine this requirement with the first requirement. In other words, for each entry that the user makes, you should check to make sure that it is not too long nor too short. This is tricky so again THINK before you code.
3 Continue to play:
Once item 2 is working correctly, make the following changes.
- You are already prompting
for and receiving a yes/no response to the
question "Do you want to ask a
question(yes/no)? ". However, if they say something other than "yes" we are using a System.exit to leave the program. While this is legal, it is not the best coding practice. So consider how you might change this if statement to simply end main if the answer is something other than "yes" and then implement that change.
- Then, you should change this whole structure into a loop which will execute only if they have answered "yes" to the question. Hmmmm, do you need an else any more?
4 When is a question not a question?:
Once item 3 is working correctly, add in a final refinement.
- A question ends with the character '?'. Check the question String and make sure that it ends with a question mark. (See Chapter 9 for the "endsWith"
method. Be careful...the empty String has no characters in it; in other words, make sure that there is at least one character to test).
- If there is no question mark, tell the user that they need to ask a question and reprompt as in items 1 and 2.