Lab11: Validation using do-while

8ball

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

  • Use a loop structure for error correction.

  • Use a loop structure for execution control.

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, 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 ask 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.

Part 2: CodingBat 

If you have not already done so, refer to Part 1 of Lab 8 to set up your JMU account on codingbat.comRemember to log in before doing any problems so you will receive credit.

  1. Work through several 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 in a few weeks.

  3. Scoring for this section will come from the 3 problms you complete here.  See how many of the String-2 problems you can solve today. They each require only one one loop. At a minimum, solve the following:doubleCharcatDogrepeatFront. You may find (on CodingBat's site) the Java String Help document useful also the Java String tutorials may help.

Back to Top