 ## 1. Overview

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.

## 2. The Solutions of a Quadratic Equation

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

### 2.1. The Roots of a Polynomial

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:

``````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.

### 2.2. Complex Number System

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:

``````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

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

• 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:

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

## 4. Get the Solutions

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

If the discriminant is strictly positive, the equation has two real solutions, (-b – √Δ) / 2a and (-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

If the discriminant is equal to zero, the equation has a unique real solution -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

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:

``````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

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:

``````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

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

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

### Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE 