In this question, we will write a program that solves quadratic equations, a type of equation commonly seen in science and engineering.
Quadratic equations are equations of the form:
\[ Ax^2 + Bx + C = 0 \]where we are given values for \( A \), \( B \), \( C \) and want to solve for \( x \).
We can rearrange and solve for \( x \) using the following formula:
\[ x = \frac{-B \pm \sqrt{B^2 - 4AC} }{2A} \]The \( \pm \) operator tells us that we should expect two solutions, the first when we use a plus, and the second when we use a minus.
Warning
The solution of quadratic equations can include imaginary numbers. For this exercise, we are only interested in equations with real solutions. If you try to make up random inputs for \( A \), \( B \), \( C \), then you will probably not create equations with real solutions, and your program will most likely crash. The values of \( A \), \( B \), \( C \) that will lead to a real solution will satisfy the inequation \( B^2 - 4AC \geq 0 \).
Write a program QuadraticSolver to read three floating point numbers from the command line, representing the values of \( A \), \( B \), \( C \), and then use the values of these arguments to solve for \( x \). Your program should print the two solutions for \( x \) on separate lines. Eg:
: java QuadraticSolver 1.0 0.0 -1.0 1.0 -1.0 : java QuadraticSolver 1.0 -3.0 2.0 2.0 1.0 : java QuadraticSolver 1.0 4.0 3.0 -1.0 -3.0 : java QuadraticSolver 1.0 10.0 0.0 0.0 -10.0
Note
To calculate square roots, you can use the method Math.sqrt().
An automated test has been created for this exercise: QuadraticSolverTest.java.