20.2. Lists

 
[Page 601 ( continued )]

Programming Exercises

Sections 17.2 “17.4

17.1* ( NumberFormatException ) Listing 8.4, Calculator.java, is a simple command-line calculator. Note that the program terminates if any operand is non-numeric. Write a program with an exception handler that deals with non-numeric 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 17.10).
[Page 602]
Figure 17.10. The program performs arithmetic operations and detects input errors.


17.2* ( ArithmeticException and NumberFormatException ) Write a program that creates a user interface to perform integer divisions, as shown in Figure 17.11. The user enters two numbers in the text fields, Number 1 and Number 2. The division of Number 1 and Number 2 is displayed in the Result field when the Divide button is clicked. If Number 1 or Number 2 were not an integer, the program would throw a NumberFormatException . If Number 2 were zero, the program would throw an ArithmeticException . Display the exception in a message dialog box, as shown in Figure 17.11.
Figure 17.11. The program displays an error message in the dialog box if the number is not well formatted.

17.3* ( ArrayIndexOutBoundsException ) Write a program that meets the following requirements:
  • Create an array with one hundred randomly chosen integers.

  • Create a text field to enter an array index and another text field to display the array element at the specified index (see Figure 17.12).

    Figure 17.12. The program displays the array element at the specified index or displays the message Out of Bound if the index is out of bounds.

  • Create a Show Element button to cause the array element to be displayed. If the specified index is out of bounds, display the message Out of Bound .

17.4* ( IllegalArgumentException ) Modify the Loan class in §7.15, "Case Study: The Loan Class," to throw IllegalArgumentException if the loan amount, interest rate, or number of years is less than or equal to zero.
17.5* ( IllegalTriangleException ) Exercise 9.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 :
[Page 603]
  /** Construct a triangle with the specified sides */    public   Triangle(   double   side1,   double side2   ,   double   side3)   throws   IllegalTriangleException {  // Implement it  } 

17.6* ( NumberFormatException ) Exercise 8.7 specifies the parseHex(String hexString) method, which converts a hex string into a decimal number. Implement the parseHex method to throw a NumberFormatException if the string is not a hex string.
17.7* ( NumberFormatException ) Exercise 8.8 specifies the parseBinary(String binaryString) method, which converts a binary string into a decimal number. Implement the parseBinary method to throw a NumberFormatException if the string is not a binary string.
17.8* ( HexFormatException ) Exercise 17.6 implements the parseHex method to throw a NumberFormatException if the string is not a hex string. Define a custom exception called HexFormatException . Implement the parseHex method to throw a HexFormatException if the string is not a hex string.
17.9* ( BinaryFormatException ) Exercise 17.7 implements the parseBinary method to throw a BinaryFormatException if the string is not a binary string. Define a custom exception called BinaryFormatException . Implement the parseBinary method to throw a BinaryFormatException if the string is not a binary string.
[Page 604]
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net