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

Need help with Intro to Java Programming problem: For this assignment you will w

ID: 3789480 • Letter: N

Question

Need help with Intro to Java Programming problem:

For this assignment you will write a class Matrix to operate with NxN matrices. The class should be saved in a file called Matrix.java. I have provided a template that you should download from D2L. The class has the following instance variables:

private double[][] M - an array representing the Matrix

private final int MIN_SIZE - a constant representing the minimum size of a Matrix; it should be initialized to 0

private final int MAX_SIZE - constant representing the maximum size of a Matrix; it should be initialized to 100

private static int mCount -- the number of Matrix objects that currently exist; it should be initialized to 0

You will complete the assignment by writing the following methods for the Matrix class. Note that where appropriate the methods should modify the mCount variable:

public Matrix(int N): A constructor that creates a Matrix of size N x N filled with MIN_SIZE. N must be in the range [MIN_SIZE, MAX_SIZE]. If N is less than MIN_SIZE, create a Matrix of size MIN_SIZE x MIN_SIZE. If N is larger than MAX_SIZE, create a Matrix of size MAX_SIZE x MAX_SIZE. The assignment of the values in the array must be done using explicit assignment statements. Do not rely on default values assigned when any variables are created.

public Matrix(int N, double[][] initValues): A constructor that creates a Matrix of size N x N and fills it with the values found in the array initValues. N must be in the range [MIN_SIZE, MAX_SIZE]. If N is less than MIN_SIZE, create a Matrix of size MIN_SIZE x MIN_SIZE. If N is larger than MAX_SIZE, create a Matrix of size MAX_SIZE x MAX_SIZE. You may assume that the array initValues has at least N x N double values in it.

public void print(): Display the Matrix using System.out.println, one row per line. Separate the entries using commas, but do not place a comma after the last entry in a row.

public Matrix add(Matrix aMatrix): Create a new Matrix formed by adding the calling Matrix and aMatrix entry by entry. This means that the entry in position (i, j) of the calling Matrix should be added to the entry in position (i, j) of aMatrix for each possible value of i and j. If the two Matrices are not of the same size (that is, one is of size N0 x N0 and one is of size N1 x N1 where N0 is not equal to N1), then your method should report that the operation is not possible and return a Matrix of size equal to the calling Matrix filled with MIN_SIZE. The original Matrix object (e.g. the calling Matrix) and the Matrix provided as a parameter should not be changed by the method.

public static int getNumMatrices(): Return the number of Matrix objects currently in existence.

I have provided a driver program (e.g. a main method that uses the Matrix class) Hw5.java to help you test your code which has been posted to D2L. Note that you cannot assume that the test cases provided there are the only ones the grader will use. Also, please only submit the Matrix.java file.

------------------------------------------------------------------------

Matrix.java:

package hw5;

public class Matrix {

   private double[][] M;
   private final int MIN_SIZE = 0;
   private final int MAX_SIZE = 100;
  
   private static int mCount = 0;

   // Create a new Matrix, initializing it to zeros
   // The dimensions must be in the range [1, MAX_SIZE]
   public Matrix(int size) {

   }

   // Create a new Matrix, initializing it with the parameter
   // The dimensions must be in the range [0, MAX_SIZE]
   public Matrix(int size, double[][] initValues) {
  
   }

   // Display the Matrix, one row per line
   public void print() {

  
   }

   // Add two Matrices
   // If the sizes do not match return a Matrix full of zeros
   public Matrix add(Matrix secondMatrix) {

       // Remove this stub when you write the method
       return new Matrix(MIN_SIZE);
   }
  
   public static int getNumMatrices() {
      
       // Remove this stub when you write the method
       return 0;
   }
  
}

---------------------------------------------------------------------------------------

HW5.java:

package hw5;

public class Hw5 {
  
   public static void main(String[] args) {
  
       double[][] test = {{1.5, 2, 3}, {3, 2.5, 4}, {2.5, 4, 2.5}};

       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       Matrix firstMatrix = new Matrix(3, test);
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       Matrix secondMatrix = new Matrix(2), errorMatrix = new Matrix(-3);
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       Matrix sumMatrix;
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       Matrix thirdMatrix = new Matrix(3, test);
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       System.out.println();

       System.out.println("The original Matrix:");
       firstMatrix.print();
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       System.out.println();

       System.out.println("The sum of two Matrices:");
       sumMatrix = firstMatrix.add(thirdMatrix);
       sumMatrix.print();
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       System.out.println();
      
       System.out.println("The original Matrix:");
       firstMatrix.print();
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       System.out.println();

       System.out.println("Look at an error message:");
       errorMatrix = secondMatrix.add(firstMatrix);
       errorMatrix.print();
       System.out.println("There are " + Matrix.getNumMatrices() +
               " Matrices.");
       System.out.println();

   }

}

-----------------------------------------------------------------------------------------------

The following is the output from my completed solution using the Hw5.java driver program:

There are 0 Matrices.
There are 1 Matrices.
The size of a matrix cannot be less than 0
There are 3 Matrices.
There are 3 Matrices.
There are 4 Matrices.

The original Matrix:
1.5, 2.0, 3.0
3.0, 2.5, 4.0
2.5, 4.0, 2.5
There are 4 Matrices.

