Repository: TheAlgorithms/Java
Pull request #4461 (2023-09-30 21:15:54)

Added MirrorOfMatrix.java
Problem Statement :-
We have given an array of m x n (where m is the number of rows and n is the number of columns).
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
Solution;_
it basically traverse column wise and swap the starting and ending element of each row to get the mirror of 2D array

In this pull request, I am submitting the code of this problem that prioritize readability and user-friendliness.

<!-- 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.


Aman28801: I would be grateful if my contribution is accepted and merged using "hacktoberfest-accepted" label.
Aman28801: is test file okay?
Aman28801: I would be grateful if my contribution is accepted and merged using "hacktoberfest-accepted" label.
vil02: > I would be grateful if my contribution is accepted and merged using "hacktoberfest-accepted" label.

It is not needed: the hacktoberfest page will recognize this contribution, because this repo has hactoberest as a one of the topics.

@Aman28801 please have a look at this commits: ec8e9f820129f8ddf2af6022c8fda7bdb77f2931
Aman28801: Thank you for the help @vil02 throughout the pull request as this is my first contribution towards open source.

Patch:
diff --git a/src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java b/src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java
new file mode 100644
index 00000000..89dfce3f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/misc/MirrorOfMatrix.java
@@ -0,0 +1,57 @@
+package com.thealgorithms.misc;
+
+// Problem Statement
+/*
+We have given an array of m x n (where m is the number of rows and n is the number of columns).
+Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
+
+The Original matrix is:   |   The Mirror matrix is:
+1	2	3	              |   3	 2	 1
+4	5	6	              |   6	 5	 4
+7	8	9	              |   9	 8	 7
+
+@author - Aman (https://github.com/Aman28801)
+*/
+
+public final class MirrorOfMatrix {
+    private MirrorOfMatrix() {
+    }
+
+    public static int[][] mirrorMatrix(final int[][] originalMatrix) {
+        if (originalMatrix == null) {
+            // Handle invalid input
+            return null;
+        }
+        if (originalMatrix.length == 0) {
+            return new int[0][0];
+        }
+
+        checkInput(originalMatrix);
+
+        int numRows = originalMatrix.length;
+        int numCols = originalMatrix[0].length;
+
+        int[][] mirroredMatrix = new int[numRows][numCols];
+
+        for (int i = 0; i < numRows; i++) {
+            mirroredMatrix[i] = reverseRow(originalMatrix[i]);
+        }
+        return mirroredMatrix;
+    }
+    private static int[] reverseRow(final int[] inRow) {
+        int[] res = new int[inRow.length];
+        for (int i = 0; i < inRow.length; ++i) {
+            res[i] = inRow[inRow.length - 1 - i];
+        }
+        return res;
+    }
+
+    private static void checkInput(final int[][] matrix) {
+        // Check if all rows have the same number of columns
+        for (int i = 1; i < matrix.length; i++) {
+            if (matrix[i].length != matrix[0].length) {
+                throw new IllegalArgumentException("The input is not a matrix.");
+            }
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java b/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java
new file mode 100644
index 00000000..0da0cf0f
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java
@@ -0,0 +1,53 @@
+package com.thealgorithms.misc;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+class MirrorOfMatrixTest {
+
+    @Test
+    void testMirrorMatrixRegularMatrix() {
+        int[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
+        int[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
+        int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
+        assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
+    }
+
+    @Test
+    void testMirrorMatrixEmptyMatrix() {
+        int[][] originalMatrix = {};
+        int[][] expectedMirrorMatrix = {};
+        int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
+        assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
+    }
+
+    @Test
+    void testMirrorMatrixSingleElementMatrix() {
+        int[][] originalMatrix = {{42}};
+        int[][] expectedMirrorMatrix = {{42}};
+        int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
+        assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
+    }
+
+    @Test
+    void testMirrorMatrixMultipleRowsOneColumnMatrix() {
+        int[][] originalMatrix = {{1}, {2}, {3}, {4}};
+        int[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
+        int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
+        assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
+    }
+
+    @Test
+    void testMirrorMatrixNullInput() {
+        int[][] originalMatrix = null;
+        assertNull(MirrorOfMatrix.mirrorMatrix(originalMatrix));
+    }
+
+    @Test
+    void testMirrotMarixThrows() {
+        assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new int[][] {{1}, {2, 3}}));
+    }
+}