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

Follow all instructions exactly given below and did not deviate from instruction

ID: 3808495 • Letter: F

Question

Follow all instructions exactly given below and did not deviate from instructions. Keep the program's code as simple and begineer friendly:

Write a program that accepts two 3 x 3 matrices and invokes a method (The method must be created within the main program) to add them together. The formula for adding two matrices is shown below:

You will need:

A scanner object to read the values that fill the arrays

Three 2-dimensional arrays:

One to store the values in the first 3x3 matrix

One to store the values in the second 3x3 matrix

One to store the sum of the values from the first two matrices

A method that adds the two matrices using the formula above

Comments where necessary

A sample of the output is shown below:

Enter matrix1: 1 2 3 4 5 6 7 8 9
Enter matrix2: 1 2 3 4 5 6 7 8 9
The addition of the matrices is

1.0 2.0 3.0     
4.0 5.0 6.0    
7.0 8.0 9.0    
      +

1.0 2.0 3.0     
4.0 5.0 6.0    
7.0 8.0 9.0

      =

2.0 4.0 6.0

8.0 10.0 12.0

14.0 16.0 18.0

Explanation / Answer

MatrixDisplay.java

import java.util.Scanner;


public class MatrixDisplay {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       double first[][] = new double[3][3];
       double second[][] = new double[3][3];
       double sum[][] = new double[3][3];
       System.out.print("Enter matrix1: ");
       for(int i=0; i<first.length; i++){
           for(int j=0; j<first[i].length; j++) {
               first[i][j] = scan.nextDouble();
           }
       }
       System.out.print("Enter matrix2: ");
       for(int i=0; i<second.length; i++){
           for(int j=0; j<second[i].length; j++) {
               second[i][j] = scan.nextDouble();
           }
       }
       System.out.println("The addition of the matrices is ");
       printMatrix(first);
       System.out.println(" + ");
       printMatrix(second);
       System.out.println(" = ");
       for(int i=0; i<sum.length; i++){
           for(int j=0; j<sum[i].length; j++) {
               sum[i][j] = first[i][j] + second[i][j] ;
           }
       }
       printMatrix(sum);
      
      
   }

   public static void printMatrix(double a[][]) {
       for(int i=0; i<a.length; i++){
           for(int j=0; j<a[i].length; j++) {
               System.out.print(a[i][j]+" ");
           }
           System.out.println();
       }
   }
}

Output:

Enter matrix1: 1 2 3 4 5 6 7 8 9
Enter matrix2: 1 2 3 4 5 6 7 8 9
The addition of the matrices is
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

+

1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

=

2.0 4.0 6.0
8.0 10.0 12.0
14.0 16.0 18.0