Lab number 3: The Matrix Class The goal is to use the Java constructs we've been studying: - Arrays - Control flow statements (conditionals and loops) - Class and method creation - Text output We will accomplish this goal by writing an application that can add and subtract two like matrices. class variables --------------- a) Create a 2 dimensional array (or, an array of arrays) that will store the matrix elements. It can be called m or matrix or array, whatever you like. b) numrows is an integer variable that represents the number of rows c) numcols is an integer variable that represents the number of columns class methods ------------- a)constructor - accepts 2 integer parameters, the number of rows and number of columns. The constructor will initialize each element of the array (Question what is the best construct to use in order to accomplish this?) with a random integer between zero and nine [0,9] inclusive. We have seen the Math class and its random() method. Math.random() returns a value of type double such that 0.0 <= value < 1.0. To get a value that is a desired integer, we could multiply that value by 10 AND force the Java compiler to convert the number into an integer using type casting. For example, if we declare double e = 0.27; int close; // then, e *= 10; // multiply by 10 close = (int)e; // type cast e to be an integer. what happens without "(int)" ? b)toString() - accepts no parameters but will return a String that represents the matrix. The String object will be a row by row representation of the matrix. Here's a 3x3 example [9, 5, 5] [3, 4, 8] [4, 0, 4] There is a really cool advantage to using the toString() method in the Java world; it is used automatically by Java's printing methods to convert an object being printed into a String object. For example, given a matrix object trix, you could use: System.out.print(trix); //or... System.out.printf("%s",trix); //... the printf equivalent c)add() - accepts one parameter of type Matrix and returns a boolean value. This method will return false if the dimensions of the Matrix object being passed as a parameter are NOT the same as the array of this class. return true if the dimensions of the Matrix object being passes as a parameter are the same as the array of this class AND add the corresponding elements of each matrix object into the class array. // array[0][0] += parmMatrix[0][0]; etc... d)subtract() - same specification as add(), except the method will subtract the corresponding elements of each Matrix. e)main() - public static void main(String[] args) {} in order to run the program. Rather than run the program by creating a couple of matrixes with different dimensions, we can start the program by passing in the number of rows and the number of columns. These are the "args." The two first arguments in the args array should be the number of rows and columns of the matrices to be created. Since they are off type String, you can use the Integer.parseInt(String) method to convert the String arguments into values of type int. Additional note 1: If you run the program from the command line you'd write: java Matrix 2 3 Additional note 2: If you run the program from BlueJ, you need to specify the arguments array as {"2", "3"} as they need to be String types. What else goes in main()? We could create another FunctionTester class and use it to carry out tests. For this lab, we will put all the ingredients into main(). Specifically, 1) Print a welcome message, "Keanu Reeves: cmpu 102 Matrix Lab" but using your name instead. 2) Create two matrix objects, say neo and trinity, of the specified size, initialize them with random integers and print them out... matrix neo: [2, 0, 0] [4, 7, 7] matrix trinity: [8, 8, 8] [4, 6, 6] 3) Add matrix trinity to matrix neo, and print the results... neo + trinity: [10, 8, 8] [8, 13, 13] 4) Subtract trinity from neo and print the results. Since we are doing (neo + trinity) - trinity the result should be the original matrix neo... (neo + trinity) - trinity: [2, 0, 0] [4, 7, 7] Once the program is working as specified, please call a coach or professor to check your work and give you credit. Then, submit Matrix.java to my email. (No need to send a tar file.)