Question: Write a class Matrix to operate with NxN matrices in Java. The class has the following instance variables. private double[][] M - an array representing

Write a class Matrix to operate with NxN matrices in Java. 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.

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 sMult(double val): Create a new Matrix formed by multiplying each entry of the calling Matrix by the value val. The original Matrix object should remain unchanged.

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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!