Question: Program requirements: Matrix class The private field is a two dimensional array of int values Since the size is not yet known, this can be
Program requirements: Matrix class
The private field is a two dimensional array of int values
Since the size is not yet known, this can be set up as follows:
tablefield for two dim arrayprivate int matrixVals;
The constructor calls the setMatrixValsmVals setter method to assign the correct information to the private field
The setMatrixValsmVals setter method must make a copy of the parameter
This copy becomes the private field
The getMatrixVals getter method must return a copy of the field
Program requirements continued: Matrix class
The sameSizem method returns true if the this object and the parameter Matrix object ie have the same number of rows and columns
Else returns false
The transpose method returns the this object transposed
Therefore, the rows become the columns and the columns become the rows
The scalarMultscVal method returns the Matrix object that is obtained by multiplying all elements of the this object by the scVal data type int value of the parameter
The addm method returns the Matrix object that results from adding the this object and the parameter Matrix object
The toString method returns a consistent representation of the two dimensional array ie the private field
Program requirements continued: Matrix class
The getMatrixVals getter method must return a copy of the field
Why is this important?
If the method returns only the name of the array, it is actually returning the address in memory
Therefore, any other method that has access to this address can potentially change the information stored at that address.
Therefore, to protect the security of the data, we must return a copy and not the address
The same concept applies to the setter method, which is why a copy of the parameter array must be made and assigned to the private field
Note that this will necessitate implementing a nested for loop and copying each value in the correct place
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
