To solve this problem, we need to design a class to represent a quadratic equation and a test program to compute and display its roots based on the discriminant.
Approach
-
QuadraticEquation Class:
- Private Data Fields: Store coefficients
a,b, andcof the quadratic equationax² + bx + c = 0. - Constructor: Initialize the coefficients with given values.
- Getters: Retrieve the values of
a,b, andc. - Discriminant Calculation: Compute the discriminant using the formula
b² - 4ac. - Root Calculation: Compute the roots using the quadratic formula. If the discriminant is negative, return 0 (as per problem statement).
- Private Data Fields: Store coefficients
-
Test Program:
- Input Handling: Prompt the user to enter coefficients
a,b, andc. - Discriminant Check:
- If discriminant > 0: Display two distinct roots.
- If discriminant == 0: Display one real root.
- If discriminant < 0: Display no real roots.
- Input Handling: Prompt the user to enter coefficients
Solution Code
import java.util.Scanner;
public class TestQuadraticEquation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter coefficients a, b, c: ");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
QuadraticEquation equation = new QuadraticEquation(a, b, c);
double discriminant = equation.getDiscriminant();
if (discriminant > 0) {
double root1 = equation.getRoot1();
double root2 = equation.getRoot2();
System.out.printf("The roots are %.2f and %.2f%n", root1, root2);
} else if (discriminant == 0) {
double root = equation.getRoot1();
System.out.printf("The root is %.2f%n", root);
} else {
System.out.println("The equation has no real roots.");
}
scanner.close();
}
}
class QuadraticEquation {
private double a;
private double b;
private double c;
public QuadraticEquation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getA() { return a; }
public double getB() { return b; }
public double getC() { return c; }
public double getDiscriminant() {
return (b * b) - (4 * a * c);
}
public double getRoot1() {
double discriminant = getDiscriminant();
if (discriminant < 0) return 0;
return (-b + Math.sqrt(discriminant)) / (2 * a);
}
public double getRoot2() {
double discriminant = getDiscriminant();
if (discriminant < 0) return 0;
return (-b - Math.sqrt(discriminant)) / (2 * a);
}
}
Explanation
-
QuadraticEquation Class:
- The constructor initializes the coefficients
a,b, andc. getDiscriminant()calculates the discriminant, which determines the nature of the roots.getRoot1()andgetRoot2()compute the roots using the quadratic formula. If the discriminant is negative, they return 0 (since there are no real roots).
- The constructor initializes the coefficients
-
Test Program:
- Reads input coefficients from the user.
- Uses the
QuadraticEquationclass to compute the discriminant and roots. - Displays the appropriate result based on the discriminant value: two distinct roots, one real root, or no real roots.
This approach efficiently handles the quadratic equation and its roots, providing clear and correct output based on the discriminant. The code is modular and easy to understand, adhering to object-oriented principles.

作者声明:本文包含人工智能生成内容。