/*
Exercise 2: Design and implement a Java program for programming exercise 8.5, page 306 (name it
AddMatricies) as described in the problem statement. Write method addMatrix() as specified. To test this
method, the main method of your program prompts the user to enter two matrices that have the same
dimensions, and then display their sum as shown in the sample run. 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). Document your code,
and organize and space the outputs properly. Use escape characters and formatting objects when applicable. 

(Algebra: add two matrices) Write a method to add two matrices. The header of
the method is as follows:
public static double[][] 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
cij is aij + bij. For example, for two 3 * 3 matrices a and b, c is

Write a test program that prompts the user to enter two 3 * 3 matrices and
displays their sum. Here is a sample run:


Enter matrix1: 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

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


*/

import java.util.Scanner;
public class AddMatricies
{
   public static void main (String[] args)
   {
      while(true)
      {
         Scanner input = new Scanner(System.in);
         double [][] matrix_1 = new double [3][3];
         double [][] matrix_2 = new double [3][3];
         double [][] matrix_3;
      // Prompt the user to enter two 3x3 matrixes
        
         System.out.print("Enter matrix1: ");
         for (int row = 0; row < matrix_1.length; row++) 
         {
            for (int column = 0; column < matrix_1[row].length; column++) 
            {
               matrix_1[row][column] = input.nextDouble();
            }
         }
      
         System.out.print("Enter matrix2: ");
         for (int row = 0; row < matrix_2.length; row++) 
         {
            for (int column = 0; column < matrix_2[row].length; column++) 
            {
               matrix_2[row][column] = input.nextDouble();
            }
         }   
         
         matrix_3 = addMatrix(matrix_1, matrix_2);// assigning the result to another matrix             
      // Using loop to print out the sum of 2 matrixes 
         for (int i = 0; i < matrix_3.length; i++) 
         {
            for (int j = 0; j < matrix_3[0].length; j++) 
            {
               System.out.print(matrix_3[i][j] + " ");
            } 
            System.out.println();              
         }
         System.out.println("");
      }// end of re-run  
      
   }// end of main    
   
   // The method for adding the 2 dimensional arrays 
   
   public static double[][] addMatrix(double[][] a, double[][] b)
   {
    // Verifying the dimensions of the matrixex 
      if (a.length != b.length || a[0].length != b[0].length)
         return null;
     
     // create a new 2 dimensional array to store the sum 
     
      double[][] output = new double[b.length][a[0].length];
   
      for (int i = 0; i < output.length; i++) 
      {
         for (int j = 0; j < output[0].length; j++) 
         {
            output[i][j] = a[i][j] + b[i][j];
         }
      
      }
   
      return output;
   
   }// end of the method 
   
   
}// end of the class
     
  
 
