Created
October 26, 2016 09:56
-
-
Save AlexanderShushunov/32c5e70f036b2be5ef605a73d913576e to your computer and use it in GitHub Desktop.
Result of the lesson demo (26.10.2016)
Points: meaningful names, preconditions, small function, function preconditions, using domain vocabulary, code formatting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class MatrixUtils { | |
public static int[][] multiplyMatrix( | |
int[][] left, | |
int[][] right) { | |
if (!checkMatrix(left) || !checkMatrix(right)) { | |
throw new IllegalArgumentException(); | |
} | |
if (getColumnCount(left) != getRowCount(right)) { | |
throw new IllegalArgumentException(); | |
} | |
int[][] result = new int[getRowCount(left)][getColumnCount(right)]; | |
for (int rowNumber = 0; rowNumber < getRowCount(left); rowNumber++) { | |
for (int columnNumber = 0; columnNumber < getColumnCount(right); columnNumber++) { | |
result[rowNumber][columnNumber] = | |
multiplyVector(getRow(left, rowNumber), | |
getColumn(right, columnNumber)); | |
} | |
} | |
return result; | |
} | |
private static int multiplyVector(int[] left, int[] right) { | |
return 42; | |
} | |
private static int[] getColumn(int[][] matrix, int columnNumber) { | |
int[] result = new int[getRowCount(matrix)]; | |
for (int rowNumber = 0; rowNumber < getRowCount(matrix); rowNumber++) { | |
result[rowNumber] = matrix[rowNumber][columnNumber]; | |
} | |
return result; | |
} | |
private static int[] getRow(int[][] matrix, int rowNumber) { | |
return matrix[rowNumber]; | |
} | |
private static int getRowCount(int[][] matrix) { | |
return matrix.length; | |
} | |
private static int getColumnCount(int[][] matrix) { | |
return matrix[0].length; | |
} | |
private static boolean checkMatrix(int[][] matrix) { | |
return true; | |
} | |
public static boolean testMultiplyMatrix() { | |
return Arrays.deepEquals(multiplyMatrix( | |
new int[][]{{7, 4, 88}, | |
{3, 554, 58}}, | |
new int[][]{{17, 4}, | |
{3, 54}, | |
{3, 54}}), | |
new int[][]{{7, 4, 88}, | |
{3, 554, 58}}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment