Java Program to Find the Roots of a Quadratic Equation – 查找一元二次方程根的Java程序

最后修改: 2022年 9月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll see how to compute the solutions of a quadratic equation in Java. We’ll start by defining what a quadratic equation is, and then we’ll compute its solutions whether we work in the real or the complex number system.

在这篇文章中,我们将看到如何在Java中计算一元二次方程的解。我们将首先定义什么是一元二次方程,然后无论我们在实数还是复数系统中工作,我们都将计算其解。

2. The Solutions of a Quadratic Equation

2.一元二次方程的解

Given real numbers a ≠ 0, b and c, let’s consider the following quadratic equation: ax² + bx + c = 0.

给定实数a≠0,b和c,让我们考虑下面的二次方程。ax² + bx + c = 0

2.1. The Roots of a Polynomial

2.1.多项式的根

The solutions of this equation are also called the roots of the polynomial ax² + bx + c. Thus, let’s define a Polynom class. We’ll throw an IllegalArgumentException if the a coefficient is equal to 0:

这个方程的解也被称为多项式ax² + bx + c的根。因此,让我们定义一个Polynom类。如果a系数等于0,我们将抛出一个IllegalArgumentException

public class Polynom {

    private double a;
    private double b;
    private double c;

    public Polynom(double a, double b, double c) {
        if (a==0) {
            throw new IllegalArgumentException("a can not be equal to 0");
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // getters and setters
}

We’ll solve this equation in the real number system: for this, we’ll look for some Double solutions.

我们将在实数系统中解决这个方程:为此,我们将寻找一些Double解。

2.2. Complex Number System

2.2.复数系统

We’ll also show how to solve this equation in the complex number system. There is no default representation of a complex number in Java, so we’ll create our own. Let’s give it a static method ofReal to easily convert real numbers. This will be helpful in the following steps:

我们还将展示如何在复数系统中解决这个方程。Java中没有复数的默认表示法,所以我们将创建我们自己的复数。让我们给它一个static方法ofReal来轻松转换实数。这在下面的步骤中会有帮助。

public class Complex {

    private double realPart;
    private double imaginaryPart;

    public Complex(double realPart, double imaginaryPart) {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }

    public static Complex ofReal(double realPart) {
        return new Complex(realPart, 0);
    }

    // getters and setters
}

3. Calculate the Discriminant

3.计算判别式

The quantity Δ = b² – 4ac is called the discriminant of the quadratic equation. To calculate b squared in java, we have two solutions:

Δ=b²-4ac这个量被称为二次方程的判别式。要在java中计算b的平方,我们有两个解决方案。

  • multiply b by itself
  • use Math.pow to raise it to the power of 2

Let’s stick with the first method and add a getDiscriminant method to the Polynom class:

让我们坚持使用第一种方法,给Polynom类添加一个getDiscriminant方法。

public double getDiscriminant() {
    return b*b - 4*a*c;
}

4. Get the Solutions

4.获取解决方案

Depending on the value of the discriminant, we’re able to know how many solutions exist and compute them.

根据判别式的值,我们能够知道有多少个解决方案存在,并对其进行计算。

4.1. With a Strictly Positive Discriminant

4.1.有了严格意义上的正判别因素

If the discriminant is strictly positive, the equation has two real solutions, (-b – √Δ) / 2a and (-b + √Δ) / 2a:

如果判别式是严格的正数,那么该方程有两个实数解,即(-b – √Δ) / 2a和(-b + √Δ) / 2a:

Double solution1 = (-polynom.getB() - Math.sqrt(polynom.getDiscriminant())) / (2 * polynom.getA());
Double solution2 = (-polynom.getB() + Math.sqrt(polynom.getDiscriminant())) / (2 * polynom.getA());

If we work in the complex number system, we then just need to make the conversion:

如果我们在复数系统中工作,那么我们只需要进行转换。

Complex solution1 = Complex.ofReal((-polynom.getB() - Math.sqrt(polynom.getDiscriminant())) / (2 * polynom.getA()));
Complex solution2 = Complex.ofReal((-polynom.getB() + Math.sqrt(polynom.getDiscriminant())) / (2 * polynom.getA()));

4.2. With a Discriminant Equal to Zero

4.2.判别式等于零的情况下

If the discriminant is equal to zero, the equation has a unique real solution -b / 2a:

如果判别式等于零,该方程有一个唯一的实数解-b / 2a:

Double solution = (double) -polynom.getB() / (2 * polynom.getA());

Similarly, if we work in a complex number system, we’ll transform the solution in the following way:

同样地,如果我们在复数系统中工作,我们将以如下方式转换解决方案。

Complex solution = Complex.ofReal(-polynom.getB() / (2 * polynom.getA()));

4.3. With a Strictly Negative Discriminant

4.3.使用严格的负判别式

If the discriminant is strictly negative, the equation has no solution in the real number system. However, it can be solved in the complex number system: the solutions are (-b – i√-Δ) / 2a and its conjugate (-b + i√-Δ) / 2a:

如果判别式是严格的负数,该方程在实数系统中没有解。然而,它可以在复数系统中得到解决:解是(-b – i√-Δ) / 2a及其共轭物(-b + i√-Δ) / 2a:

Complex solution1 = new Complex(-polynom.getB() / (2* polynom.getA()), -Math.sqrt(-polynom.getDiscriminant()) / 2* polynom.getA());
Complex solution2 = new Complex(-polynom.getB() / (2* polynom.getA()), Math.sqrt(-polynom.getDiscriminant()) / 2* polynom.getA());

4.4. Gather the Results

4.4.收集结果

To sum up, let’s build a method that will fill in a List with the solutions of the equation when they exist. In the real number system, this method looks like this:

综上所述,让我们建立一个方法,当方程的解存在时,将其填入List在实数系统中,这个方法看起来像这样。

public static List<Double> getPolynomRoots(Polynom polynom) {
    List<Double> roots = new ArrayList<>();
    double discriminant = polynom.getDiscriminant();
    if (discriminant > 0) {
        roots.add((-polynom.getB() - Math.sqrt(discriminant)) / (2 * polynom.getA()));
        roots.add((-polynom.getB() + Math.sqrt(discriminant)) / (2 * polynom.getA()));
    } else if (discriminant == 0) {
        roots.add(-polynom.getB() / (2 * polynom.getA()));
    }
    return roots;
}

If we work in a complex number system, we’ll rather write:

如果我们在复数系统中工作,我们宁愿写。

public static List<Complex> getPolynomRoots(Polynom polynom) {
    List<Complex> roots = new ArrayList<>();
    double discriminant = polynom.getDiscriminant();
    if (discriminant > 0) {
        roots.add(Complex.ofReal((-polynom.getB() - Math.sqrt(discriminant)) / (2 * polynom.getA())));
        roots.add(Complex.ofReal((-polynom.getB() + Math.sqrt(discriminant)) / (2 * polynom.getA())));
    } else if (discriminant == 0) {
        roots.add(Complex.ofReal(-polynom.getB() / (2 * polynom.getA())));
    } else {
        roots.add(new Complex(-polynom.getB() / (2* polynom.getA()), -Math.sqrt(-discriminant) / 2* polynom.getA()));
        roots.add(new Complex(-polynom.getB() / (2* polynom.getA()), Math.sqrt(-discriminant) / 2* polynom.getA()));
    }
    return roots;
}

5. Conclusion

5.总结

In this tutorial, we’ve seen how to solve a quadratic equation in Java, whether we work with real or complex numbers.

在本教程中,我们已经看到了如何在Java中解决一元二次方程的问题,无论我们是用实数还是用复数工作。

As always, the code can be found over on GitHub.

一如既往,代码可以在GitHub上找到over