CSE 131 Module 7: Array Structures

Lab


Update

Update your repository as usual. You should see a lab7 package.


Project: Matrix

Implement a Matrix class with the following features. Add JUnit tests as necessary to MatrixTest to test thoroughly all of your methods.
  1. A constructor that takes a two-dimensional array of type double and saves it (as an instance variable) as the values of the matrix.
    To be safe, your instance variable should be a copy of the parameter, so that the contents of your Matrix's array cannot be changed beyond your control.

    To copy the two-dimensional array, you must instantiate a new two-dimensional array and copy the original array's contents into your new array.

  2. An arraysAreEqual(double[][] one, double[][] two) method that compares the two arrays to see if they are the same. The two arrays are the same if:
    The .equals(Object) method included with this lab calls your arraysAreEqual method, so that Matrix equality of two matrices depends on the contents of those matrices.

    Until this method is working, the rest of the JUnit tests will not work properly.

  3. A toString method that neatly shows the contents of the matrix. In your string use "\n" to insert a line break, and use "\t" where you want a tab between elements. Include JUnit tests that print these strings to the console (using System.out.println) for visual inspection, in addition to any automated tests you perform. Your toString method will come in handy for debugging the other methods.

  4. A scaleRow method that takes a row number (an integer) and a scale factor (a double), and multiplies that row by the given scale factor. This method does not return anything. It just modifies the matrix.
    In this lab, rows are numbered as arrays are indexed. Thus, the top row in the matrix is row 0, and the bottom row is numbered one less than the number of rows in the matrix.

  5. An addRows method that takes two row numbers as parameters, and adds the two rows together, storing the result in place of the second of the two rows.

  6. A plus method that takes another Matrix as its parameter and returns the matrix that results from adding this matrix by the given one. Matrix addition is only valid when the two matrices are the same size in both dimensions, so your plus method should throw an IllegalArgumentException when this is not the case.

  7. A transpose method that takes no parameters and returns a new matrix that is the transpose of this one. Recall that the columns of the transposed matrix have the same values as the rows of the original matrix.

  8. A times method that takes another Matrix as its parameter and returns the matrix that results from multiplying this matrix by the given one. Recall that when you multiply two matrices A and B, the value in the cell at row r and column c of the resulting matrix is the dot product of A's row r with B's column c. Also recall that matrix multiplication is only valid when the number of columns of the first matrix is equal to the number of rows of the second, so your times method should throw an IllegalArgumentException when this is not the case.

Submitting your work (read carefully)



Last modified 22:14:03 CST 11 November 2009 by Ron K. Cytron