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

JAVA Write a method to add two matrices. The header of the method is as follows:

ID: 3693574 • Letter: J

Question

JAVA

Write a method to add two matrices. The header of the method is as follows: public static doub1e[][] addMatrix(double[] [] a, double[][] b) In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element C_ij is a_ij + b_ij. For example, for two 3 Times 3 matrices a and b, c is (a_11 a_21 a_31 a_21 a_22 a_32 a_31 a_23 a_33) + (b_11 b_21 b_31 b_12 b_22 b_32 b_13 b_23 b_33) = (a_11 + b_11 a_21 + b_21 a_31 + b_31 a_12 + b_12 a_22 + b_22 a_32 + b_32 a_13 + b_13 a_23 + b_23 a_33 + b_33) Write a test program that prompts the user to enter two 3 Times 3 matrices and displays their sum. Here is a sample run: Enter matrix: 1 2 3 4 5 6 7 8 9 Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 The matrices are added as follows 1.0 4.0 7.0 2.0 5.0 8.0 3.0 6.0 9.0 + 0.0 1.0 1.1 2.0 4.5 4.3 4.0 2.2 5.2 = 1.0 5.0 8.1 4.0 9.5 12.3 7.0 8.2 14.2

Explanation / Answer

MatrixAddition.java

package org.students;

import java.util.Scanner;

public class MatrixAddition {

   public static void main(String[] args) {
       //Declaring Two dimensional arrays
       double matarr1[][]=new double[3][3];
       double matarr2[][]=new double[3][3];
       //Scanner object is used to get the inputs from the keyboard
   Scanner sc=new Scanner(System.in);
  
   //For loop which get the input from the user and fills into 1st matrix
   System.out.print("Enter Martrix 1:");
   for(int i=0;i<3;i++)
   {
       for(int j=0;j<3;j++)
       {   
           matarr1[i][j]=sc.nextDouble();
       }
   }
   //For loop which get the input from the user and fills into 2nd matrix
   System.out.print("Enter Martrix 2:");
   for(int i=0;i<3;i++)
   {
       for(int j=0;j<3;j++)
       {
           matarr2[i][j]=sc.nextDouble();
       }
      
   }
   //calling the method
   double sum[][]=addMatrix(matarr1,matarr2);
  
   //Displays the result
   System.out.println("The matrices area added as follows ::");
   for(int i=0;i<3;i++)
   {
       for(int j=0;j<3;j++)
       {
           System.out.print(matarr1[i][j]+" ");
          
       }
       if(i==1)
           System.out.print(" + ");
       else
           System.out.print(" ");
       for(int j=0;j<3;j++)
       {
           System.out.print(matarr2[i][j]+" ");
          
       }
       if(i==1)
           System.out.print(" = ");
       else
           System.out.print(" ");
       for(int j=0;j<3;j++)
       {
           System.out.print(sum[i][j]+" ");
       }
       System.out.println(" ");
   }
  
   }
// Static method which add the two matrices and returns the resultant matrix to the calling method
   private static double[][] addMatrix(double[][] matarr1, double[][] matarr2)
   {
       double sum[][]=new double[3][3];
       for(int i=0;i<3;i++)
       {
           for(int j=0;j<3;j++)
           {
               sum[i][j]=matarr1[i][j]+matarr2[i][j];
           }
          
       }
       return sum;
   }

}

_________________________________________________________________________________________

output:

Enter Martrix 1:1 2 3 4 5 6 7 8 9
Enter Martrix 2:0 2 4 1 4.5 2.2 1.1 4.3 5.2
The matrices area added as follows ::
1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0
4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2

_________________________________________________________________________________________