From 75434a8a675a9fafe9ba7b2dd7ffcbc8fe03a807 Mon Sep 17 00:00:00 2001 From: arthurfellipe-dev Date: Thu, 2 Apr 2026 10:04:11 -0300 Subject: [PATCH 1/6] Add LinearEquation to maths --- .../thealgorithms/maths/LinearEquation.java | 25 ++++++++++++ .../maths/LinearEquationTest.java | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/LinearEquation.java create mode 100644 src/test/java/com/thealgorithms/maths/LinearEquationTest.java diff --git a/src/main/java/com/thealgorithms/maths/LinearEquation.java b/src/main/java/com/thealgorithms/maths/LinearEquation.java new file mode 100644 index 000000000000..4f219281e6fc --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/LinearEquation.java @@ -0,0 +1,25 @@ +package com.thealgorithms.maths; + +/** + * Solves linear equations of the form ax + b = 0. + */ +public final class LinearEquation { + + private LinearEquation() { + } + + /** + * Solves the equation ax + b = 0 and returns the value of x. + * + * @param a the coefficient of x, must not be zero + * @param b the constant term + * @return the value of x that satisfies the equation + * @throws IllegalArgumentException if a is zero + */ + public static double solve(final double a, final double b) { + if (a == 0) { + throw new IllegalArgumentException("Coefficient 'a' must not be zero"); + } + return -b / a; + } +} diff --git a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java new file mode 100644 index 000000000000..501310ad49de --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java @@ -0,0 +1,39 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +class LinearEquationTest { + + @Test + void testSolveBasic() { + assertEquals(3.0, LinearEquation.solve(2, -6)); + } + + @Test + void testSolveNegativeResult() { + assertEquals(-5.0, LinearEquation.solve(1, 5)); + } + + @Test + void testSolveWithZeroB() { + assertEquals(0.0, LinearEquation.solve(4, 0), 1e-9); + } + + @Test + void testSolveWithNegativeA() { + assertEquals(3.0, LinearEquation.solve(-3, 9)); + } + + @Test + void testAllIllegalInput() { + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 5)), + () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 0)), + () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, -3)) + ); + } +} \ No newline at end of file From a8f37a802ddf11b72443173e29f86d8f5b8d3b86 Mon Sep 17 00:00:00 2001 From: arthurfellipe-dev Date: Thu, 2 Apr 2026 10:08:04 -0300 Subject: [PATCH 2/6] Add Wikipedia reference to LinearEquation --- src/main/java/com/thealgorithms/maths/LinearEquation.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/thealgorithms/maths/LinearEquation.java b/src/main/java/com/thealgorithms/maths/LinearEquation.java index 4f219281e6fc..ab14717dd2d9 100644 --- a/src/main/java/com/thealgorithms/maths/LinearEquation.java +++ b/src/main/java/com/thealgorithms/maths/LinearEquation.java @@ -3,6 +3,12 @@ /** * Solves linear equations of the form ax + b = 0. */ + +/** + * Solves linear equations of the form ax + b = 0. + * + * @see Linear equation (Wikipedia) + */ public final class LinearEquation { private LinearEquation() { From 387d1fb3730f27c8cc7b2c24e3f79ac92e3645d4 Mon Sep 17 00:00:00 2001 From: arthurfellipe-dev Date: Thu, 2 Apr 2026 10:19:08 -0300 Subject: [PATCH 3/6] Fix clang-format style in LinearEquationTest --- .../java/com/thealgorithms/maths/LinearEquationTest.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java index 501310ad49de..574f8b0d4b8c 100644 --- a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java +++ b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java @@ -30,10 +30,7 @@ void testSolveWithNegativeA() { @Test void testAllIllegalInput() { - assertAll( - () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 5)), - () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 0)), - () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, -3)) - ); + assertAll(() -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 5)), () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 0)), () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, -3))); + } } \ No newline at end of file From d56f98c461095ade898e129bfaec0b7a33c379fb Mon Sep 17 00:00:00 2001 From: arthurfellipe-dev Date: Thu, 2 Apr 2026 10:28:30 -0300 Subject: [PATCH 4/6] Fix clang-format style in LinearEquationTest --- src/test/java/com/thealgorithms/maths/LinearEquationTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java index 574f8b0d4b8c..05763cfb311c 100644 --- a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java +++ b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java @@ -31,6 +31,5 @@ void testSolveWithNegativeA() { @Test void testAllIllegalInput() { assertAll(() -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 5)), () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 0)), () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, -3))); - } } \ No newline at end of file From 5e9209010f9a152f77b49704be53b0c7cddb5be6 Mon Sep 17 00:00:00 2001 From: arthurfellipe-dev Date: Thu, 2 Apr 2026 10:39:25 -0300 Subject: [PATCH 5/6] Fix clang-format style in LinearEquationTest --- src/test/java/com/thealgorithms/maths/LinearEquationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java index 05763cfb311c..d6ef64ead47b 100644 --- a/src/test/java/com/thealgorithms/maths/LinearEquationTest.java +++ b/src/test/java/com/thealgorithms/maths/LinearEquationTest.java @@ -32,4 +32,4 @@ void testSolveWithNegativeA() { void testAllIllegalInput() { assertAll(() -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 5)), () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, 0)), () -> assertThrows(IllegalArgumentException.class, () -> LinearEquation.solve(0, -3))); } -} \ No newline at end of file +} From 44e12626f4a8ab88c563c13a0b8877522db00adb Mon Sep 17 00:00:00 2001 From: arthurfellipe-dev Date: Thu, 2 Apr 2026 10:50:36 -0300 Subject: [PATCH 6/6] Fix clang-format style in LinearEquationTest --- src/main/java/com/thealgorithms/maths/LinearEquation.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/LinearEquation.java b/src/main/java/com/thealgorithms/maths/LinearEquation.java index ab14717dd2d9..407b63a45a71 100644 --- a/src/main/java/com/thealgorithms/maths/LinearEquation.java +++ b/src/main/java/com/thealgorithms/maths/LinearEquation.java @@ -1,9 +1,5 @@ package com.thealgorithms.maths; -/** - * Solves linear equations of the form ax + b = 0. - */ - /** * Solves linear equations of the form ax + b = 0. *