Design methods using the following headings: int [][] add[][] x, int[][] y), int
ID: 3930827 • Letter: D
Question
Design methods using the following headings: int [][] add[][] x, int[][] y), int [][] multiply[][] x, int[][] y) that add and multiply two matrices and return references to the sum and product matrices. If the dimensions of x and y are not compatible with addition (multiplication), then the method should issue an appropriate error message and exit. Test your methods in a program. Include two additional methods that print the contents of a two-dimensional array, and read data, row by row, into a two-dimensional array.
Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.util.Scanner;
public class MatrixAddMul { // clas to run the code
public static int[][] add(int[][] x, int[][] y) { // desired method to return the array that is added as the result
int aRows = x.length; // getting the data from the arrays
int aColumns = x[0].length;
int bRows = y.length;
int bColumns = y[0].length;
if (aColumns != bRows) {
throw new IllegalArgumentException("x:Rows: " + aColumns + " did not match y:Columns " + bRows + ".");
}
int[][] res = new int[aRows][bColumns]; //initialising the array for the result
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
res[i][j] = 0;
}
}
for (int i = 0; i < aRows; i++) { // aRow
for (int j = 0; j < bColumns; j++) { // bColumn
res[i][j] = x[i][j] + y[i][j];
}
}
return res; // return the result
}
public static int[][] multiply(int[][] x, int[][] y) { // desired method to return the multiplied matrix
int aRows = x.length; // required data for the array
int aColumns = x[0].length;
int bRows = y.length;
int bColumns = y[0].length;
if (aColumns != bRows) { // checking for the multiplication condition
throw new IllegalArgumentException("x : Rows : " + aColumns + " did not match with the y : Columns : " + bRows + ".");
}
int[][] C = new int[aRows][bColumns]; // resultant array initialisation
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
C[i][j] = 0;
}
}
for (int i = 0; i < aRows; i++) { // aRow
for (int j = 0; j < bColumns; j++) { // bColumn
for (int k = 0; k < aColumns; k++) { // aColumn
C[i][j] += x[i][k] * y[k][j]; // saving the data to the array
}
}
}
return C; // return the result array
}
public static void printArray(int[][] result) { // method to print the array
int aRows = result.length; // required initialisations
int bRows = result.length;
for (int i = 0; i < aRows; i++) { // aRow
for (int j = 0; j < bRows; j++) { // bColumn
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
public static int[][] insertArray() { // method to insert data to the array
Scanner in = new Scanner(System.in); // scanner class that reads the data from the user
int m, n, c, d;
System.out.println("Please Enter the Details of the Matrix."); // prompt for the user about the code
System.out.println("Enter the Number of Rows : ");
m = in.nextInt(); // get the row value
System.out.println("Enter the Number of Columns : ");
n = in.nextInt(); // get the column value
int array[][] = new int[m][n]; // creating a new matrix
System.out.println("Enter the Elements of the Matrix : ");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
array[c][d] = in.nextInt(); // saving the data to the matrix
}
}
return array; // returnt the result
}
public static void main(String[] args) { // driver method
int firstArray[][] = insertArray(); // inserting the data to the array through method call
int secondArray[][] = insertArray(); // inserting the data to the array through method call
int[][] addResult = add(firstArray, secondArray); // calling the method to return the added array as result
System.out.println(" The Added Matrix is : "); // print the data to console
printArray(addResult); // method that prints the data
int[][] mulResult = multiply(firstArray, secondArray); // calling the method to return the multiplied array as result
System.out.println(" The Multiplied Matrix is : ");// print the data to console
printArray(mulResult);// method that prints the data
}
}
OUTPUT :
Please Enter the Details of the Matrix.
Enter the Number of Rows :
2
Enter the Number of Columns :
2
Enter the Elements of the Matrix :
1
2
3
4
Please Enter the Details of the Matrix.
Enter the Number of Rows :
2
Enter the Number of Columns :
2
Enter the Elements of the Matrix :
5
6
7
8
The Added Matrix is :
6 8
10 12
The Multiplied Matrix is :
19 22
43 50
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.