The sum of two Matrices:
3.0, 4.0, 6.0
6.0, 5.0, 8.0
5.0, 8.0, 5.0
There are 5 Matrices.

The original Matrix:
1.5, 2.0, 3.0
3.0, 2.5, 4.0
2.5, 4.0, 2.5
There are 5 Matrices.

Look at an error message:
Error(add): Matrices are of unequal sizes!
0.0, 0.0
0.0, 0.0
There are 6 Matrices.

Explanation / Answer

----------------------------------------------------Matrix.java-----------------------------------------------------------------------

public class Matrix {

   private double[][] M;
   private final int MIN_SIZE = 0;
   private final int MAX_SIZE = 100;

   private static int mCount = 0;

   // Create a new Matrix, initializing it to zeros
   // The dimensions must be in the range [1, MAX_SIZE]
   public Matrix(int size) {
       mCount++;
       if (size < 0) { // checking the condition of size
           System.out.println("The size of a matrix cannot be less than "+MIN_SIZE+"");
           M = new double[MIN_SIZE][MIN_SIZE];
          
       } else if (size > 100) {
           System.out.println("The size of a matrix cannot be greter than "+MAX_SIZE+"");
           M = new double[MAX_SIZE][MAX_SIZE];
          
       }else{
       M = new double[size][size];
       }

       for (int i = 0; i < size; i++) { // assigning the value to the matrix

           for (int j = 0; j < size; j++) {

               M[i][j] = 0;
           }
       }

   }

   // Create a new Matrix, initializing it with the parameter
   // The dimensions must be in the range [0, MAX_SIZE]
   public Matrix(int size, double[][] initValues) {
       mCount++;
       if (size < 0) { // checking the condition of size
           M = new double[MIN_SIZE][MIN_SIZE];
          
       } else if (size > 100) {
           M = new double[MAX_SIZE][MAX_SIZE];
          
       }else{
       M = new double[size][size];
       }
      

       for (int i = 0; i < size; i++) { // assigning the value to the matrix

           for (int j = 0; j < size; j++) {

               M[i][j] = initValues[i][j];
           }
       }
   }

   // Display the Matrix, one row per line
   public void print() {
       //iterating the array to print row by row
       for (int i = 0; i < M.length; i++) {

           for (int j = 0; j < M.length; j++) {
               if(j!=0){
                   System.out.print(", ");
               }
               System.out.print(M[i][j]);
           }
           System.out.println();
       }
   }

   // Add two Matrices
   // If the sizes do not match return a Matrix full of zeros
   public Matrix add(Matrix secondMatrix) {
       double[][] sum = new double[M.length][M.length]; // creating an sum array for creating sum matrix
       double [][]secondMatrixData=secondMatrix.M; // getting the array from second matrix
       if(M.length!=secondMatrixData.length){
           System.out.println("Error(add): Matrices are of unequal sizes!");
           return new Matrix(M.length);
       }
       for (int i = 0; i < M.length; i++) {

           for (int j = 0; j < M.length; j++) {

               sum[i][j] = M[i][j] + secondMatrixData[i][j]; // adding the elements
           }
       }
       return new Matrix(M.length,sum); //return the new sum matrix
   }

   public static int getNumMatrices() {

      
       return mCount;
   }
}

------------------------------------------------------------------------------HW5.java-------------------------------------------------------------

public class HW5 {

   public static void main(String[] args) {
      
   double[][] test = {{1.5, 2, 3}, {3, 2.5, 4}, {2.5, 4, 2.5}};
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   Matrix firstMatrix = new Matrix(3, test);
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   Matrix secondMatrix = new Matrix(2), errorMatrix = new Matrix(-3);
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   Matrix sumMatrix;
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   Matrix thirdMatrix = new Matrix(3, test);
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   System.out.println();
   System.out.println("The original Matrix:");
   firstMatrix.print();
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   System.out.println();
   System.out.println("The sum of two Matrices:");
   sumMatrix = firstMatrix.add(thirdMatrix);
   sumMatrix.print();
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   System.out.println();
  
   System.out.println("The original Matrix:");
   firstMatrix.print();
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   System.out.println();
   System.out.println("Look at an error message:");
   errorMatrix = secondMatrix.add(firstMatrix);
   errorMatrix.print();
   System.out.println("There are " + Matrix.getNumMatrices() +
   " Matrices.");
   System.out.println();
   }
}

-----------------------------------------------------------------------------------output---------------------------------------------------------------

There are 0 Matrices.
There are 1 Matrices.
The size of a matrix cannot be less than 0
There are 3 Matrices.
There are 3 Matrices.
There are 4 Matrices.

The original Matrix:
1.5, 2.0, 3.0
3.0, 2.5, 4.0
2.5, 4.0, 2.5
There are 4 Matrices.

The sum of two Matrices:
3.0, 4.0, 6.0
6.0, 5.0, 8.0
5.0, 8.0, 5.0
There are 5 Matrices.

The original Matrix:
1.5, 2.0, 3.0
3.0, 2.5, 4.0
2.5, 4.0, 2.5
There are 5 Matrices.

Look at an error message:
Error(add): Matrices are of unequal sizes!
0.0, 0.0
0.0, 0.0
There are 6 Matrices.