public class Matrix_Multiplication { Scanner scan; int matrix1[][], matrix2[][], multi[][]; int row, column; void create() { scan = new Scanner(System.in); System.out.println("Matrix Multiplication"); // First Matrix Creation.. System.out.println("\nEnter number of rows & columns"); row = Integer.parseInt(scan.nextLine()); column = Integer.parseInt(scan.nextLine()); matrix1 = new int[row][column]; matrix2 = new int[row][column]; multi = new int[row][column]; System.out.println("Enter the data for first matrix :"); for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { matrix1[i][j] = scan.nextInt(); } } // Second Matrix Creation.. System.out.println("Enter the data for second matrix :"); for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { matrix2[i][j] = scan.nextInt(); } } } void display() { System.out.println("\nThe First Matrix is :"); for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { System.out.print("\t" + matrix1[i][j]); } System.out.println(); } System.out.println("\n\nThe Second Matrix is :"); for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { System.out.print("\t" + matrix2[i][j]); } System.out.println(); } } void multi() { for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { multi[i][j] = matrix1[i][j] * matrix2[i][j]; } } System.out.println("\n\nThe Multiplication is :"); for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { System.out.print("\t" + multi[i][j]); } System.out.println(); } }} Closed due to the fact that off-topic participants YuriySPb ♦ , Vadim Ovchinnikov , pavel , temq , Denis Jan 16 '17 at 12:51 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Yuriy SPb, Vadim Ovchinnikov, Denis
|
1 answer
Good day,
Such code is really very difficult to test :) There is a proposal to make the methods more general and select the parameters on which they depend in the arguments, and the results of the algorithm in the returned values. Also try to avoid code duplication.
For example:
public class Matrix_Multiplication { Scanner scan; public Matrix_Multiplication() { scan = new Scanner(System.in); } public int askForInt(String message){ System.out.println(message); return Integer.parseInt(scan.nextLine()); } public int[][] createMatrix(int rows, int columns) { int matrix[][] = new int[rows][columns]; System.out.println("Enter the data for matrix :"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { matrix[i][j] = scan.nextInt(); } } return matrix; } public void display(int[][] matrix) { System.out.println("\nMatrix is :"); for (int i = 0; i < matrix[0].length; i++) { for (int j = 0; j < matrix.length; j++) { System.out.print("\t" + matrix[i][j]); } System.out.println(); } } public int[][] multi(int[][] matrix1, int[][] matrix2) { int row = matrix1.length; int column = matrix1[0].length; int result[][] = new int[row][column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { result[i][j] = matrix1[i][j] * matrix2[i][j]; } } return result; } public void closeResourses(){ scan.close(); } } Then main will be something like this:
public static void main(String[] args) { Matrix_Multiplication matrixMultiplication = new Matrix_Multiplication(); int rows = matrixMultiplication.askForInt("\nEnter number of rows"); int columns = matrixMultiplication.askForInt("\nEnter number of columns"); int[][] matrix1 = matrixMultiplication.createMatrix(rows, columns); int[][] matrix2 = matrixMultiplication.createMatrix(rows, columns); int[][] result = matrixMultiplication.multi(matrix1, matrix2); matrixMultiplication.display(result); matrixMultiplication.closeResourses(); } and it will be much easier to write tests, just call the method with parameters and compare with the expected result:
@Test public void testMultiiple() { int[][] m1 = new int[][]{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; int[][] m2 = new int[][]{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; int[][] expectedResult = new int[][]{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; assertArrayEquals(expectedResult,matrix_multiplication.multi(m1,m2)); } |