Repository: TheAlgorithms/Java
Pull request #5771 (2024-10-13 10:33:12)

Add tests, remove `main` in `EulerMethod`
<!--
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/5771?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 `88.88889%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 64.23%. Comparing base [(`596c614`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/596c6147af0af91d1ca8990ed91820df1fe3b8ea?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`fae2ce7`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/fae2ce7f36486697f3252753d6f4fbd5d40940fb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 4 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5771?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...n/java/com/thealgorithms/misc/MatrixTranspose.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5771?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmisc%2FMatrixTranspose.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9taXNjL01hdHJpeFRyYW5zcG9zZS5qYXZh) | 88.88% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5771?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    #5771      +/-   ##
============================================
+ Coverage     64.05%   64.23%   +0.18%     
- Complexity     4177     4209      +32     
============================================
  Files           584      587       +3     
  Lines         16332    16378      +46     
  Branches       3152     3159       +7     
============================================
+ Hits          10461    10520      +59     
+ Misses         5445     5428      -17     
- Partials        426      430       +4     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5771?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).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index c23cb4a3..b756b8d1 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -1014,6 +1014,7 @@
             * [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
             * [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java)
             * [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java)
+            * [MatrixTransposeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java)
             * [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
             * [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
             * [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
diff --git a/src/main/java/com/thealgorithms/misc/MatrixTranspose.java b/src/main/java/com/thealgorithms/misc/MatrixTranspose.java
index 153cf4e9..74368278 100644
--- a/src/main/java/com/thealgorithms/misc/MatrixTranspose.java
+++ b/src/main/java/com/thealgorithms/misc/MatrixTranspose.java
@@ -1,7 +1,5 @@
 package com.thealgorithms.misc;
 
-import java.util.Scanner;
-
 /**
  *
  *
@@ -22,62 +20,27 @@ public final class MatrixTranspose {
     private MatrixTranspose() {
     }
 
-    public static void main(String[] args) {
-        /*
-         * This is the main method
-         *
-         * @param args Unused.
-         *
-         * @return Nothing.
-         */
-        Scanner sc = new Scanner(System.in);
-        int i;
-        int j;
-        int row;
-        int column;
-        System.out.println("Enter the number of rows in the 2D matrix:");
-
-        /*
-         * Take input from user for how many rows to be print
-         */
-        row = sc.nextInt();
-
-        System.out.println("Enter the number of columns in the 2D matrix:");
-
-        /*
-         * Take input from user for how many coloumn to be print
-         */
-        column = sc.nextInt();
-        int[][] arr = new int[row][column];
-        System.out.println("Enter the elements");
-        for (i = 0; i < row; i++) {
-            for (j = 0; j < column; j++) {
-                arr[i][j] = sc.nextInt();
-            }
-        }
-
-        /*
-         * Print matrix before the Transpose in proper way
-         */
-        System.out.println("The matrix is:");
-        for (i = 0; i < row; i++) {
-            for (j = 0; j < column; j++) {
-                System.out.print(arr[i][j] + "\t");
-            }
-            System.out.print("\n");
+    /**
+     * Calculate the transpose of the given matrix.
+     *
+     * @param matrix The matrix to be transposed
+     * @throws IllegalArgumentException if the matrix is empty
+     * @throws NullPointerException     if the matrix is null
+     * @return The transposed matrix
+     */
+    public static int[][] transpose(int[][] matrix) {
+        if (matrix == null || matrix.length == 0) {
+            throw new IllegalArgumentException("Matrix is empty");
         }
 
-        /*
-         * Print matrix after the tranpose in proper way Transpose means Interchanging
-         * of rows wth column so we interchange the rows in next loop Thus at last
-         * matrix of transpose is obtained through user input...
-         */
-        System.out.println("The Transpose of the given matrix is:");
-        for (i = 0; i < column; i++) {
-            for (j = 0; j < row; j++) {
-                System.out.print(arr[j][i] + "\t");
+        int rows = matrix.length;
+        int cols = matrix[0].length;
+        int[][] transposedMatrix = new int[cols][rows];
+        for (int i = 0; i < cols; i++) {
+            for (int j = 0; j < rows; j++) {
+                transposedMatrix[i][j] = matrix[j][i];
             }
-            System.out.print("\n");
         }
+        return transposedMatrix;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java
new file mode 100644
index 00000000..cf668807
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/MatrixTransposeTest.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.misc;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class MatrixTransposeTest {
+
+    private static Stream<Arguments> provideValidMatrixTestCases() {
+        return Stream.of(Arguments.of(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, new int[][] {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}, "Transpose of square matrix"), Arguments.of(new int[][] {{1, 2}, {3, 4}, {5, 6}}, new int[][] {{1, 3, 5}, {2, 4, 6}}, "Transpose of rectangular matrix"),
+            Arguments.of(new int[][] {{1, 2, 3}}, new int[][] {{1}, {2}, {3}}, "Transpose of single-row matrix"), Arguments.of(new int[][] {{1}, {2}, {3}}, new int[][] {{1, 2, 3}}, "Transpose of single-column matrix"));
+    }
+
+    private static Stream<Arguments> provideInvalidMatrixTestCases() {
+        return Stream.of(Arguments.of(new int[0][0], "Empty matrix should throw IllegalArgumentException"), Arguments.of(null, "Null matrix should throw IllegalArgumentException"));
+    }
+
+    @ParameterizedTest(name = "Test case {index}: {2}")
+    @MethodSource("provideValidMatrixTestCases")
+    void testValidMatrixTranspose(int[][] input, int[][] expected, String description) {
+        assertArrayEquals(expected, MatrixTranspose.transpose(input), description);
+    }
+
+    @ParameterizedTest(name = "Test case {index}: {1}")
+    @MethodSource("provideInvalidMatrixTestCases")
+    void testInvalidMatrixTranspose(int[][] input, String description) {
+        assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description);
+    }
+}