Repository: TheAlgorithms/Java
Pull request #6193 (2025-03-13 08:58:55)

Added Parenthesis
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6193?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `22.00000%` with `78 lines` in your changes missing coverage. Please review.
> Project coverage is 73.74%. Comparing base [(`e6073f8`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/e6073f8fefa7f1b63a9170ac56712f04469c5442?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`2878983`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/2878983fc8a1d124786a67f63f71a47bed2c36c8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6193?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...main/java/com/thealgorithms/maths/MathBuilder.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6193?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmaths%2FMathBuilder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9tYXRocy9NYXRoQnVpbGRlci5qYXZh) | 22.00% | [57 Missing and 21 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6193?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6193      +/-   ##
============================================
- Coverage     73.78%   73.74%   -0.05%     
- Complexity     5219     5220       +1     
============================================
  Files           667      667              
  Lines         17983    18016      +33     
  Branches       3457     3487      +30     
============================================
+ Hits          13269    13286      +17     
- Misses         4196     4202       +6     
- Partials        518      528      +10     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6193?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>🚀 New features to boost your workflow: </summary>

- ❄ [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java
index 3534749d..1cf3d8b7 100644
--- a/src/main/java/com/thealgorithms/maths/MathBuilder.java
+++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java
@@ -26,7 +26,7 @@ public final class MathBuilder {
     public long toLong() {
         try {
             if (Double.isNaN(result)) {
-                throw new IllegalArgumentException("Cannot convert NaN to long");
+                throw new IllegalArgumentException("Cannot convert NaN to long!");
             }
             if (result == Double.POSITIVE_INFINITY) {
                 return Long.MAX_VALUE;
@@ -48,6 +48,8 @@ public final class MathBuilder {
 
     public static class Builder {
         private double number;
+        private double sideNumber;
+        private boolean inParenthesis;
         private double memory = 0;
 
         public Builder() {
@@ -59,26 +61,44 @@ public final class MathBuilder {
         }
 
         public Builder add(double num) {
-            number += num;
+            if (inParenthesis) {
+                sideNumber += num;
+            } else {
+                number += num;
+            }
             return this;
         }
 
         // Takes a number and a condition, only does the operation if condition is true.
         public Builder addIf(double num, BiFunction<Double, Double, Boolean> condition) {
-            if (condition.apply(number, num)) {
+            if (!condition.apply(number, num)) {
+                return this;
+            }
+            if (inParenthesis) {
+                sideNumber += num;
+            } else {
                 number += num;
             }
             return this;
         }
 
         public Builder minus(double num) {
-            number -= num;
+            if (inParenthesis) {
+                sideNumber -= num;
+            } else {
+                number -= num;
+            }
             return this;
         }
 
         // Takes a number and a condition, only does the operation if condition is true.
         public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition) {
-            if (condition.apply(number, num)) {
+            if (!condition.apply(number, num)) {
+                return this;
+            }
+            if (inParenthesis) {
+                sideNumber -= num;
+            } else {
                 number -= num;
             }
             return this;
@@ -113,7 +133,6 @@ public final class MathBuilder {
         }
 
         public Builder randomInRange(double min, double max) {
-
             if (number != 0) {
                 throw new RuntimeException("Number must be zero for random assignment!");
             }
@@ -123,28 +142,49 @@ public final class MathBuilder {
         }
 
         public Builder toDegrees() {
-            number = Math.toDegrees(number);
+            if (inParenthesis) {
+                sideNumber = Math.toDegrees(sideNumber);
+            } else {
+                number = Math.toDegrees(number);
+            }
             return this;
         }
 
         public Builder max(double num) {
-            number = Math.max(number, num);
+            if (inParenthesis) {
+                sideNumber = Math.max(sideNumber, num);
+            } else {
+                number = Math.max(number, num);
+            }
             return this;
         }
 
         public Builder min(double num) {
-            number = Math.min(number, num);
+            if (inParenthesis) {
+                sideNumber = Math.min(sideNumber, num);
+            } else {
+                number = Math.min(number, num);
+            }
             return this;
         }
 
         public Builder multiply(double num) {
-            number *= num;
+            if (inParenthesis) {
+                sideNumber *= num;
+            } else {
+                number *= num;
+            }
             return this;
         }
 
         // Takes a number and a condition, only does the operation if condition is true.
         public Builder multiplyIf(double num, BiFunction<Double, Double, Boolean> condition) {
-            if (condition.apply(number, num)) {
+            if (!condition.apply(number, num)) {
+                return this;
+            }
+            if (inParenthesis) {
+                sideNumber *= num;
+            } else {
                 number *= num;
             }
             return this;
@@ -154,7 +194,11 @@ public final class MathBuilder {
             if (num == 0) {
                 return this;
             }
-            number /= num;
+            if (inParenthesis) {
+                sideNumber /= num;
+            } else {
+                number /= num;
+            }
             return this;
         }
 
@@ -163,107 +207,189 @@ public final class MathBuilder {
             if (num == 0) {
                 return this;
             }
-            if (condition.apply(number, num)) {
+            if (!condition.apply(number, num)) {
+                return this;
+            }
+            if (inParenthesis) {
+                sideNumber /= num;
+            } else {
                 number /= num;
             }
             return this;
         }
 
         public Builder mod(double num) {
-            number %= num;
+            if (inParenthesis) {
+                sideNumber %= num;
+            } else {
+                number %= num;
+            }
             return this;
         }
 
         // Takes a number and a condition, only does the operation if condition is true.
         public Builder modIf(double num, BiFunction<Double, Double, Boolean> condition) {
-            if (condition.apply(number, num)) {
+            if (!condition.apply(number, num)) {
+                return this;
+            }
+            if (inParenthesis) {
+                sideNumber %= num;
+            } else {
                 number %= num;
             }
             return this;
         }
 
         public Builder pow(double num) {
-            number = Math.pow(number, num);
+            if (inParenthesis) {
+                sideNumber = Math.pow(sideNumber, num);
+            } else {
+                number = Math.pow(number, num);
+            }
             return this;
         }
 
         public Builder sqrt() {
-            number = Math.sqrt(number);
+            if (inParenthesis) {
+                sideNumber = Math.sqrt(sideNumber);
+            } else {
+                number = Math.sqrt(number);
+            }
             return this;
         }
 
         public Builder round() {
-            number = Math.round(number);
+            if (inParenthesis) {
+                sideNumber = Math.round(sideNumber);
+            } else {
+                number = Math.round(number);
+            }
             return this;
         }
 
         public Builder floor() {
-            number = Math.floor(number);
+            if (inParenthesis) {
+                sideNumber = Math.floor(sideNumber);
+            } else {
+                number = Math.floor(number);
+            }
             return this;
         }
 
         public Builder ceil() {
-            number = Math.ceil(number);
+            if (inParenthesis) {
+                sideNumber = Math.ceil(sideNumber);
+            } else {
+                number = Math.ceil(number);
+            }
             return this;
         }
 
         public Builder abs() {
-            number = Math.abs(number);
+            if (inParenthesis) {
+                sideNumber = Math.abs(sideNumber);
+            } else {
+                number = Math.abs(number);
+            }
             return this;
         }
 
         public Builder cbrt() {
-            number = Math.cbrt(number);
+            if (inParenthesis) {
+                sideNumber = Math.cbrt(sideNumber);
+            } else {
+                number = Math.cbrt(number);
+            }
             return this;
         }
 
         public Builder log() {
-            number = Math.log(number);
+            if (inParenthesis) {
+                sideNumber = Math.log(sideNumber);
+            } else {
+                number = Math.log(number);
+            }
             return this;
         }
 
         public Builder log10() {
-            number = Math.log10(number);
+            if (inParenthesis) {
+                sideNumber = Math.log10(sideNumber);
+            } else {
+                number = Math.log10(number);
+            }
             return this;
         }
 
         public Builder sin() {
-            number = Math.sin(number);
+            if (inParenthesis) {
+                sideNumber = Math.sin(sideNumber);
+            } else {
+                number = Math.sin(number);
+            }
             return this;
         }
 
         public Builder cos() {
-            number = Math.cos(number);
+            if (inParenthesis) {
+                sideNumber = Math.cos(sideNumber);
+            } else {
+                number = Math.cos(number);
+            }
             return this;
         }
 
         public Builder tan() {
-            number = Math.tan(number);
+            if (inParenthesis) {
+                sideNumber = Math.tan(sideNumber);
+            } else {
+                number = Math.tan(number);
+            }
             return this;
         }
 
         public Builder sinh() {
-            number = Math.sinh(number);
+            if (inParenthesis) {
+                sideNumber = Math.sinh(sideNumber);
+            } else {
+                number = Math.sinh(number);
+            }
             return this;
         }
 
         public Builder cosh() {
-            number = Math.cosh(number);
+            if (inParenthesis) {
+                sideNumber = Math.cosh(sideNumber);
+            } else {
+                number = Math.cosh(number);
+            }
             return this;
         }
 
         public Builder tanh() {
-            number = Math.tanh(number);
+            if (inParenthesis) {
+                sideNumber = Math.tanh(sideNumber);
+            } else {
+                number = Math.tanh(number);
+            }
             return this;
         }
 
         public Builder exp() {
-            number = Math.exp(number);
+            if (inParenthesis) {
+                sideNumber = Math.exp(sideNumber);
+            } else {
+                number = Math.exp(number);
+            }
             return this;
         }
 
         public Builder toRadians() {
-            number = Math.toRadians(number);
+            if (inParenthesis) {
+                sideNumber = Math.toRadians(sideNumber);
+            } else {
+                number = Math.toRadians(number);
+            }
             return this;
         }
 
@@ -279,7 +405,6 @@ public final class MathBuilder {
             if (cleanMemory) {
                 memory = 0;
             }
-
             return this;
         }
 
@@ -292,7 +417,6 @@ public final class MathBuilder {
             if (cleanMemory) {
                 memory = 0;
             }
-
             return this;
         }
 
@@ -322,6 +446,40 @@ public final class MathBuilder {
             return this;
         }
 
+        public Builder openParenthesis(double num) {
+            sideNumber = num;
+            inParenthesis = true;
+            return this;
+        }
+
+        public Builder closeParenthesisAndPlus() {
+            number += sideNumber;
+            inParenthesis = false;
+            sideNumber = 0;
+            return this;
+        }
+
+        public Builder closeParenthesisAndMinus() {
+            number -= sideNumber;
+            inParenthesis = false;
+            sideNumber = 0;
+            return this;
+        }
+
+        public Builder closeParenthesisAndMultiply() {
+            number *= sideNumber;
+            inParenthesis = false;
+            sideNumber = 0;
+            return this;
+        }
+
+        public Builder closeParenthesisAndDivide() {
+            number /= sideNumber;
+            inParenthesis = false;
+            sideNumber = 0;
+            return this;
+        }
+
         public Builder format(String format) {
             DecimalFormat formater = new DecimalFormat(format);
             String num = formater.format(number);


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
index b6ecc674..dc381bfc 100644
--- a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
+++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
@@ -35,4 +35,18 @@ class MathBuilderTest {
         long[] expectedSalaries = {1840, 3036, 4048, 5060};
         assertArrayEquals(expectedSalaries, processedSalaries);
     }
+
+    @Test
+    void parenthesis() {
+        // 10 + (20*5) - 40 + (100 / 10) = 80
+        double result = new MathBuilder.Builder(10).openParenthesis(20).multiply(5).closeParenthesisAndPlus().minus(40).openParenthesis(100).divide(10).closeParenthesisAndPlus().build().get();
+        assertEquals(80, result);
+    }
+
+    @Test
+    void areaOfCircle() {
+        // Radius is 4
+        double area = new MathBuilder.Builder().pi().openParenthesis(4).multiply(4).closeParenthesisAndMultiply().build().get();
+        assertEquals(Math.PI * 4 * 4, area);
+    }
 }