Question
Im still learing and dont know how or what to do. Gain additional experience writing a class and instantiating objects in the driver class Gain
Im still learing and dont know how or what to do.
Gain additional experience writing a class and instantiating objects in the driver class Gain additional experience implementing two dimensional arrays and nested for loops Implement the following matrix operations: Transpose The rows become the columns and the columns become the rows Scalar multiply Add two matrices Note that two matrices can be added only if they are the same size That is, they must both have the same number of rows and columns
n the driver class do the following: Write a method that returns a two dimensional array of random data type int values
This facilitates instantiating Matrix objects Instantiate a Matrix object Display the Matrix object Transpose and display the transposed object Multiply the Matrix object by a scalar (i.e. data type int) value and display the resultant Matrix Instantiate and display a second Matrix object If the two Matrix objects are the same size, display their sum Else display an error message that matrices cannot be added unless they are the same size
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: The constructor calls the setMatrixVals(mVals) setter method to assign the correct information to the private field The setMatrixVals(mVals) 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
The sameSize(m2) method returns true if the this object and the parameter Matrix object (i.e. m2) 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 scalarMult(scVal) 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 add(m2) 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 (i.e. the private field) 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
Implement a method to return a two dimensional array of random int values Parameters: the number of rows and columns and the maxRand val In the main method: Instantiate a Matrix object The size can be set up as constant values or obtained from the user Display the Matrix by calling the toString( ) method Transpose and display the transposed Matrix Scalar multiply and display the resultant Matrix (the scalar value can be set as a constant or obtained from the user) Instantiate a second Matrix object Display the second Matrix If the two are the same size, add the two Matrix objects and display resultant sum Else, display error message that they cannot be added
Create two new Java files, named as follows: Matrix.java MatrixDemo.java
Program output: - The scalar value is set at 2 - Since Matrix A and Matrix B are not the same size, they cannot Matrix A+ Matrix B : be added To add matrices, they must Program output: - The scalar value is set at 2 - Since Matrix A and Matrix B are the same size, they can be addedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started