CSCI 161: Quadratic Formula
Objectives
- Write a Java Project from scratch
- Learn how to read input from the user
- Decompose problem solving to several methods
- Use good naming conventions when creating variables
- Leverage your improved knowledge of the Java programming language to produce the desired output
Overview
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
Input Specification
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 Specification
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 \]
Sample Output
a ==> 1
b ==> -2
c ==> -4
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.
Required Methods
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.
IMPORTANT: make sure you CAREFULLY read what each method does
// 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
printQuadraticRoots (...)
DetermineReturnType
// 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
printEquation (...)
DetermineReturnType
// 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
calcDiscriminant (...) DetermineReturnType
Hints
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.
Style
Use good programming style:
- Write comments
- Choose mnemonic, meaningful variable names (e.g. seconds, minutes)
- Indent consistently (Ctrl+Shift+F will format your code)
- Remember the comment block at the top of your program
Submission
Please name your program QuadraticFormula.java
Submit the Java source code file on Autolab under the “Quadratic Formula” assignment.
Grading
- 20 points will be awarded for having a well-formatted, well-documented source code file. This includes your name, date, description, and comments throughout the program. This also includes other style requirements, such as method naming conventions. NOTE: The autograder will not display these points.
- 20 points will be awarded for having proper input from a user.
- 40 points will be awarded for having methods with correct parameters and return types
- 20 points will be awarded for having proper output that matches EXACTLY
- NOTE: if your program does not compile/run, the highest score you will earn will be a 20/100