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

JAVA Programming. Your assignment is to write a program that inputs two square m

ID: 3766605 • Letter: J

Question

JAVA Programming.

Your assignment is to write a program that inputs two square matrices and then calls methods within a class you create that adds, subtracts, and multiplies the two matrices.

Your program must first ask the user the size of the square matrices that are to be used. This must be a number from 2 to 6. If the number is not in this range, the user must be prompted again and again until the user enters a valid size. Next the user is requested to enter the two matrices. Make certain that you give good directions on how this is to be done! If I cannot understand your directions without asking you, the directions will not be considered to be good.

Next the program prints out the two matrices and then invokes the three methods that your have created in your new class. The first method adds the two matrices, the next subtracts the second from the first, and the third multiplies the two matrices. The program must print the results of these operations after each of the methods returns. Make certain to output these matrices in a way that is easy to read and understand!

Part of your assignment is to use the Internet, the library, or some other source to find out how to add, subtract, and multiply two square matricies. Note, while, the add and subtract are fairly straightforward, the multiply is a bit different – make certain that you get it done correctly. Make certain to validate the functioning of your program before you have me check it.

Thank you.

Explanation / Answer

Here is the code for you. If you have any further queries, just get back to me.

import java.io.*;
import java.util.*;

class SquareMatrixOperations
{
//Function to add two matrices.
public static int[][] addMatrix(int[][] X, int[][] Y)
{
int[][] Z = new int[X.length][X.length];
for(int i = 0; i < X.length; i++)
for(int j = 0; j < X.length; j++)
Z[i][j] = X[i][j] + Y[i][j];
return Z;
}
//Function to subtract two matrices.
public static int[][] subtractMatrix(int[][] X, int[][] Y)
{
int[][] Z = new int[X.length][X.length];
for(int i = 0; i < X.length; i++)
for(int j = 0; j < X.length; j++)
Z[i][j] = X[i][j] - Y[i][j];
return Z;
}
//Function to multiply two matrices.
public static int[][] multiplyMatrix(int[][] X, int[][] Y)
{
int[][] Z = new int[X.length][X.length];
for(int i = 0; i < X.length; i++)
for(int j = 0; j < X.length; j++)
{
Z[i][j] = 0;
for(int k = 0; k < X.length; k++)
Z[i][j] += X[i][k] * Y[k][j];
}
return Z;
}
//Function to print a matrix.
public static void printMatrix(int[][] X)
{
for(int i = 0; i < X.length; i++)
{
for(int j = 0; j < X.length; j++)
System.out.print(X[i][j]+ " ");
System.out.println();
}
}
public static void main(String[] args)
{
System.out.print("Enter the size of the square matrix to be used (should be in the range 2 - 6): ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n < 2 || n > 6)
{
System.out.println("The matrix size should be in the range 2 - 6.");
System.out.print("Enter the size of the square matrix to be used (should be in the range 2 - 6): ");
n = sc.nextInt();
}
int[][] A = new int[n][n];
int[][] B = new int[n][n];
int[][] C = new int[n][n];
System.out.println("Enter the first matrix A of size "+n+" x "+n+":");
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
A[i][j] = sc.nextInt();
System.out.println("Enter the first matrix B of size "+n+" x "+n+":");
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
B[i][j] = sc.nextInt();
System.out.println("The matrix A is:");
printMatrix(A);
System.out.println("The matrix B is:");
printMatrix(B);
C = addMatrix(A, B);
System.out.println("The sum of the given two matrices is: ");
printMatrix(C);
C = subtractMatrix(A, B);
System.out.println("The difference of the given two matrices is: ");
printMatrix(C);
C = multiplyMatrix(A, B);
System.out.println("The product of the given two matrices is: ");
printMatrix(C);
}
}