James Madison University, Spring 2018 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
Use a loop structure for error correction.
Use a loop structure for execution control.
Part 1: CodingBat Warm-up
If you have not already done so, refer to Part 1 of Lab09 to set up your JMU account on codingbat.com. Remember to log in before doing any problems so you will receive credit.
Work through several of the Warmup-2 problems. All of them have solutions available; try to solve them first before looking at the answer.
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.See how many of the String-1 problems you can solve today. None of these problems require any loops. At a minimum, solve the following:
firstHalf, endsLy, and conCat.See how many of the String-2 problems you can solve today. Each of these problems requires ONE loop. At a minimum, solve the following:
doubleChar and catDog.You may find (on CodingBat's site) the Java String Introduction useful.
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!
-
Download the EightBall.java program, and read through the code. Ask the instructor or lab assistant about any lines you do not understand.
-
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.
-
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. -
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. -
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. About half of your lab grade will be based on your CodingBat results.