Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment : The assignment is another exercise in the use of the exception and

ID: 3857901 • Letter: A

Question

Assignment:

The assignment is another exercise in the use of the exception and handling of exceptions. There is another simple calculation program listed below, which takes decimal values (doubles) as input for the a, b and c coefficients of a quadratic equation and calculates the two values for X. The program should always display two values as the result; a quadratic equation always has two values as the solution even when they are identical.

The program should repeat itself if the user wishes; this makes it easier to test. Values for A and B will be cleared to 0 (zero) after each calculation.

However it does nothing to handle the situation where a user enters data which is non-double or even non numeric. Your task today is to modify that program to handle the bad input situation with the use of exception handling. Bad inputs are:

Non-numeric values where numeric values are expected

Any input that causes the output to be “NaN” (Not a Number)

Any input that causes the output to be “INFINITY”

The program should not terminate but should inform the user of the specific error and should re-request the input value which was in error

Testing:

     Test your program with the following input;

a. Non-numeric value when double is expected.

b. A = 0

c. Values which result in “NaN” or “INFINITY” for answers.

d. Correct input

In all cases the program should ask for only the input values that were invalid and should continue with the values that were correct. The request for a corrected value should be repeated as many times as the user inputs illegal values.

The program should never crash!

Explanation / Answer

import java.util.Scanner;

public class QuadraticEquationSolver {

public static void main(String[] args) {
Scanner keyboard = new Scanner( System.in );
System.out.println("Enter the co-efficients and constants for the equation Ax^2 + Bx + C = 0");
System.out.println("Enter a value for A: ");
double a = checkDbl(keyboard);
System.out.println("Enter a value for B: ");
double b = checkDbl(keyboard);
System.out.println("Enter a value for C: ");
double c = checkDbl(keyboard);
System.out.println("Solving " + a + "x^2 +" + b + "x + " + c + " x = 0");
if ((b*b - 4*a*c)>=0)
{
double sol1 = (-b + Math.sqrt(b*b - 4*a*c) ) / (2*a);
double sol2 = (-b - Math.sqrt(b*b - 4*a*c) ) / (2*a);
if (sol1 == sol2)
{
System.out.println(sol1);
}
else
{
System.out.println(sol1 + " " + sol2);
}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote