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.

Respuesta :

Answer:

// Java profram to perform Arithmetic operations on square matrices

// Comments are used for explanatory purpose

// Program starts here

import java.util.*;

import java.io;

public class Matrices {

// Multiply Method Here

static void multiply(int mat1[][],int mat2[][], int mulmat[][])

{

int i, j, k;

for (i = 0; i < N; i++)

{

for (j = 0; j < N; j++)

{

mulmat[i][j] = 0;

for (k = 0; k < N; k++)

mulmat[i][j] += mat1[i][k] * mat2[k][j];

}

}

System.out.println("Result matrix" + " is ");

for (i = 0; i < N; i++)

{

for (j = 0; j < N; j++)

System.out.print( mulmat[i][j] + " ");

System.out.println();

}

}

// Subtraction Method here

static void sub(int mat1[][],int mat2[][], int submat[][])

{

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

addmat[i][j]=mat1[i][j]-mat2[i][j]; //

System.out.print(submat[i][j]+" ");

}

}

// Addition Method here

static void add(int mat1[][],int mat2[][], int addmat[][])

{

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

addmat[i][j]=mat1[i][j]+mat2[i][j]; //

System.out.print(addmat[i][j]+" ");

}

}

// Main Method Starts here

public static void main (String [] args)

{

Scanner input = new Scanner (System.in);

// Declare type of square matrix

int N;

// input N

lbl: N = input.nextInt();

if(N>=2 && N<=6)

{

int mulmat[][] = new int[N][N] ;

int addmat[][] = new int[N][N] ;

int submat[][] = new int[N][N] ;

// Multiplication

mulmat(mat1, mat2, mulmat);

// Addition

addmat(mat1, mat2, addmat);

// Subtraction

submat(mat1, mat2,submat);

}

else

{

goto lbl;

}

}