BAEL-5709 code for the Finding the roots of a quadratic equation article
This commit is contained in:
parent
3d17a48aa2
commit
faaebaddbe
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public double getRealPart() {
|
||||
return realPart;
|
||||
}
|
||||
|
||||
public double getImaginaryPart() {
|
||||
return imaginaryPart;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ComplexRootsCalculator {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public double getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public double getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public double getC() {
|
||||
return c;
|
||||
}
|
||||
|
||||
public double getDiscriminant() {
|
||||
return b * b - 4 * a * c;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class RealRootsCalculator {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ComplexRootsCalculatorUnitTest {
|
||||
|
||||
@Test
|
||||
void givenPolynomWithStrictlyPositiveDiscriminant_whenGetPolynomRoots_ThenReturnBothRealRoots() {
|
||||
Polynom polynom = new Polynom(1d, 1d, -6d);
|
||||
List<Complex> roots = ComplexRootsCalculator.getPolynomRoots(polynom);
|
||||
assertEquals(2, roots.size());
|
||||
assertTrue(roots.stream().anyMatch(c -> c.getRealPart() == 2d && c.getImaginaryPart() == 0));
|
||||
assertTrue(roots.stream().anyMatch(c -> c.getRealPart() == -3d && c.getImaginaryPart() == 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPolynomWithDiscriminantEqualsZero_whenGetPolynomRoots_ThenReturnRoot() {
|
||||
Polynom polynom = new Polynom(1d, 4d, 4d);
|
||||
List<Complex> roots = ComplexRootsCalculator.getPolynomRoots(polynom);
|
||||
assertEquals(1, roots.size());
|
||||
assertTrue(roots.get(0).getRealPart() == -2d && roots.get(0).getImaginaryPart() == 0d);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPolynomWithStrictlyNegativeDiscriminant_whenGetPolynomRoots_ThenReturnBothComplexRoot() {
|
||||
Polynom polynom = new Polynom(1d, -4d, 8d);
|
||||
List<Complex> roots = ComplexRootsCalculator.getPolynomRoots(polynom);
|
||||
assertEquals(2, roots.size());
|
||||
assertTrue(roots.stream().anyMatch(c -> c.getRealPart() == 2d && c.getImaginaryPart() == 2d));
|
||||
assertTrue(roots.stream().anyMatch(c -> c.getRealPart() == 2d && c.getImaginaryPart() == -2d));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class PolynomUnitTest {
|
||||
|
||||
@Test
|
||||
void givenaEqualTo0_WhenNewPolynom_ThenThrows() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Polynom(0, 1, 1));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.math.quadraticequationroot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class RealRootsCalculatorUnitTest {
|
||||
|
||||
@Test
|
||||
void givenPolynomWithStrictlyPositiveDiscriminant_whenGetPolynomRoots_ThenReturnBothRoots() {
|
||||
Polynom polynom = new Polynom(1d, 1d, -6d);
|
||||
List<Double> roots = RealRootsCalculator.getPolynomRoots(polynom);
|
||||
assertEquals(2, roots.size());
|
||||
assertTrue(roots.containsAll(Arrays.asList(2d, -3d)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPolynomWithDiscriminantEqualsZero_whenGetPolynomRoots_ThenReturnRoot() {
|
||||
Polynom polynom = new Polynom(1d, 4d, 4d);
|
||||
List<Double> roots = RealRootsCalculator.getPolynomRoots(polynom);
|
||||
assertEquals(1, roots.size());
|
||||
assertTrue(roots.get(0).equals(-2d));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenPolynomWithStrictlyNegativeDiscriminant_whenGetPolynomRoots_ThenReturnNoRoot() {
|
||||
Polynom polynom = new Polynom(3d, 2d, 5d);
|
||||
List<Double> roots = RealRootsCalculator.getPolynomRoots(polynom);
|
||||
assertEquals(0, roots.size());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue