Skip to content

Commit bd9e324

Browse files
authored
Add QuadraticEquationSolver and test cases (#5619)
1 parent 5dcf6c0 commit bd9e324

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
* [PrimeFactorization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PrimeFactorization.java)
378378
* [PronicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PronicNumber.java)
379379
* [PythagoreanTriple](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java)
380+
* [QuadraticEquationSolver](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java)
380381
* [ReverseNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ReverseNumber.java)
381382
* [RomanNumeralUtil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java)
382383
* [SecondMinMax](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SecondMinMax.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* This class represents a complex number which has real and imaginary part
5+
*/
6+
class ComplexNumber {
7+
Double real;
8+
Double imaginary;
9+
10+
ComplexNumber(double real, double imaginary) {
11+
this.real = real;
12+
this.imaginary = imaginary;
13+
}
14+
15+
ComplexNumber(double real) {
16+
this.real = real;
17+
this.imaginary = null;
18+
}
19+
}
20+
21+
/**
22+
* Quadratic Equation Formula is used to find
23+
* the roots of a quadratic equation of the form ax^2 + bx + c = 0
24+
*
25+
* @see <a href="https://en.wikipedia.org/wiki/Quadratic_equation">Quadratic Equation</a>
26+
*/
27+
public class QuadraticEquationSolver {
28+
/**
29+
* Function takes in the coefficients of the quadratic equation
30+
*
31+
* @param a is the coefficient of x^2
32+
* @param b is the coefficient of x
33+
* @param c is the constant
34+
* @return roots of the equation which are ComplexNumber type
35+
*/
36+
public ComplexNumber[] solveEquation(double a, double b, double c) {
37+
double discriminant = b * b - 4 * a * c;
38+
39+
// if discriminant is positive, roots will be different
40+
if (discriminant > 0) {
41+
return new ComplexNumber[] {new ComplexNumber((-b + Math.sqrt(discriminant)) / (2 * a)), new ComplexNumber((-b - Math.sqrt(discriminant)) / (2 * a))};
42+
}
43+
44+
// if discriminant is zero, roots will be same
45+
if (discriminant == 0) {
46+
return new ComplexNumber[] {new ComplexNumber((-b) / (2 * a))};
47+
}
48+
49+
// if discriminant is negative, roots will have imaginary parts
50+
if (discriminant < 0) {
51+
double realPart = -b / (2 * a);
52+
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
53+
54+
return new ComplexNumber[] {new ComplexNumber(realPart, imaginaryPart), new ComplexNumber(realPart, -imaginaryPart)};
55+
}
56+
57+
// return no roots
58+
return new ComplexNumber[] {};
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class QuadraticEquationSolverTest {
7+
private final QuadraticEquationSolver quadraticEquationSolver = new QuadraticEquationSolver();
8+
9+
@Test
10+
public void testSolveEquationRealRoots() {
11+
// 4.2x^2 + 8x + 1.9 = 0
12+
double a = 4.2;
13+
double b = 8;
14+
double c = 1.9;
15+
16+
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
17+
Assertions.assertEquals(roots.length, 2);
18+
Assertions.assertEquals(roots[0].real, -0.27810465435684306);
19+
Assertions.assertNull(roots[0].imaginary);
20+
Assertions.assertEquals(roots[1].real, -1.6266572504050616);
21+
Assertions.assertNull(roots[1].imaginary);
22+
}
23+
24+
@Test
25+
public void testSolveEquationEqualRoots() {
26+
// x^2 + 2x + 1 = 0
27+
double a = 1;
28+
double b = 2;
29+
double c = 1;
30+
31+
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
32+
Assertions.assertEquals(roots.length, 1);
33+
Assertions.assertEquals(roots[0].real, -1);
34+
}
35+
36+
@Test
37+
public void testSolveEquationComplexRoots() {
38+
// 2.3x^2 + 4x + 5.6 = 0
39+
double a = 2.3;
40+
double b = 4;
41+
double c = 5.6;
42+
43+
ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c);
44+
Assertions.assertEquals(roots.length, 2);
45+
Assertions.assertEquals(roots[0].real, -0.8695652173913044);
46+
Assertions.assertEquals(roots[0].imaginary, 1.2956229935435948);
47+
Assertions.assertEquals(roots[1].real, -0.8695652173913044);
48+
Assertions.assertEquals(roots[1].imaginary, -1.2956229935435948);
49+
}
50+
}

0 commit comments

Comments
 (0)