COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Exception Handling Practice1.java QuadraticEquation.java YouthLeague.java HandleTimeException Program
InvalidHourException.java
InvalidMinuteException.java
InvalidSecondException.java
HandleTimeException.java
HardwareStore Programs



QuadraticEquation Program


Description

This program inputs three integers: a, b, and c for a quadratic equation, then uses exception handling techniques to handle several possible exceptions: InputMismatchException and ArithmeticException.

Associated Concepts:

Exception Handling

Algorithm:

main method:

Input three integers: a, b, and c for a quadratic equation.

If the user entered an invalid integer, the try block terminates immediately, and the program execution jumps to the matching catch block to handle the InputMismatchException.

If a is zero, the program throws an ArithmeticException immediately, and the program execution jumps to the matching catch block to handle the exception.

If all three inputs are valid integers, the program calculates the discriminant and calls the calRoot() method with passed arguments of a, b, c, and discriminant.

The finally block is executed regardless.

calRoot() method definition:

If the discriminant is less than 0, the method throws an exception of the superclass Exception object.

If the discriminant is equal to 0, the method calculates and prints the single root.

If the discriminant is greater than 0, the method calculates and prints two distinct roots.

Source Code



Sample Run



Video