内容中心

2026年汽车顶棚水刺无纺布厂家推荐哪家好-常熟市永得利水刺无纺布有限公司

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

  1. QuadraticEquation Class:

    • Private Data Fields: Store coefficients a, b, and c of the quadratic equation ax² + bx + c = 0.
    • Constructor: Initialize the coefficients with given values.
    • Getters: Retrieve the values of a, b, and c.
    • 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).
  2. Test Program:

    • Input Handling: Prompt the user to enter coefficients a, b, and c.
    • Discriminant Check:
      • If discriminant > 0: Display two distinct roots.
      • If discriminant == 0: Display one real root.
      • If discriminant < 0: Display no real roots.

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, and c.
    • getDiscriminant() calculates the discriminant, which determines the nature of the roots.
    • getRoot1() and getRoot2() compute the roots using the quadratic formula. If the discriminant is negative, they return 0 (since there are no real roots).
  • Test Program:

    • Reads input coefficients from the user.
    • Uses the QuadraticEquation class 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.

常熟市永得利水刺无纺布有限公司



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

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?