Activity: Exception Handling Solution

$35.00 $29.05

You'll get a: . zip file solution, download link after Payment

Description

Goals:

By the end of this project you should be able to do the following:

  • Handle exceptions that can occur in your program using a try-catch statement

  • Throw exceptions in your program

Directions:

Don’t forget to add your Javadoc comments for your classes, constructor, and methods in this activity.

For this assignment, you will be creating two classes: Division and DivisionDriver

Division: methods

  • Division has two public static methods:

  1. intDivide: takes two int parameters (a numerator and denominator), performs integer division, and returns the int result of dividing the numerator by the

denominator.

    1. decimalDivide: takes two int parameters (a numerator and denominator), performs floating point division (you’ll have to use casting), and returns the result of dividing the numerator by the denominator.

  • Test your methods in the Interactions pane:

Division.intDivide(10, 3)

lu22645olv tmp bbb78eee5c969bb7

3

Division.decimalDivide(10, 3)

3.3333333333333335

DivisionDriver

  • DivisionDriver contains a main method only. The program will get a numerator and denominator from the user and print the integer division and decimal division result.

  • Create a dialog box that will get the numerator and denominator as a String (you’ll have to import the JOptionPane class in the javax.swing package):

lu22645olv tmp 4e5477db1e425f3a

  • Convert each to an integer value using the static parseInt method in the Integer class:

lu22645olv tmp 3b02fc0fb900e3f1 lu22645olv tmp ee2600a625f382c5

Page 1 of 5

Activity: Exception Handling Page 2 of 5

lu22645olv tmp 1105fc365e7a1091

  • Create a String object to hold the result of the division:

  • Print the result in a dialog box:

  • Test your method by running the driver program with numerator 19 and denominator 5:

lu22645olv tmp 8e6e58305de6fdd0 lu22645olv tmp 67f9181ae304f18 lu22645olv tmp b6a27d57e286acfc

  • Now try entering an invalid number in the dialogs (five and ten):

lu22645olv tmp 7b37f43021ad8f76

Your program should generate a run-time error in the form of a NumberFormatException exception:

—-jGRASP exec: java DivisionDriver

lu22645olv tmp 5865af27cb29d780

Exception in thread “main” java.lang.NumberFormatException: For input string: “five” at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580)

at java.lang.Integer.parseInt(Integer.java:615)

at DivisionDriver.main(DivisionDriver.java:9)

—-jGRASP wedge: exit code for process is 1.

—-jGRASP: operation complete.

lu22645olv tmp 3b02fc0fb900e3f1

Page 2 of 5

Activity: Exception Handling Page 3 of 5

lu22645olv tmp 1105fc365e7a1091

  • The exception occurs when the parseInt method tries to convert the String “five” to an integer. The Java API listing for parseInt lists the exception that it might throw.

  • Use a try-catch statement to catch the exception and tell the user what went wrong without creating a run-time error:

lu22645olv tmp 1ff93566681c7135

Existing

code

  • Try entering invalid values five and ten once more for numerator and denominator once more. You should now get the following error:

lu22645olv tmp d6c13202edd642cb

Exception Throwing

  • Try the following in the interactions pane:

Division.intDivide(10, 0)

lu22645olv tmp 1c00fbd62835ebaa

java.lang.ArithmeticException: / by zero

  • Try to run your driver program with the numerator 10 and denominator 0:

lu22645olv tmp 4ec0342528ff18b6

—-jGRASP exec: java DivisionDriver

lu22645olv tmp eb91779177b83cea

Exception in thread “main” java.lang.ArithmeticException: / by zero at Division.intDivide(Division.java:7)

at DivisionDriver.main(DivisionDriver.java:12)

lu22645olv tmp 3b02fc0fb900e3f1

Page 3 of 5

Activity: Exception Handling

Page 4 of 5

—-jGRASP wedge: exit code for process is 1.

—-jGRASP: operation complete.

  • The exception is generated in the intDivide method and not caught/handled, so it is propagated to main where it is also not caught/handled. Next we want to catch the exception in the intDivide method so that it will not be propagated to the main method.

  • In your intDivide method, add code that will return 0 if an ArithmeticException occurs and the division result otherwise:

lu22645olv tmp ee4b8573c19c3d64

Existing code

Run DivisionDriver with inputs 10 and 0. The result should be 0 for integer division:

lu22645olv tmp 66b803224aff7657

  • Suppose that you do not want users to be able to divide by 0 in your decimalDivide method.

Division.decimalDivide(10, 0)

lu22645olv tmp 1c00fbd62835ebaa

Infinity

The IllegalArgumentException in the Java API can be thrown if a particular argument

(parameter) to a method is not allowed:

http://download.oracle.com/javase/6/docs/api/java/lang/IllegalArgumentException.html

  • In your decimalDivide method, throw an IllegalArgumentException if the denominator is zero:

if (denom == 0) {

throw new IllegalArgumentException(“The denominator “

+ “cannot be zero.”);

}

lu22645olv tmp 3b02fc0fb900e3f1

Page 4 of 5

Activity: Exception Handling Page 5 of 5

lu22645olv tmp 1105fc365e7a1091

  • Test your method again in interactions. You should now see the exception:

Division.decimalDivide(10, 0)

lu22645olv tmp 4645ba8ae35829f2

java.lang.IllegalArgumentException: The denominator cannot be zero.

  • In your main method, add another catch statement to catch the exception that is thrown by the decimalDivide method. This time, print the exception text itself (stored by variable errorMessage):

lu22645olv tmp f3a39094b1d108b

Existing code

  • Now try dividing by 0 in your program. You will get the following error message instead of a run-time error:

lu22645olv tmp 3bd36926cf65b22f lu22645olv tmp a4f9029fc16506b6

  • Run Checkstyle on each of your files and make any necessary corrections indicated by audit errors.

  • Finally, submit your files to the grading system.

lu22645olv tmp 3b02fc0fb900e3f1

Page 5 of 5