Inf1 OP : Lab Sheet Week 2 Q8 - Safer Quadratic Solver
Overview

In an earlier lab exercise, you wrote a program to solve quadratic equations. However, at the time, we noted that some quadratic equations would not have real solutions if we try to take the square-root of a negative number. In this case, the solution will involve imaginary solutions. Furthermore, in the quadratic equation we divide by \( A \), which may also prove problematic if \( A=0 \). We will look at these issues in more detail.

Checking the numerator

Start by writing a program SaferQuadraticSolver which does everything QuadraticSolver did from the earlier lab exercise.

In order to make our solver for quadratic equations more robust, we need to ensure that the value of \( B^2 - 4AC \) is non-negative. If the equation is solvable, your program should print the two solutions for \( x \) on separate lines. By contrast, in the case where \( B^2 - 4AC \) is negative, the program should print a suitable error message telling the user that the equation can not be solved for real values of \( x \). As in the last exercise, you should do this using an if-then-else statement. For example:

: java SaferQuadraticSolver 1.0 0.0 -1.0
1.0
-1.0

: java SaferQuadraticSolver 1.0 -3.0 2.0
2.0
1.0

: java SaferQuadraticSolver 1.0 -2.0 2.0
Equation is not solvable for real x.
Checking the denominator

Consider what happens if \( A = 0 \). Update your code to check for this possibility and produce an error message as follows:

: java SaferQuadraticSolver 0.0 -3.0 2.0
A = 0. Cannot solve equation.

An automated test has been created for this exercise: SaferQuadraticSolverTest.java.