Repository: TheAlgorithms/Java
Pull request #6190 (2025-03-10 03:31:24)

Merged Changes
<!--
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/6190?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 `35.36585%` with `106 lines` in your changes missing coverage. Please review.
> Project coverage is 73.79%. Comparing base [(`9bfc05a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9bfc05ad8d1e4449e9c3fc9cd10320e043f8506f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`ca012ad`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ca012ada3c0e3f054414819a9cf755892ec93c25?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/6190?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/6190?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) | 27.08% | [95 Missing and 10 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6190?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |
| [...healgorithms/others/PrintAMatrixInSpiralOrder.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6190?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fothers%2FPrintAMatrixInSpiralOrder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9vdGhlcnMvUHJpbnRBTWF0cml4SW5TcGlyYWxPcmRlci5qYXZh) | 95.00% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6190?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    #6190      +/-   ##
============================================
- Coverage     74.16%   73.79%   -0.37%     
- Complexity     5208     5220      +12     
============================================
  Files           665      667       +2     
  Lines         17818    17982     +164     
  Branches       3429     3457      +28     
============================================
+ Hits          13214    13270      +56     
- Misses         4099     4195      +96     
- Partials        505      517      +12     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6190?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>
sadiul-hakim: ![{1FB1E1B7-B06D-42B3-A414-014A0E5DDD30}](https://github.com/user-attachments/assets/3ee93d84-0cf9-4fe8-bb04-56177c05063d)
I do not know how to fix this one. No matter whatever i do it keeps complaining.
sadiul-hakim: ![{2FE2B5E2-40E6-4FA7-B971-93BE0A877760}](https://github.com/user-attachments/assets/5bce629f-0f0c-4032-af99-9e6c8f2fe958)
Now what? 🥲
siriak: @sadiul-hakim the linter tells you to replace tabs with spaces
![image](https://github.com/user-attachments/assets/11429e60-f10f-44ad-b093-9b35d36952c2)

sadiul-hakim: Is CheckStyle and Clang format automatic on your end? I had a hard time. I use intelij how can i make this automatic?
siriak: No, it's manual. I usually check in logs of failed PR check to see what failed. You can add them with a Git hook to run them before commit, but it should be done by everyone separately

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 009de204..f53a6220 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -162,6 +162,7 @@
               * [MatrixGraphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/MatrixGraphs.java)
               * [PrimMST](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/PrimMST.java)
               * [TarjansAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java)
+              * [UndirectedAdjacencyListGraph](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/UndirectedAdjacencyListGraph.java)
               * [WelshPowell](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/WelshPowell.java)
             * hashmap
               * hashing
@@ -319,6 +320,7 @@
             * [SubsetSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java)
             * [SubsetSumSpaceOptimized](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimized.java)
             * [SumOfSubset](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java)
+            * [TreeMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/TreeMatching.java)
             * [Tribonacci](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Tribonacci.java)
             * [UniquePaths](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/UniquePaths.java)
             * [UniqueSubsequencesCount](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCount.java)
@@ -423,6 +425,7 @@
             * [LongDivision](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LongDivision.java)
             * [LucasSeries](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LucasSeries.java)
             * [MagicSquare](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MagicSquare.java)
+            * [MathBuilder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MathBuilder.java)
             * [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MaxValue.java)
             * [Means](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Means.java)
             * [Median](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Median.java)
@@ -537,6 +540,7 @@
             * [PageRank](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PageRank.java)
             * [PasswordGen](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PasswordGen.java)
             * [PerlinNoise](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PerlinNoise.java)
+            * [PrintAMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java)
             * [QueueUsingTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java)
             * [RemoveDuplicateFromString](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java)
             * [ReverseStackUsingRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java)
@@ -724,7 +728,7 @@
             * zigZagPattern
               * [ZigZagPattern](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/zigZagPattern/ZigZagPattern.java)
           * tree
-            * [HeavyLightDecomposition](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/tree/HeavyLightDecomposition.java)
+            * [HeavyLightDecomposition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/tree/HeavyLightDecomposition.java)
   * test
     * java
       * com
@@ -1003,6 +1007,7 @@
             * [SubsetSumSpaceOptimizedTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumSpaceOptimizedTest.java)
             * [SubsetSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetSumTest.java)
             * [SumOfSubsetTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java)
+            * [TreeMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/TreeMatchingTest.java)
             * [TribonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/TribonacciTest.java)
             * [UniquePathsTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java)
             * [UniqueSubsequencesCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCountTest.java)
@@ -1097,6 +1102,7 @@
             * [LeonardoNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeonardoNumberTest.java)
             * [LongDivisionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LongDivisionTest.java)
             * [LucasSeriesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LucasSeriesTest.java)
+            * [MathBuilderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MathBuilderTest.java)
             * [MaxValueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MaxValueTest.java)
             * [MeansTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MeansTest.java)
             * [MedianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MedianTest.java)
@@ -1190,6 +1196,7 @@
             * [RemoveDuplicateFromStringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java)
             * [ReverseStackUsingRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java)
             * [SkylineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SkylineProblemTest.java)
+            * [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
             * [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
             * [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java)
           * puzzlesandgames
diff --git a/src/main/java/com/thealgorithms/maths/MathBuilder.java b/src/main/java/com/thealgorithms/maths/MathBuilder.java
new file mode 100644
index 00000000..3534749d
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/MathBuilder.java
@@ -0,0 +1,345 @@
+package com.thealgorithms.maths;
+
+import java.text.DecimalFormat;
+import java.util.Random;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+/**
+ * Author: Sadiul Hakim : https://github.com/sadiul-hakim
+ * Profession: Backend Engineer
+ * Date: Oct 20, 2024
+ */
+public final class MathBuilder {
+    private final double result;
+
+    private MathBuilder(Builder builder) {
+        this.result = builder.number;
+    }
+
+    // Returns final result
+    public double get() {
+        return result;
+    }
+
+    // Return result in long
+    public long toLong() {
+        try {
+            if (Double.isNaN(result)) {
+                throw new IllegalArgumentException("Cannot convert NaN to long");
+            }
+            if (result == Double.POSITIVE_INFINITY) {
+                return Long.MAX_VALUE;
+            }
+            if (result == Double.NEGATIVE_INFINITY) {
+                return Long.MIN_VALUE;
+            }
+            if (result > Long.MAX_VALUE) {
+                return Long.MAX_VALUE;
+            }
+            if (result < Long.MIN_VALUE) {
+                return Long.MIN_VALUE;
+            }
+            return Math.round(result);
+        } catch (Exception ex) {
+            return 0;
+        }
+    }
+
+    public static class Builder {
+        private double number;
+        private double memory = 0;
+
+        public Builder() {
+            number = 0;
+        }
+
+        public Builder(double num) {
+            number = num;
+        }
+
+        public Builder add(double num) {
+            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)) {
+                number += num;
+            }
+            return this;
+        }
+
+        public Builder minus(double num) {
+            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)) {
+                number -= num;
+            }
+            return this;
+        }
+
+        // Generates a random number and sets to NUMBER
+        public Builder rand(long seed) {
+            if (number != 0) {
+                throw new RuntimeException("Number must be zero for random assignment!");
+            }
+            Random random = new Random();
+            number = random.nextDouble(seed);
+            return this;
+        }
+
+        // Takes PI value and sets to NUMBER
+        public Builder pi() {
+            if (number != 0) {
+                throw new RuntimeException("Number must be zero for PI assignment!");
+            }
+            number = Math.PI;
+            return this;
+        }
+
+        // Takes E value and sets to NUMBER
+        public Builder e() {
+            if (number != 0) {
+                throw new RuntimeException("Number must be zero for E assignment!");
+            }
+            number = Math.E;
+            return this;
+        }
+
+        public Builder randomInRange(double min, double max) {
+
+            if (number != 0) {
+                throw new RuntimeException("Number must be zero for random assignment!");
+            }
+            Random random = new Random();
+            number = min + (max - min) * random.nextDouble();
+            return this;
+        }
+
+        public Builder toDegrees() {
+            number = Math.toDegrees(number);
+            return this;
+        }
+
+        public Builder max(double num) {
+            number = Math.max(number, num);
+            return this;
+        }
+
+        public Builder min(double num) {
+            number = Math.min(number, num);
+            return this;
+        }
+
+        public Builder multiply(double num) {
+            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)) {
+                number *= num;
+            }
+            return this;
+        }
+
+        public Builder divide(double num) {
+            if (num == 0) {
+                return this;
+            }
+            number /= num;
+            return this;
+        }
+
+        // Takes a number and a condition, only does the operation if condition is true.
+        public Builder divideIf(double num, BiFunction<Double, Double, Boolean> condition) {
+            if (num == 0) {
+                return this;
+            }
+            if (condition.apply(number, num)) {
+                number /= num;
+            }
+            return this;
+        }
+
+        public Builder mod(double num) {
+            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)) {
+                number %= num;
+            }
+            return this;
+        }
+
+        public Builder pow(double num) {
+            number = Math.pow(number, num);
+            return this;
+        }
+
+        public Builder sqrt() {
+            number = Math.sqrt(number);
+            return this;
+        }
+
+        public Builder round() {
+            number = Math.round(number);
+            return this;
+        }
+
+        public Builder floor() {
+            number = Math.floor(number);
+            return this;
+        }
+
+        public Builder ceil() {
+            number = Math.ceil(number);
+            return this;
+        }
+
+        public Builder abs() {
+            number = Math.abs(number);
+            return this;
+        }
+
+        public Builder cbrt() {
+            number = Math.cbrt(number);
+            return this;
+        }
+
+        public Builder log() {
+            number = Math.log(number);
+            return this;
+        }
+
+        public Builder log10() {
+            number = Math.log10(number);
+            return this;
+        }
+
+        public Builder sin() {
+            number = Math.sin(number);
+            return this;
+        }
+
+        public Builder cos() {
+            number = Math.cos(number);
+            return this;
+        }
+
+        public Builder tan() {
+            number = Math.tan(number);
+            return this;
+        }
+
+        public Builder sinh() {
+            number = Math.sinh(number);
+            return this;
+        }
+
+        public Builder cosh() {
+            number = Math.cosh(number);
+            return this;
+        }
+
+        public Builder tanh() {
+            number = Math.tanh(number);
+            return this;
+        }
+
+        public Builder exp() {
+            number = Math.exp(number);
+            return this;
+        }
+
+        public Builder toRadians() {
+            number = Math.toRadians(number);
+            return this;
+        }
+
+        // Remembers the NUMBER
+        public Builder remember() {
+            memory = number;
+            return this;
+        }
+
+        // Recalls the NUMBER
+        public Builder recall(boolean cleanMemory) {
+            number = memory;
+            if (cleanMemory) {
+                memory = 0;
+            }
+
+            return this;
+        }
+
+        // Recalls the NUMBER on condition
+        public Builder recallIf(Function<Double, Boolean> condition, boolean cleanMemory) {
+            if (!condition.apply(number)) {
+                return this;
+            }
+            number = memory;
+            if (cleanMemory) {
+                memory = 0;
+            }
+
+            return this;
+        }
+
+        // Replaces NUMBER with given number
+        public Builder set(double num) {
+            if (number != 0) {
+                throw new RuntimeException("Number must be zero to set!");
+            }
+            number = num;
+            return this;
+        }
+
+        // Replaces NUMBER with given number on condition
+        public Builder setIf(double num, BiFunction<Double, Double, Boolean> condition) {
+            if (number != 0) {
+                throw new RuntimeException("Number must be zero to set!");
+            }
+            if (condition.apply(number, num)) {
+                number = num;
+            }
+            return this;
+        }
+
+        // Prints current NUMBER
+        public Builder print() {
+            System.out.println("MathBuilder Result :: " + number);
+            return this;
+        }
+
+        public Builder format(String format) {
+            DecimalFormat formater = new DecimalFormat(format);
+            String num = formater.format(number);
+            number = Double.parseDouble(num);
+            return this;
+        }
+
+        public Builder format(int decimalPlace) {
+            String pattern = "."
+                + "#".repeat(decimalPlace);
+            DecimalFormat formater = new DecimalFormat(pattern);
+            String num = formater.format(number);
+            number = Double.parseDouble(num);
+            return this;
+        }
+
+        public MathBuilder build() {
+            return new MathBuilder(this);
+        }
+    }
+}
diff --git a/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java
index 2e735222..2757da1f 100644
--- a/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java
+++ b/src/main/java/com/thealgorithms/matrix/PrintAMatrixInSpiralOrder.java
@@ -12,7 +12,6 @@ public class PrintAMatrixInSpiralOrder {
      * @param col    number of columns matrix has
      * @author Sadiul Hakim : https://github.com/sadiul-hakim
      */
-
     public List<Integer> print(int[][] matrix, int row, int col) {
 
         // r traverses matrix row wise from first
@@ -20,35 +19,27 @@ public class PrintAMatrixInSpiralOrder {
         // c traverses matrix column wise from first
         int c = 0;
         int i;
-
         List<Integer> result = new ArrayList<>();
-
         while (r < row && c < col) {
             // print first row of matrix
             for (i = c; i < col; i++) {
                 result.add(matrix[r][i]);
             }
-
             // increase r by one because first row printed
             r++;
-
             // print last column
             for (i = r; i < row; i++) {
                 result.add(matrix[i][col - 1]);
             }
-
             // decrease col by one because last column has been printed
             col--;
-
             // print rows from last except printed elements
             if (r < row) {
                 for (i = col - 1; i >= c; i--) {
                     result.add(matrix[row - 1][i]);
                 }
-
                 row--;
             }
-
             // print columns from first except printed elements
             if (c < col) {
                 for (i = row - 1; i >= r; i--) {
diff --git a/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java
new file mode 100644
index 00000000..abfdd006
--- /dev/null
+++ b/src/main/java/com/thealgorithms/others/PrintAMatrixInSpiralOrder.java
@@ -0,0 +1,52 @@
+package com.thealgorithms.others;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PrintAMatrixInSpiralOrder {
+    /**
+     * Search a key in row and column wise sorted matrix
+     *
+     * @param matrix matrix to be searched
+     * @param row    number of rows matrix has
+     * @param col    number of columns matrix has
+     * @author Sadiul Hakim : https://github.com/sadiul-hakim
+     */
+    public List<Integer> print(int[][] matrix, int row, int col) {
+        // r traverses matrix row wise from first
+        int r = 0;
+        // c traverses matrix column wise from first
+        int c = 0;
+        int i;
+        List<Integer> result = new ArrayList<>();
+        while (r < row && c < col) {
+            // print first row of matrix
+            for (i = c; i < col; i++) {
+                result.add(matrix[r][i]);
+            }
+            // increase r by one because first row printed
+            r++;
+            // print last column
+            for (i = r; i < row; i++) {
+                result.add(matrix[i][col - 1]);
+            }
+            // decrease col by one because last column has been printed
+            col--;
+            // print rows from last except printed elements
+            if (r < row) {
+                for (i = col - 1; i >= c; i--) {
+                    result.add(matrix[row - 1][i]);
+                }
+                row--;
+            }
+            // print columns from first except printed elements
+            if (c < col) {
+                for (i = row - 1; i >= r; i--) {
+                    result.add(matrix[i][c]);
+                }
+                c++;
+            }
+        }
+        return result;
+    }
+}
diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java
index 91fda373..b53c7e52 100644
--- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java
+++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java
@@ -15,7 +15,6 @@ public class SearchInARowAndColWiseSortedMatrix {
         // This variable iterates over columns
         int j = n - 1;
         int[] result = {-1, -1};
-
         while (i < n && j >= 0) {
             if (matrix[i][j] == value) {
                 result[0] = i;


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/MathBuilderTest.java b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
new file mode 100644
index 00000000..b6ecc674
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/MathBuilderTest.java
@@ -0,0 +1,38 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class MathBuilderTest {
+
+    @Test
+    void simpleMath() {
+        double result = new MathBuilder.Builder(100).add(200).multiply(10).print().divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get();
+        assertEquals(13, result);
+    }
+
+    @Test
+    void memoryTest() {
+        long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true).mod(2).build().toLong();
+        assertEquals(0, result);
+    }
+
+    @Test
+    void freeFallDistance() {
+        long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong();
+        assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123
+    }
+
+    @Test
+    void batchSalaryProcessing() {
+        double[] salaries = {2000, 3000, 4000, 5000};
+        long[] processedSalaries = new long[salaries.length];
+        for (int i = 0; i < salaries.length; i++) {
+            processedSalaries[i] = new MathBuilder.Builder(salaries[i]).addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round().build().toLong();
+        }
+        long[] expectedSalaries = {1840, 3036, 4048, 5060};
+        assertArrayEquals(expectedSalaries, processedSalaries);
+    }
+}
diff --git a/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java
new file mode 100644
index 00000000..d35d4bb6
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java
@@ -0,0 +1,26 @@
+package com.thealgorithms.others;
+
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class TestPrintMatrixInSpiralOrder {
+    @Test
+    public void testOne() {
+        int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
+        var printClass = new PrintAMatrixInSpiralOrder();
+        List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
+        List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, 24, 15, 16);
+        assertIterableEquals(res, list);
+    }
+
+    @Test
+    public void testTwo() {
+        int[][] matrix = {{2, 2}};
+        var printClass = new PrintAMatrixInSpiralOrder();
+        List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
+        List<Integer> list = List.of(2, 2);
+        assertIterableEquals(res, list);
+    }
+}
diff --git a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java
index 014fb4bd..a56f7967 100644
--- a/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java
+++ b/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java
@@ -1,27 +1,25 @@
-package com.thealgorithms.searches;
-
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-
-import org.junit.jupiter.api.Test;
-
-public class TestSearchInARowAndColWiseSortedMatrix {
-    @Test
-    public void searchItem() {
-        int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
-
-        var test = new SearchInARowAndColWiseSortedMatrix();
-        int[] res = test.search(matrix, 16);
-        int[] expectedResult = {2, 2};
-        assertArrayEquals(expectedResult, res);
-    }
-
-    @Test
-    public void notFound() {
-        int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
-
-        var test = new SearchInARowAndColWiseSortedMatrix();
-        int[] res = test.search(matrix, 96);
-        int[] expectedResult = {-1, -1};
-        assertArrayEquals(expectedResult, res);
-    }
-}
+package com.thealgorithms.searches;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class TestSearchInARowAndColWiseSortedMatrix {
+    @Test
+    public void searchItem() {
+        int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
+        var test = new SearchInARowAndColWiseSortedMatrix();
+        int[] res = test.search(matrix, 16);
+        int[] expectedResult = {2, 2};
+        assertArrayEquals(expectedResult, res);
+    }
+
+    @Test
+    public void notFound() {
+        int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
+        var test = new SearchInARowAndColWiseSortedMatrix();
+        int[] res = test.search(matrix, 96);
+        int[] expectedResult = {-1, -1};
+        assertArrayEquals(expectedResult, res);
+    }
+}