-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadraticTest.java
More file actions
91 lines (86 loc) · 3.46 KB
/
Copy pathQuadraticTest.java
File metadata and controls
91 lines (86 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Exercise 9.10
* (Algebra: quadratic equations) Design a class named QuadraticEquation for
* a quadratic equation ax^2 + bx + x = 0. The class contains:
* - Private data fields a , b , and c that represent three coefficients.
* - A constructor for the arguments for a , b , and c .
* - Three getter methods for a , b , and c .
* - A method named getDiscriminant() that returns the discriminant, which is b^2 - 4ac.
* - The methods named getRoot1() and getRoot2() for returning two roots of the equation
* r1 = (-b + sqrt(b^2 -4ac))/ 2a
* r2 = (-b - sqrt(b^2 -4ac))/ 2a
* These methods are useful only if the discriminant is nonnegative. Let these methods
* return 0 if the discriminant is negative.
* Write a test program that prompts the user to enter values for a, b, and c and displays the result
* based on the discriminant. If the discriminant is positive, display the two roots. If
* the discriminant is 0, display the one root. Otherwise, display "The equation has no roots."
* Test:
* positive x^2 +6x + 5 = 0
* zero: x^2 - 2x +1 = 0
* negative: x^2 -3x +10 = 0
*
*/
import java.util.Scanner;
public class QuadraticTest {
public static void main(String[] args) {
// Create scanner and prompt user for input
Scanner input = new Scanner(System.in);
System.out.print("Enter values for the three data fields a, b, c: " );
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
QuadraticEquation quadtraticequation = new QuadraticEquation(a, b, c);
double discriminant = quadtraticequation.getDiscriminant();
// If the discriminant is positive, display the two roots.
if (discriminant > 0) {
System.out.println("The equation with values a = " + quadtraticequation.getA() + ", b = " + quadtraticequation.getB() +
", c = " + quadtraticequation.getC() + " has two real roots: r1 = " + quadtraticequation.getRoot1() +
" and r2 = " + quadtraticequation.getRoot2());
}
// If the discriminant is 0, display the one root
else if (discriminant == 0) {
System.out.println("The equation with values a = " + quadtraticequation.getA() + ", b = " + quadtraticequation.getB() +
", c = " + quadtraticequation.getC() + " has only one root r = " + (quadtraticequation.getRoot2()));// || quadtraticequation.getRoot2()));
}
// Otherwise, display “The equation has no roots"
else System.out.println("The equation has no roots");
}
}
class QuadraticEquation {
// Private data fields a , b , and c that represent three coefficient
private double a, b, c;
// A constructor for the arguments for a , b , and c
public QuadraticEquation (double newA, double newB, double newC) {
a = newA;
b = newB;
c = newC;
}
// Three getter methods for a , b , and c
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
// A method named getDiscriminant() that returns the discriminant, which is b^2 - 4ac.
public double getDiscriminant() {
return Math.pow(b, 2) - (4 * a * c);
}
// The methods named getRoot1() and getRoot2() for returning two roots of the equation
public double getRoot1() {
if (getDiscriminant() < 0 )
return 0;
else {
return (-b + Math.sqrt(getDiscriminant())) / (2 * a);
}
}
public double getRoot2() {
if (getDiscriminant() < 0 )
return 0;
else {
return (-b - Math.sqrt(getDiscriminant())) / (2 * a);
}
}
}