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

Matrix Subtraction Objective: Write a program that can create two matrices and t

ID: 3585845 • Letter: M

Question

Matrix Subtraction

Objective:

Write a program that can create two matrices and then subtracts them!

The user first inputs the size length and width

The user then inputs the size length and width of the next matrix

Next the user populates that matrices with data

The program must check to see if the dimensions match. Matrix subtraction requires the size of both to be the same. If they are not then print “Cannot subtract these! Dimension mismatch”           

If they do match correctly then create a new matrix and put the newly subtracted info inside of it.

Subtract each component wise, so A[1][1] +-B[1][1] = C[1][1]

Example Dialog:

Welcome to the matrix subtracting program

Please enter the length of the first matrix

2

Please enter the height of the first matrix

2

Please enter the length of the second matrix

2

Please enter the height of the second matrix

2

Please enter a value for matrix 1 space 1, 1

4

Please enter a value for matrix 1 space 1, 2

1

Please enter a value for matrix 1 space 2, 1

8

Please enter a value for matrix 1 space 2, 2

7

Please enter a value for matrix 2 space 1, 1

1

Please enter a value for matrix 2 space 1, 2

3

Please enter a value for matrix 2 space 2, 1

2

Please enter a value for matrix 2 space 2, 2

4

4 1

8 7

-

1 3

2 4

=

3 -2

6 3

DONE!

i need it by Drjava program please

Explanation / Answer

MatrixSubtraction.java

import java.util.Scanner;

public class MatrixSubtraction {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Welcome to the matrix subtracting program");

System.out.println("Please enter the length of the first matrix:");

int l1 = scan.nextInt();

System.out.println("Please enter the height of the first matrix:");

int h1 = scan.nextInt();

int a[][]= new int[l1][h1];

System.out.println("Please enter the length of the second matrix:");

int l2 = scan.nextInt();

System.out.println("Please enter the height of the second matrix:");

int h2 = scan.nextInt();

int b[][]= new int[l2][h2];

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

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

System.out.println("Please enter a value for matrix "+(i+1)+" space "+(i+1)+", "+(j+1));

a[i][j] = scan.nextInt();

}

}

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

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

System.out.println("Please enter a value for matrix "+(i+1)+" space "+(i+1)+", "+(j+1));

b[i][j] = scan.nextInt();

}

}

if(l1==l2 && h1 == h2) {

int c[][] = new int[l1][h1];

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

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

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

}

System.out.println();

}

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

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

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

}

System.out.println();

}

System.out.println("=");

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

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

c[i][j] = a[i][j] - b[i][j];

}

}

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

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

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

}

System.out.println();

}

System.out.println("DONE!");

} else {

System.out.println("Cannot subtract these! Dimension mismatch");

}

}

}

Output:

Welcome to the matrix subtracting program
Please enter the length of the first matrix:
2
Please enter the height of the first matrix:
2
Please enter the length of the second matrix:
2
Please enter the height of the second matrix:
2
Please enter a value for matrix 1 space 1, 1
4
Please enter a value for matrix 1 space 1, 2
1
Please enter a value for matrix 2 space 2, 1
8
Please enter a value for matrix 2 space 2, 2
7
Please enter a value for matrix 1 space 1, 1
1
Please enter a value for matrix 1 space 1, 2
3
Please enter a value for matrix 2 space 2, 1
2
Please enter a value for matrix 2 space 2, 2
4
4 1
8 7
1 3
2 4
=
3 -2
6 3
DONE!