/**
 * CS139 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Spring 2016
 */

While (or Do-While) For Input Validation

Objectives

Resources

Java String API - This page contains a list of all the methods for the String class. Clicking a method in the "Method Summary" chart will take you to a more detailed description of that method.

Part 1: The Magic 8-Ball

The Magic 8-Ball is a toy produced by Tyco Toys (now Mattel) and consists of a ball filled with a blue fluid. Suspended in the fluid is a icosahedron (a 20-sided polyhedron with each side consisting of an equilateral triangle). Each face has an answer to a yes/no question. Check out this link to see what a Magic 8-Ball looks like on the inside!

  1. Download the EightBall.java program. Read through the code and try running it. Ask the instructor about any lines you do not understand.

  2. The provided program code prompts the user for a question and then "shakes" the 8 ball to get an answer. It also checks to see if the question is too long. In this variant, you will not end the program, but instead ask the user to enter a shorter question as follows.

    If the String is longer than 60 characters, print out an error message, "Your question is too long. Be more concise.\n". Re-prompt the user and read in the question again. ("What is your question?\n") Continue checking for the length until the user enters a question less than or equal to 60 characters. Test your code after making this change.

  3. Questions shouldn't be too long, but they also shouldn't be too short. Modify the code to print an error message if a user enters a question that is less than 3 characters long. The message should be "Your question is too short. Please try again.\n". Once again, the user should be re-prompted until they enter a valid question. Test your modifications.
  4. Questions must end with a question mark. If there is no question mark, tell the user that they need to ask a question and re-prompt as with the previous two items. The error message should be "You need to ask a question.\n". Test your modification. (HINT: The endsWith method of the String class will probably be useful here.)
  5. Add a new loop (surrounding most of the original code) that will execute the program until the user says they no longer wish to play. Note you are already prompting for and receiving a yes/no response to the question "Do you want to ask a question (yes/no)?"

Your final version should allow you to keep asking questions until you enter the word no, and it should verify that each question has an appropriate length and ends with a question mark. Submit your final EightBall.java file via Canvas.

Part 2: If You Have Extra Time

Complete as many exercises as possible in the Codingbat Logic-2 section, starting with makeBricks. (Note that these are challenging problems. There are some pages of advice for the makeBricks problem. Feel free to take advantage of the help if you get stuck.)