Java Reference
In-Depth Information
15.
You can use
Scanner
to read string and primitive data values from a text file and use
PrintWriter
to create a file and write data to a text file.
16.
The
JFileChooser
class can be used to display file dialogs for choosing files.
17.
You can read from a file on the Web using the
URL
class.
T
EST
Q
UESTIONS
P
ROGRAMMING
E
XERCISES
Sections 14.2-14.9
*14.1
(
NumberFormatException
) Listing 9.5, Calculator.java, is a simple command-
line calculator. Note that the program terminates if any operand is nonnumeric.
Write a program with an exception handler that deals with nonnumeric
operands; then write another program without using an exception handler to
achieve the same objective. Your program should display a message that informs
the user of the wrong operand type before exiting (see Figure 14.12).
F
IGURE
14.12
The program performs arithmetic operations and detects input errors.
*14.2
(
InputMismatchException
) Write a program that prompts the user to read
two integers and displays their sum. Your program should prompt the user to
read the number again if the input is incorrect.
*14.3
(
ArrayIndexOutOfBoundsException
) Write a program that meets the fol-
lowing requirements:
■
Creates an array with
100
randomly chosen integers.
■
Prompts the user to enter the index of the array, then displays the corre-
sponding element value. If the specified index is out of bounds, display the
message
Outof Bounds
.
*14.4
(
IllegalArgumentException
) Modify the
Loan
class in Listing 10.2 to
throw
IllegalArgumentException
if the loan amount, interest rate, or num-
ber of years is less than or equal to zero.
*14.5
(
IllegalTriangleException
) Programming Exercise 11.1 defined the
Triangle
class with three sides. In a triangle, the sum of any two sides is
greater than the other side. The
Triangle
class must adhere to this rule. Create
the
IllegalTriangleException
class, and modify the constructor of the