Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hw3: Write a method to read the input from your console. The header of the metho

ID: 3746397 • Letter: H

Question

Hw3: Write a method to read the input from your console. The header of the method is as follows: Hint: declare "Scanner input - new Scannert(System.in)" inside the inputMatrix) public static int[Il] inputMatrixlint row, int col) Write a method to add two matrices. The header of the method is as follows: public static intill addMatrix(intUti a, intI b) Write a method to display the matrix. The header of the method is as follows: public static void print Matrix(intll a) Write a complete program that reads two 3X3 matrices and displays their sum. * Example is as following. 4 sé Enter i 2 3 axXS matzix2 rou by row 7 s 9 The aun of matrices 2:46 8 10 12 14 16 18

Explanation / Answer

In my opinion i had done the code with basoc assumptions those are:

The inputting matrices should square matrices only (equal num of rows and cols) should in both matrices.

You can put that condition as per your need inplace where mentioned about the condition statement in the program. I had added much comments on code if anywhere they omit the lines of your compiler please do comment the general english language please...since i had did it in Text Editor.I did code for 3*3 only you made it for n*n also but do necessary changes by following comments.

The program designed as per above instructions and executed correctly...if any queries please comment on the answer i will try to respond as soon as possible.

Code for Adding two Matrices: Save the file with AddMtrix.java

import java.util.Scanner;
public class AddMatrix {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
// System.out.print("Enter number of rows of matrix: "); in case of dynamically assign rows
// int rows_count= input.nextInt();
int rows_count=3;
// System.out.print("Enter number of columns of matrix: "); in case of dynamically assign columns
// int columns_count= input.nextInt();
int columns_count= 3;
System.out.println();
System.out.println("Enter 3*3 matrix1 row by row:");
int[][] a= inputMatrix(rows_count, columns_count); //to read the imput matrix1
System.out.println();
System.out.println("Enter 3*3 matrix2 row by row:"); //to read the imput matrix2
int[][] b= inputMatrix(rows_count, columns_count); //to call the adding method
int[][] sum= addMatrix(a, b); // here we can put condition since matrix addition needs only square matrices.
System.out.println();
System.out.println("The Sum of 3*3 Matrices:");
printMatrix(sum); //to call the display method
System.out.println();
}

public static int[][] inputMatrix(int row, int col) { //Takes input and returns array
int[][] result= new int[row][col];
Scanner input= new Scanner(System.in);
for (int i= 0; i < row; i++) {
for (int j= 0; j < col; j++) {
result[i][j]= input.nextInt();
}
}
return result;
}

public static int[][] addMatrix(int[][] a, int[][] b) { //Takes inputted arrays ans sum it up then return them
int rows= a.length; //to get rows length
int cols= a[0].length; //to cols length by values in first column
int[][] result= new int[rows][cols];
// here also we can put condition since matrix addition needs only square matrices.
for (int i= 0; i < rows; i++) {
for (int j= 0; j < cols; j++) {
result[i][j]= a[i][j] + b[i][j];
}
}
return result;
}

public static void printMatrix(int[][] a) { //Takes ouput array and displays it.
int rows= a.length; //to get rows length
int cols= a[0].length; //to cols length by values in first column
for (int i= 0; i < rows; i++) {
for (int j= 0; j < cols; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote