diff --git a/src/main/java/ru/j4j/array/Matrix.java b/src/main/java/ru/j4j/array/Matrix.java new file mode 100644 index 0000000..3c4ad62 --- /dev/null +++ b/src/main/java/ru/j4j/array/Matrix.java @@ -0,0 +1,13 @@ +package ru.j4j.array; + +public class Matrix { + public static int[][] multiple(int size) { + int[][] table = new int[size][size]; + for (int row = 0; row < table.length; row++) { + for (int cell = 0; cell < table[row].length; cell++) { + table[row][cell] = (row + 1) * (cell + 1); + } + } + return table; + } +} diff --git a/src/test/java/ru/j4j/array/MatrixTest.java b/src/test/java/ru/j4j/array/MatrixTest.java new file mode 100644 index 0000000..7bc977f --- /dev/null +++ b/src/test/java/ru/j4j/array/MatrixTest.java @@ -0,0 +1,32 @@ +package ru.j4j.array; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class MatrixTest { + @Test + public void when2on2() { + int size = 2; + int[][] result = Matrix.multiple(size); + int[][] expected = { + {1, 2}, + {2, 4} + }; + assertThat(result).isDeepEqualTo(expected); + } + + @Test + public void when5on5() { + int size = 5; + int[][] result = Matrix.multiple(size); + int[][] expected = { + {1, 2, 3, 4, 5}, + {2, 4, 6, 8, 10}, + {3, 6, 9, 12, 15}, + {4, 8, 12, 16, 20}, + {5, 10, 15, 20, 25} + }; + assertThat(result).isDeepEqualTo(expected); + } +} \ No newline at end of file