mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Add QuadraticEquationSolver and test cases (#5619)
This commit is contained in:
@ -377,6 +377,7 @@
|
||||
* [PrimeFactorization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PrimeFactorization.java)
|
||||
* [PronicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PronicNumber.java)
|
||||
* [PythagoreanTriple](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java)
|
||||
* [QuadraticEquationSolver](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java)
|
||||
* [ReverseNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ReverseNumber.java)
|
||||
* [RomanNumeralUtil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java)
|
||||
* [SecondMinMax](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SecondMinMax.java)
|
||||
|
@ -0,0 +1,60 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/**
|
||||
* This class represents a complex number which has real and imaginary part
|
||||
*/
|
||||
class ComplexNumber {
|
||||
Double real;
|
||||
Double imaginary;
|
||||
|
||||
ComplexNumber(double real, double imaginary) {
|
||||
this.real = real;
|
||||
this.imaginary = imaginary;
|
||||
}
|
||||
|
||||
ComplexNumber(double real) {
|
||||
this.real = real;
|
||||
this.imaginary = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Quadratic Equation Formula is used to find
|
||||
* the roots of a quadratic equation of the form ax^2 + bx + c = 0
|
||||
*
|
||||
* @see <a href="https://en.wikipedia.org/wiki/Quadratic_equation">Quadratic Equation</a>
|
||||
*/
|
||||
public class QuadraticEquationSolver {
|
||||
/**
|
||||
* Function takes in the coefficients of the quadratic equation
|
||||
*
|
||||
* @param a is the coefficient of x^2
|
||||
* @param b is the coefficient of x
|
||||
* @param c is the constant
|
||||
* @return roots of the equation which are ComplexNumber type
|
||||
*/
|
||||
public ComplexNumber[] solveEquation(double a, double b, double c) {
|
||||
double discriminant = b * b - 4 * a * c;
|
||||
|
||||
// if discriminant is positive, roots will be different
|
||||
if (discriminant > 0) {
|
||||
return new ComplexNumber[] {new ComplexNumber((-b + Math.sqrt(discriminant)) / (2 * a)), new ComplexNumber((-b - Math.sqrt(discriminant)) / (2 * a))};
|
||||
}
|
||||
|
||||
// if discriminant is zero, roots will be same
|
||||
if (discriminant == 0) {
|
||||
return new ComplexNumber[] {new ComplexNumber((-b) / (2 * a))};
|
||||
}
|
||||
|
||||
// if discriminant is negative, roots will have imaginary parts
|
||||
if (discriminant < 0) {
|
||||
double realPart = -b / (2 * a);
|
||||
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
|
||||
|
||||
return new ComplexNumber[] {new ComplexNumber(realPart, imaginaryPart), new ComplexNumber(realPart, -imaginaryPart)};
|
||||
}
|
||||
|
||||
// return no roots
|
||||
return new ComplexNumber[] {};
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class QuadraticEquationSolverTest {
|
||||
private final QuadraticEquationSolver quadraticEquationSolver = new QuadraticEquationSolver();
|
||||
|
||||
@Test
|
||||
public void testSolveEquationRealRoots() {
|
||||
// 4.2x^2 + 8x + 1.9 = 0
|
||||
double a = 4.2;
|
||||
double b = 8;
|
||||
double c = 1.9;
|
||||
|
||||
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
|
||||
Assertions.assertEquals(roots.length, 2);
|
||||
Assertions.assertEquals(roots[0].real, -0.27810465435684306);
|
||||
Assertions.assertNull(roots[0].imaginary);
|
||||
Assertions.assertEquals(roots[1].real, -1.6266572504050616);
|
||||
Assertions.assertNull(roots[1].imaginary);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSolveEquationEqualRoots() {
|
||||
// x^2 + 2x + 1 = 0
|
||||
double a = 1;
|
||||
double b = 2;
|
||||
double c = 1;
|
||||
|
||||
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
|
||||
Assertions.assertEquals(roots.length, 1);
|
||||
Assertions.assertEquals(roots[0].real, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSolveEquationComplexRoots() {
|
||||
// 2.3x^2 + 4x + 5.6 = 0
|
||||
double a = 2.3;
|
||||
double b = 4;
|
||||
double c = 5.6;
|
||||
|
||||
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
|
||||
Assertions.assertEquals(roots.length, 2);
|
||||
Assertions.assertEquals(roots[0].real, -0.8695652173913044);
|
||||
Assertions.assertEquals(roots[0].imaginary, 1.2956229935435948);
|
||||
Assertions.assertEquals(roots[1].real, -0.8695652173913044);
|
||||
Assertions.assertEquals(roots[1].imaginary, -1.2956229935435948);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user