Java Reference
In-Depth Information
Programming Assignments
1 Multiplication Quiz
Start TextPad. Open the file named Multiply from the Chapter04 folder of the
Data Disk. Review the program. This Multiplication Quiz application asks
students to enter the multiplication table they wish to practice and then prompts
them to enter each answer, multiplying their table value by each integer from 0
to 12. Although the program tells students whether they are right or wrong, it
does not provide error checking to handle exceptions caused by the input of
non-integer values, such as decimal or String values.
Using techniques learned in this chapter, write the try and catch statements
to display appropriate messages if students try to enter non-integer numbers.
Also write a while statement to create a loop that repeats the input prompt if a
student enters invalid data.
1. With the Multiply.java code displayed in the TextPad window, substitute
your name and date in the block comment. Type Quiz as the new class
name. Edit the name of the class in the class header as well.
2. Save the file on your Data Disk with the file name Quiz.java. If you wish,
print a copy of the source code to reference while completing this lab.
3. Compile the program by pressing CTRL + 1 .
4. Run the program by pressing CTRL + 2 . When the program executes in the
command prompt window, enter an integer value, such as 8, as the value for
the multiplication table you wish to practice. Respond to the prompts by
entering the correct answers as integers. When you have completed the
multiplication table, press any key or click the Close button in the command
prompt window to quit the program.
5. Run the program again. Enter 8 as the value for the multiplication table you
wish to practice. Respond to the first prompt by entering an incorrect
answer, such as 80. Notice that the program tells you the answer is incorrect
but does not allow you to try again. Click the Close button on the command
prompt window to quit the program.
6. Run the program again. Enter a non-integer value, such as 7.5, as the value
for the multiplication table you wish to practice. Java throws an exception,
and a NumberFormatException message displays in the command prompt
window.
7. Use TextPad to edit the program source code. In the main() method, enclose
the section of code starting with the line, //Calling the user-defined meth-
ods, in a try statement. Remember to enter the try statement and an opening
brace before and a closing brace after that section of code.
8. Below the try statement, enter a catch statement for a
NumberFormatException. Write code to print an appropriate error message
using the System.out.println() method or a JOptionPane message box.
9. Compile and run the program again, testing with both integer and non-
integer values. Notice that an appropriate message now displays, but students
must run the program again to answer any additional questions.
(continued)
Search WWH ::




Custom Search