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

Write a java program called AddMatricies that reads in two matrices of doubles t

ID: 3768234 • Letter: W

Question

Write a java program called AddMatricies that reads in two matrices of doubles that are both 3-by-3 in dimension, and then add them up and display the sum of the two matrices according to the following formula:

The user will enter the values for each matrix one row at a time. And your main() method must call the following method to accomplish the task of adding the matrices:

Design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure).

Here is a sample output:

Comment your code properly and be sure

Explanation / Answer

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class MatrixAddition {

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);

       // declare two matrix with 3X3 size
       double[][] a = new double[3][3];
       double[][] b = new double[3][3];
       do {
           // reading matrix a from keyboard
           System.out.println("Enter your first 3-by-3 matrix row by row:");
           for (int i = 0; i < 3; i++) {
               for (int j = 0; j < 3; j++) {
                   a[i][j] = s.nextDouble();
               }
           }
           // reading matrix b from keyboard
           System.out.println("Enter your second 3-by-3 matrix row by row:");
           for (int i = 0; i < 3; i++) {
               for (int j = 0; j < 3; j++) {
                   b[i][j] = s.nextDouble();
               }
           }

           // calling add matrix and assign to c
           double[][] c = addMatrix(a, b);

           // printing c matrix(addition of a and b)
           System.out.println("The sum of those two matrices is:");
           for (int i = 0; i < 3; i++) {
               for (int j = 0; j < 3; j++) {
                   System.out.print(c[i][j] + " ");
               }
               System.out.println();
           }
           System.out.println("Would you like to go again?");
           String ch = s.next();
           if (ch.equalsIgnoreCase("no")) {
               break;
           }
       } while (true);
   }

   /**
   * @param a
   * @param b
   * @return addition of a and b
   */
   public static double[][] addMatrix(double[][] a, double[][] b) {

       double[][] c = new double[3][3];
       for (int i = 0; i < 3; i++) {
           for (int j = 0; j < 3; j++) {
               c[i][j] = a[i][j] + b[i][j];
           }
       }
       return c;
   }

}

OUTPUT:

Enter your first 3-by-3 matrix row by row:
1 2 3
4 5 6
7 8 9
Enter your second 3-by-3 matrix row by row:
1 2 3
4 5 6
7 8 9
The sum of those two matrices is:
2.0 4.0 6.0
8.0 10.0 12.0
14.0 16.0 18.0
Would you like to go again?
yes
Enter your first 3-by-3 matrix row by row:
2 1 3
3 2 3
5 4 6
Enter your second 3-by-3 matrix row by row:
2 3 3
4 3 2
5 3 5
The sum of those two matrices is:
4.0 4.0 6.0
7.0 5.0 5.0
10.0 7.0 11.0
Would you like to go again?
no

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote