Java Reference
In-Depth Information
Q UIZ
Answer the quiz for this chapter online at www.cs.armstrong.edu/liang/intro10e/quiz.html .
P ROGRAMMING E XERCISES
Sections 12.2-12.9
*12.1
( NumberFormatException ) Listing 7.9, 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 12.12).
F IGURE 12.12
The program performs arithmetic operations and detects input errors.
*12.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.
*12.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 Out of Bounds .
*12.4
( IllegalArgumentException ) Modify the Loan class in Listing 10.2 to
throw IllegalArgumentException if the loan amount, interest rate, or
number of years is less than or equal to zero.
*12.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 Triangle class to throw an IllegalTriangleException object if a
triangle is created with sides that violate the rule, as follows:
/** Construct a triangle with the specified sides */
public Triangle( double side1, double side2, double side3)
throws IllegalTriangleException {
// Implement it
}
 
 
Search WWH ::




Custom Search