Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSE114 Spring 2016
Lab Exercise 2 of Week 7
Solve Quadratic Equations
Chen-Wei Wang
Problem
For a quadratic equation
a · x2 + b · x+ c = 0
where a, b, and c denote coefficients that are constants, the following formula computes the solution(s)
for x (also known as the roots of the equation):
x =
−b±√b2 − 4ac
2a
If a = 0, then the equation is not quadratic, and to avoid the computation of a division by zero, an
error must be reported. If the value of a is non-zero, then the value of the discriminant (i.e., b2 − 4ac)
determines the number of roots for the above quadratic equation:
– If b2 − 4ac > 0, then there are two real roots;
– If b2 − 4ac = 0, then there is only one real root (i.e., −b2a );
– If b2 − 4ac < 0, then there is no real root.
For the purpose of this lab, we consider integer constant coefficients only. Create a new Java project
CSE114 S16 Week07 Lab2. Then create a new class SolveQuadraticEquations with the proper main
method header.
Your program should repeatedly prompt the user for integer values of the above constants a, b, and
c. If the value of a is zero, then an error must be reported immediately without further asking the user
to enter values for b and c.
If values of all constants are valid, then you will first calculate the value of the discriminant, based
upon which you will report back to the user about how many roots (i.e,. zero, one, or two) there are in
the equation they just specified. Also, when there are at least one root value existing for the equation,
print it (or them) out.
To calculate the value of discriminant, you are required to identify and use the appropriate methods
from the Math class to compute values of b2 and
√
b2 − 4ac. Click or enter the following URL to access
the API page for the Math class:
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
When the computation of root(s) is completed for the current equation, ask the user if they would
like to continue. Use the nextLine() method from the Scanner class to read in user’s answers. Assume
that “Y” means an yes, and everything else means a no.
Important Exercise: Put a break point next to the line of code that computes the discriminant, launch
the debugger, and observe how its value determines the branch of code that is executed.
1
Here is an expected sample run of your program (where user input values are set in bold face):
Enter an integer costant a:
0
Error: the equation is not quadratic.
Would you like to continue (Y/N)?
Y
Enter an integer costant a:
2
Enter an integer costant b:
17
Enter an integer costant c:
32
The equation has two real roots:
-2.813859338365493
-5.686140661634507
Would you like to continue (Y/N)?
Y
Enter an integer costant a:
2
Enter an integer costant b:
16
Enter an integer costant c:
32
The equation has one real root:
-4.0
Would you like to continue (Y/N)?
Y
Enter an integer costant a:
2
Enter an integer costant b:
15
Enter an integer costant c:
32
The equation has no real roots.
Would you like to continue (Y/N)?
N
Bye!
2