From bec6b11d52cde3b8e983c29b0c0d9f6838a50366 Mon Sep 17 00:00:00 2001 From: "sailok.chinta" Date: Mon, 7 Oct 2024 11:36:52 +0530 Subject: [PATCH 1/6] feat: add QuadraticEquationSolver and test cases --- DIRECTORY.md | 1 + .../maths/QuadraticEquationSolver.java | 57 +++++++++++++++++++ .../maths/QuadraticEquationSolverTest.java | 48 ++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java create mode 100644 src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index d93dde3dffb6..8cae8e3f0127 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -369,6 +369,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) diff --git a/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java b/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java new file mode 100644 index 000000000000..268be55d9cd0 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java @@ -0,0 +1,57 @@ +package com.thealgorithms.maths; + +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 Quadratic Equation + */ +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[] {}; + } +} diff --git a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java new file mode 100644 index 000000000000..46345dcf5a63 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java @@ -0,0 +1,48 @@ +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 testSolveEquation_realRoots() { + // 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, -1.6266572504051); + Assertions.assertEquals(roots[1].real, -0.27810465435684); + } + + @Test + public void testSolveEquation_equalRoots() { + // 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 testSolveEquation_imaginaryRoots() { + // 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.8695652173913); + Assertions.assertEquals(roots[0].imaginary, 1.2956229935436); + Assertions.assertEquals(roots[1].real, -0.8695652173913); + Assertions.assertEquals(roots[1].imaginary, -1.2956229935436); + } +} From 77be52cf789fa42753a3c531f78d7cd30dd559a0 Mon Sep 17 00:00:00 2001 From: "sailok.chinta" Date: Mon, 7 Oct 2024 11:40:31 +0530 Subject: [PATCH 2/6] feat: fix test cases --- .../thealgorithms/maths/QuadraticEquationSolverTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java index 46345dcf5a63..954fd801da61 100644 --- a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java +++ b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java @@ -40,9 +40,9 @@ public void testSolveEquation_imaginaryRoots() { ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c); Assertions.assertEquals(roots.length, 2); - Assertions.assertEquals(roots[0].real, -0.8695652173913); - Assertions.assertEquals(roots[0].imaginary, 1.2956229935436); - Assertions.assertEquals(roots[1].real, -0.8695652173913); - Assertions.assertEquals(roots[1].imaginary, -1.2956229935436); + Assertions.assertEquals(roots[0].real, -0.8695652173913044); + Assertions.assertEquals(roots[0].imaginary, 0.27810465435684306); + Assertions.assertEquals(roots[1].real, -0.8695652173913044); + Assertions.assertEquals(roots[1].imaginary, -0.27810465435684306); } } From 74d6ab5b23e875e472dc212aa5c0bfb45f0a108c Mon Sep 17 00:00:00 2001 From: "sailok.chinta" Date: Mon, 7 Oct 2024 11:43:00 +0530 Subject: [PATCH 3/6] feat: fix test cases --- .../thealgorithms/maths/QuadraticEquationSolverTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java index 954fd801da61..77271842d779 100644 --- a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java +++ b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java @@ -16,7 +16,7 @@ public void testSolveEquation_realRoots() { ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c); Assertions.assertEquals(roots.length, 2); Assertions.assertEquals(roots[0].real, -1.6266572504051); - Assertions.assertEquals(roots[1].real, -0.27810465435684); + Assertions.assertEquals(roots[1].real, -0.27810465435684306); } @Test @@ -41,8 +41,8 @@ public void testSolveEquation_imaginaryRoots() { ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c); Assertions.assertEquals(roots.length, 2); Assertions.assertEquals(roots[0].real, -0.8695652173913044); - Assertions.assertEquals(roots[0].imaginary, 0.27810465435684306); + Assertions.assertEquals(roots[0].imaginary, 1.2956229935435948); Assertions.assertEquals(roots[1].real, -0.8695652173913044); - Assertions.assertEquals(roots[1].imaginary, -0.27810465435684306); + Assertions.assertEquals(roots[1].imaginary, -1.2956229935435948); } } From 40d7e7aaa53e4c223fdc7cdf0888f79991b29bf6 Mon Sep 17 00:00:00 2001 From: "sailok.chinta" Date: Mon, 7 Oct 2024 11:49:59 +0530 Subject: [PATCH 4/6] feat: fix test cases --- .../com/thealgorithms/maths/QuadraticEquationSolver.java | 4 +++- .../thealgorithms/maths/QuadraticEquationSolverTest.java | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java b/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java index 268be55d9cd0..4544c345658c 100644 --- a/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java +++ b/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java @@ -1,5 +1,7 @@ package com.thealgorithms.maths; +import java.util.Arrays; + class ComplexNumber { Double real; Double imaginary; @@ -48,7 +50,7 @@ public ComplexNumber[] solveEquation(double a, double b, double c) { double realPart = -b / (2 * a); double imaginaryPart = Math.sqrt(-discriminant) / (2 * a); - return new ComplexNumber[] {new ComplexNumber(realPart, imaginaryPart), new ComplexNumber(realPart, imaginaryPart)}; + return new ComplexNumber[] {new ComplexNumber(realPart, imaginaryPart), new ComplexNumber(realPart, -imaginaryPart)}; } // return no roots diff --git a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java index 77271842d779..0ec1c7d71bd5 100644 --- a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java +++ b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java @@ -15,8 +15,10 @@ public void testSolveEquation_realRoots() { ComplexNumber[] roots = quadraticEquationSolver.solveEquation(a, b, c); Assertions.assertEquals(roots.length, 2); - Assertions.assertEquals(roots[0].real, -1.6266572504051); - Assertions.assertEquals(roots[1].real, -0.27810465435684306); + 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 From f749b8209789b7db8e2661a5754f479b7a344815 Mon Sep 17 00:00:00 2001 From: "sailok.chinta" Date: Mon, 7 Oct 2024 11:52:58 +0530 Subject: [PATCH 5/6] feat: add comments --- .../com/thealgorithms/maths/QuadraticEquationSolver.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java b/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java index 4544c345658c..cd654c5dc023 100644 --- a/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java +++ b/src/main/java/com/thealgorithms/maths/QuadraticEquationSolver.java @@ -1,7 +1,8 @@ package com.thealgorithms.maths; -import java.util.Arrays; - +/** + * This class represents a complex number which has real and imaginary part + */ class ComplexNumber { Double real; Double imaginary; From c19da9237f154bf6697da5cc4bafce62bf041854 Mon Sep 17 00:00:00 2001 From: "sailok.chinta" Date: Mon, 7 Oct 2024 11:54:01 +0530 Subject: [PATCH 6/6] feat: fix checkstyle errors --- .../thealgorithms/maths/QuadraticEquationSolverTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java index 0ec1c7d71bd5..a2046511ddf5 100644 --- a/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java +++ b/src/test/java/com/thealgorithms/maths/QuadraticEquationSolverTest.java @@ -7,7 +7,7 @@ public class QuadraticEquationSolverTest { private final QuadraticEquationSolver quadraticEquationSolver = new QuadraticEquationSolver(); @Test - public void testSolveEquation_realRoots() { + public void testSolveEquationRealRoots() { // 4.2x^2 + 8x + 1.9 = 0 double a = 4.2; double b = 8; @@ -22,7 +22,7 @@ public void testSolveEquation_realRoots() { } @Test - public void testSolveEquation_equalRoots() { + public void testSolveEquationEqualRoots() { // x^2 + 2x + 1 = 0 double a = 1; double b = 2; @@ -34,7 +34,7 @@ public void testSolveEquation_equalRoots() { } @Test - public void testSolveEquation_imaginaryRoots() { + public void testSolveEquationComplexRoots() { // 2.3x^2 + 4x + 5.6 = 0 double a = 2.3; double b = 4;