mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Add tests, remove main
in EulerMethod
(#5771)
This commit is contained in:
@ -1014,6 +1014,7 @@
|
|||||||
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [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)
|
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.thealgorithms.misc;
|
package com.thealgorithms.misc;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
@ -22,62 +20,27 @@ public final class MatrixTranspose {
|
|||||||
private MatrixTranspose() {
|
private MatrixTranspose() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
/*
|
* Calculate the transpose of the given matrix.
|
||||||
* This is the main method
|
*
|
||||||
*
|
* @param matrix The matrix to be transposed
|
||||||
* @param args Unused.
|
* @throws IllegalArgumentException if the matrix is empty
|
||||||
*
|
* @throws NullPointerException if the matrix is null
|
||||||
* @return Nothing.
|
* @return The transposed matrix
|
||||||
*/
|
*/
|
||||||
Scanner sc = new Scanner(System.in);
|
public static int[][] transpose(int[][] matrix) {
|
||||||
int i;
|
if (matrix == null || matrix.length == 0) {
|
||||||
int j;
|
throw new IllegalArgumentException("Matrix is empty");
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
int rows = matrix.length;
|
||||||
* Print matrix before the Transpose in proper way
|
int cols = matrix[0].length;
|
||||||
*/
|
int[][] transposedMatrix = new int[cols][rows];
|
||||||
System.out.println("The matrix is:");
|
for (int i = 0; i < cols; i++) {
|
||||||
for (i = 0; i < row; i++) {
|
for (int j = 0; j < rows; j++) {
|
||||||
for (j = 0; j < column; j++) {
|
transposedMatrix[i][j] = matrix[j][i];
|
||||||
System.out.print(arr[i][j] + "\t");
|
|
||||||
}
|
}
|
||||||
System.out.print("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 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");
|
|
||||||
}
|
|
||||||
System.out.print("\n");
|
|
||||||
}
|
}
|
||||||
|
return transposedMatrix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user