title: Adding Matrices
body: <p>In this program I am attempting to create a new method that can add two of the matrices that are created. In order to do this I need to check to see if the matrices have the same dimensions and then print out the added matrices. The matrices are filled with random numbers that are set in the code. Can someone help create a method that will allow me to add two of them together?</p>
  <pre><code>public class Matrix {
      private int [][] grid;  
      
      /**
       * default constructor: creates 3x3 matrix with random values
       * in the range 0..9
       */
      public Matrix() {
          grid = new int[3][3];
          for(int x=0; x&lt;3; x++)
              for(int y=0; y&lt;3; y++)
                  grid[x][y] = (int)(Math.random()*10);
      }
      
      /**
       * Creates matrix of specified size with random values 0..9
       * @param size positive integer that represents the number of rows
       * and columns in the matrix
       */
      public Matrix(int size) {
          grid = new int[size][size];
          for(int x=0; x&lt;size; x++)
              for(int y=0; y&lt;size; y++)
                  grid[x][y] = (int)(Math.random()*10);
      }
      
      /** 
       * Creates a matrix of specified size with random values 0..9
       * @param rows number of rows in matrix
       * @param columns number of columns int matrix
       */
      public Matrix(int rows, int columns) {
          grid = new int[rows][columns];
          for(int x=0; x&lt;rows; x++)
              for(int y=0; y&lt;columns; y++)
                  grid[x][y] = (int)(Math.random()*10);
      }
      
      /**
       * @return String formatted as an m x n matrix of m rows and 
       * n columns
       */
      public String toString() {
          int rows = grid.length;
          int columns = grid[0].length;
          String table = new String(&quot;&quot;);
          for(int x=0; x&lt;rows; x++) {
              table = table + '|' + '\t';
              for(int y=0; y&lt;columns; y++)
                  table = table + grid[x][y] + '\t';
              table = table + '|' + '\n';
          }
          return table;
      }
      
      /**
       * @return true if number of rows equals number of columns
       */
      public boolean isSquare() {
          return grid.length == grid[0].length;
      }
      
      /**
       * @param other another matrix to compare to this one
       * @return true if this matrix and the other have the same
       * number of rows and columns
       */
      public boolean sameSize(Matrix other) {
          return grid.length == other.grid.length &amp;&amp; 
                  grid[0].length == other.grid[0].length;
      }
      // main method: for testing
      public static void main(String[] args) {
          
          Matrix m = new Matrix();
          Matrix n = new Matrix((int)(Math.random()*5)+2);
          Matrix o = new Matrix(((int)(Math.random()*5)+2),
                              ((int)(Math.random()*5)+2));
          System.out.println (&quot;First matrix:&quot;);
          System.out.print(m);
          System.out.println (&quot;Second matrix:&quot;);
          System.out.println(n);
          System.out.println (&quot;Third matrix:&quot;);
          System.out.println(o);
          if(m.sameSize(n))
              System.out.println(&quot;First two are the same size&quot;);
          else
              System.out.println(&quot;First two are not the same size&quot;);
          if(o.isSquare())
              System.out.println(&quot;All three are square matrices&quot;);
          else
              System.out.println(&quot;Only first two are square matrices&quot;);      
      }
  }
  </code></pre>