CS 149: Introduction to Programming
James Madison University, Fall 2017 Semester

Lab11: Validation using do-while

Background

We have already worked with if statements to detect invalid input. But in some cases users will repeatedly enter the wrong value. In addition, some problems lend themselves to executing the program repeatedly. This lab will explore these uses of loops in Java.

Objectives

Part 1: CodingBat Warm-up

If you have not already done so, refer to Lab08 to set up your JMU account on codingbat.com. Remember to log in before doing any problems so you will receive credit.

  1. Work through three or more of the Warmup-2 problems. All of them have solutions available; try to solve them first before looking at the answer.

  2. Some of the warm-up problems will ask you to use integer arrays. You can tell by the data type int[]. Don't worry about those problems; we'll get to that topic next week.

  3. Work through some of the String-2 problems. They each require only one loop. At a minimum, solve the following: doubleChar, catDog, repeatFront. You may find (on CodingBat's site) the Java String Introduction useful. Ideally, you will do more problems, but do Part 2 below and start Lab12 first.

Part 2: 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, and read through the code. Ask the instructor or lab assistant about any lines you do not understand.

  2. Rather than exit the program if the question is longer than 60 characters, read in the question again. Continue checking for the length until the user enters a question less than or equal to 60 characters.

  3. Recall that the nextLine method can read in an empty string. If the question is empty (zero length), you should tell the user that's not allowed and then prompt for and read in the question again. Do this until the user enters a valid string.

  4. A question should end with the '?' character. Check the question string and make sure that it ends with a question mark. Be careful...the empty string has no characters in it. If there is no question mark, tell the user that they need to add a question mark and repeat as before.

  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 is at most 60 chars ending with a question mark. Submit your final EightBall.java file via Canvas by the end of the day.