Skip to content

Commit 7abf5f7

Browse files
committed
Add tests, remove main in EulerMethod
1 parent 596c614 commit 7abf5f7

File tree

2 files changed

+77
-55
lines changed

2 files changed

+77
-55
lines changed
Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
4-
53
/**
64
*
75
*
@@ -22,62 +20,27 @@ public final class MatrixTranspose {
2220
private MatrixTranspose() {
2321
}
2422

25-
public static void main(String[] args) {
26-
/*
27-
* This is the main method
28-
*
29-
* @param args Unused.
30-
*
31-
* @return Nothing.
32-
*/
33-
Scanner sc = new Scanner(System.in);
34-
int i;
35-
int j;
36-
int row;
37-
int column;
38-
System.out.println("Enter the number of rows in the 2D matrix:");
39-
40-
/*
41-
* Take input from user for how many rows to be print
42-
*/
43-
row = sc.nextInt();
44-
45-
System.out.println("Enter the number of columns in the 2D matrix:");
46-
47-
/*
48-
* Take input from user for how many coloumn to be print
49-
*/
50-
column = sc.nextInt();
51-
int[][] arr = new int[row][column];
52-
System.out.println("Enter the elements");
53-
for (i = 0; i < row; i++) {
54-
for (j = 0; j < column; j++) {
55-
arr[i][j] = sc.nextInt();
56-
}
57-
}
58-
59-
/*
60-
* Print matrix before the Transpose in proper way
61-
*/
62-
System.out.println("The matrix is:");
63-
for (i = 0; i < row; i++) {
64-
for (j = 0; j < column; j++) {
65-
System.out.print(arr[i][j] + "\t");
66-
}
67-
System.out.print("\n");
23+
/**
24+
* Calculate the transpose of the given matrix.
25+
*
26+
* @param matrix The matrix to be transposed
27+
* @throws IllegalArgumentException if the matrix is empty
28+
* @throws NullPointerException if the matrix is null
29+
* @return The transposed matrix
30+
*/
31+
public static int[][] transpose(int[][] matrix) {
32+
if (matrix.length == 0 || matrix == null) {
33+
throw new IllegalArgumentException("Matrix is empty");
6834
}
6935

70-
/*
71-
* Print matrix after the tranpose in proper way Transpose means Interchanging
72-
* of rows wth column so we interchange the rows in next loop Thus at last
73-
* matrix of transpose is obtained through user input...
74-
*/
75-
System.out.println("The Transpose of the given matrix is:");
76-
for (i = 0; i < column; i++) {
77-
for (j = 0; j < row; j++) {
78-
System.out.print(arr[j][i] + "\t");
36+
int rows = matrix.length;
37+
int cols = matrix[0].length;
38+
int[][] transposedMatrix = new int[cols][rows];
39+
for (int i = 0; i < cols; i++) {
40+
for (int j = 0; j < rows; j++) {
41+
transposedMatrix[i][j] = matrix[j][i];
7942
}
80-
System.out.print("\n");
8143
}
44+
return transposedMatrix;
8245
}
8346
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class MatrixTransposeTest {
9+
10+
@Test
11+
public void testTransposeSquareMatrix() {
12+
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
13+
14+
int[][] expected = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
15+
16+
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the square matrix is incorrect.");
17+
}
18+
19+
@Test
20+
public void testTransposeRectangularMatrix() {
21+
int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};
22+
23+
int[][] expected = {{1, 3, 5}, {2, 4, 6}};
24+
25+
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the rectangular matrix is incorrect.");
26+
}
27+
28+
@Test
29+
public void testTransposeSingleRowMatrix() {
30+
int[][] matrix = {{1, 2, 3}};
31+
32+
int[][] expected = {{1}, {2}, {3}};
33+
34+
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-row matrix is incorrect.");
35+
}
36+
37+
@Test
38+
public void testTransposeSingleColumnMatrix() {
39+
int[][] matrix = {{1}, {2}, {3}};
40+
41+
int[][] expected = {{1, 2, 3}};
42+
43+
assertArrayEquals(expected, MatrixTranspose.transpose(matrix), "Transpose of the single-column matrix is incorrect.");
44+
}
45+
46+
@Test
47+
public void testTransposeEmptyMatrix() {
48+
int[][] matrix = new int[0][0];
49+
50+
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for empty matrix.");
51+
}
52+
53+
@Test
54+
public void testTransposeNullMatrix() {
55+
int[][] matrix = null;
56+
57+
assertThrows(NullPointerException.class, () -> MatrixTranspose.transpose(matrix), "Expected IllegalArgumentException for null matrix.");
58+
}
59+
}

0 commit comments

Comments
 (0)