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

Java program! Assignment : The next lab assignment is another exercise in the us

ID: 3866874 • Letter: J

Question

Java program!

Assignment:

The next lab 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 shouldalways 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

You must create a file named driver.java and paste given code into it!

driver.java


import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.*;
import static java.lang.Math.sqrt;

public class driver {

public static void main(String[] args)
{
  
while(true)
{
double a = 0,b = 0,c = 0;
Scanner scanner = new Scanner(System.in);

boolean bError = true;
while (bError)
{
System.out.println("Enter the value of a!");
if (scanner.hasNextDouble())
{
a = scanner.nextDouble();
}
else
{
System.out.println("Entered Non-numeric value, please enter correct value of a!");
scanner.next();
continue;
}
bError = false;
if(a == 0)
{
bError = true;
System.out.println("Error, Please do not enter a = 0!");
}
}
bError = true;

while(bError)
{
System.out.println("Enter the value of b!");
if (scanner.hasNextDouble())
{
b = scanner.nextDouble();
}
else
{
System.out.println("Entered Non-numeric value, please enter correct value of b!");
scanner.next();
continue;
}
bError = false;
}

bError = true;
while(bError)
{
System.out.println("Enter the value of c!");
if (scanner.hasNextDouble())
{
c = scanner.nextDouble();
}
else
{
System.out.println("Entered Non-numeric value, please enter correct value of c!");
scanner.next();
continue;
}
bError = false;
}
if( (b*b - 4*a*c) < 0)
{
System.out.println("Descriminant of the quadratic is < 0, so please Enter values of a,b,c again!");
}
else
{
double r1 = ( -b + Math.sqrt(b*b - 4*a*c) ) /(2*a);
double r2 = ( -b - Math.sqrt(b*b - 4*a*c) ) /(2*a);
System.out.println("Root1 = "+ r1 + " Root2 = " + r2 + " ");
System.out.println("You can now test other quadratic!");
}
  
}
}
}

Sample Output:

Enter the value of a!
ss
Entered Non-numeric value, please enter correct value of a!
Enter the value of a!
0
Error, Please do not enter a = 0!
Enter the value of a!
1
Enter the value of b!
5
Enter the value of c!
6
Root1 = -2.0 Root2 = -3.0


You can now test other quadratic!
Enter the value of a!

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