Starting from:

$25

Assignment № 5- Reporting and Handling Exceptions



Reporting and Handling Exceptions
In Lab # 2, we defined static methods to compute the GCD of two numbers, the LCM of two
numbers, the factorial of a number and a term of the Fibonacci sequence. One drawback in
our implementation was returning -1 whenever those methods were invoked using invalid arguments. In modern programming languages, this situation is handled more elegantly.
Definition 1. An exception is the occurrence of an unusual event while a software application
is executing. Exception-handling is a programming technique used to deal with error conditions
in a controlled manner, sometimes without terminating execution of the program.
Definition 2. There are two categories of exceptions: checked and unchecked. Checked
exceptions must be handled and unchecked exceptions need not be handled. The standard
Java Library Error and RuntimeException, a subclass of Exception, or any of its subclasses
are checked exceptions. The Exception class and any of its other subclasses are unchecked
exceptions.
In this week’s lab session, you will revisit Lab # 2 and modify the program so that the methods
written for those mathematical functions handle invalid arguments by reporting any potential
exception. You will also modify the main class so that any exception that is reported is handled
in a try-catch statement. You will implement a user-define checked exception class and use it
to report potential exceptions.
The NotANumberException Class
Define a user-defined subclass of the standard Java library Exception class. This class should
consist of both a default and parameterized constructor. The parameterized instructor will have
one explicit parameter, a string. See sample classroom demo program on how to implement a
user-defined exception class.
The AuxiliaryMath Class
Modify the methods in this class so that each has exception-reporting capabilities. In each case
a NotANumberException should be reported along with a message describing why the exception
occurred.
1. public static int factorial(int n): throws the exception when n is negative.
2. public static int fibonacci(int n): throws an exception when n is not positive.
Assignment № 5 Page 1
3. public static gCD(int a, int b): throws an exception when both a and b are 0.
4. public static lCM(int a, int b): throws an exception when either a or b is 0.
Be sure to modify the Javadoc comments of each method by adding an @throws tag and when
the exception occurs.
The AuxiliaryMathDemo Class
Modify the main method of the AuxiliaryMathDemo class. Use a try-catch statement to handle
any potential exception as the following tasks are performed:
1. Prompts the user for three integers, reads the inputs from the keyboard, and computes
and displays the GCD of the three integers. (Hint: gcd(a, b, c) = gcd(gcd(a, b), c))
2. Prompts the user for two integers, reads the inputs from the keyboard, and computes and
displays the LCM of the integers.
3. Prompts the user for an integer n, reads the integer, and computes and displays n!.
4. Finally, prompts the user for two positive integers i and j, where i < j, reads the integers
and computes and displays the series consisting of the i
th through j
th Fibonacci numbers.
Additional Requirements
Exhaustively, test your program to ensure that it works. Also, test your program with inputs that
will generate an exception. Write header comments for each class using the follow Javadoc
documentation:
/**
* Explain the purpose of this class; what it does <br
* CSC 1350 Lab # 5
* @author YOUR NAME
* @since DATE THE CLASS WAS WRITTEN
*/
For the AuxiliaryMath class, after the @since line, add the following Javadoc:
* @see NotANumberException
For the AuxiliaryMathDemo class, after the @since line, add the following Javadoc:
* @see AuxiliaryMath, NotANumberException
Add Javadoc style documentation for each method in the NotANumberException and AuxiliaryMath classes. Run the Javadoc utility to make sure that it generates documentation for these
classes. Locate your source files, NotANumberException.java, AuxiliaryMath.java and AuxiliaryMathDemo.java, and enclose them in a zip file, YOURPAWSID_lab05.zip, and submit your
lab assignment for grading using the digital dropbox set up for this purpose. Here are three
sample program interactions:
Assignment № 5 Page 2
Listing 1: Sample Run 1
1 Enter three integers whose GCD is to be found - 75 90 100
2 Enter two integers whose LCM is to be found - 25 40
3 Enter a non-negative integer n to find n! - 6
4 Enter two positive integers i and j, where i < j - 1 5
5
6 gcd(75,90,100) = 5
7 lcm(25,40) = 200
8 6! = 720
9 fib(1) + ... + fib(5) = 7
Listing 2: Sample Run 2
1 Enter three integers whose GCD is to be found - 0 5 100
2 Enter two integers whose LCM is to be found - 0 20
3 auxiliarymathdemo.NotANumberException: lCM invoked with a 0 argument.
Listing 3: Sample Run 3
1 Enter three integers whose GCD is to be found - 12 60 96
2 Enter two integers whose LCM is to be found - 50 80
3 Enter a non-negative integer n to find n! - 7
4 Enter two positive integers i and j, where i < j - -4 8
5 auxiliarymathdemo.NotANumberException: fibonacci invoked with an argument ←-
that is not positive.
Assignment № 5 Page 3

More products