Due: Wednesday, March 7, 2018 @ 8:00PM
Write a program that solves quadratic equations and prints their roots. A quadratic equation has the following form: \[ ax^2 + bx + c = 0 \] where [ a≠ 0 ]. There are two solutions to such an equation: \[ x= \frac{-b \pm \sqrt{ b^2 - 4ac}}{2a} \] Note the ± symbol, which indicates one solution (root) is obtained by adding the two terms in the numerator, and the other is obtained by subtracting the two terms.
Write a method named printQuadraticRoots
that prints an equation and its two roots, and call the method in "main" with the appropriate parameters. You may assume the equation has two real roots and that a ≠ 0.
Please name your program QuadraticFormula.java
Use variables a
, b
, and c
to represent the INTEGER coefficients of the equation. These should be read from the user with a Scanner
object. Extensively TEST your program with many other values to verify correctness.
Input should be in the form:
a ==>
b ==>
c ==>
Make sure there is a space before and after the arrow (==>
)
Output the equation along with its two roots. You do NOT need to worry about how many digits follow the decimal point.
Use PRECISELY the format below with the EXACT same SPACING and SPELLING. The output sample below is for the equation \[ x^2 − 2x − 4=0 \]
For the equation "(1)x^2 + (-2)x + (-4) = 0", the roots are
x = 3.236068
and
x = -1.236068
Note: there are two spaces between the beginning of the line and the x
output on the second and fourth lines. There is only one space before the and
on the third line.
Include at LEAST the following methods. You will need to DETERMINE the return types and parameters, and the calling relationship between methods. Only call the discriminant
method ONCE so you don't compute the same quantity twice.
// Print "For the equation ... the roots are"
// followed by the two roots, in the format above
//
// HINT: look at the required variables to determine the parameters
DetermineReturnType printQuadraticRoots (...)
// Print a quadratic equation using the following format:
// (a)x^2 + (b)x + (c) = 0
// Do NOT print any quotation marks or newline characters
// HINT: look at the required variables to determine the parameters
DetermineReturnType printEquation (...)
// Compute and return the discriminant (b^2 - 4ac)
// HINT: look at the required variables to determine the parameters
// look at what the discriminant should evaluate to in order
// to derive the return type
DetermineReturnType calcDiscriminant (...)
You may use methods of the Math
class. Use WolframAlpha to verify that your program is correct. Ensure your program can handle any equation with real roots.
Use good programming style:
Ctrl+Shift+F
will format your code)Please name your program QuadraticFormula.java
Submit the Java source code file on Autolab under the "Lab 5" assignment.