/** * This class represents a square matrix. The class invariant for this class is that the internal matrix * should always have the same number of rows as columns. * @author max * */ public class SquareMatrix { private Double[][] matrix; private int numRows; private int numCols; /* Test cases go here! */ public static void main(String[] args) { } /** Fill in the following: @Precondition: @Postcondition: */ public SquareMatrix(Double[][] matrix, int numRows, int numCols) { this.matrix = new Double[matrix.length][matrix[0].length]; // Initialize our array this.numCols = numCols; this.numRows = numRows; for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { this.matrix[i][j] = new Double(matrix[i][j]); // Copy each element to avoid aliasing } } } /** Fill in the following: @Precondition: @Postcondition: */ public SquareMatrix multiply(Double[][] otherMatrix) { Double[][] newMatrix = new Double[numRows][numCols]; for(int i = 0; i < numRows; i++) { for(int j = 0; j < numCols; j++) { newMatrix[i][j] = new Double(0); for(int k = 0; k < numCols; k++) { newMatrix[i][j] = new Double(newMatrix[i][j].doubleValue() + (matrix[i][k].doubleValue() * otherMatrix[k][j].doubleValue())); } } } return new SquareMatrix(newMatrix, numRows, numCols); } // These methods function as expected. public Double get(int row, int col) { return matrix[row][col]; } public int getRows() { return numRows; } public int getCols() { return numCols; } public String toString() { String s = ""; for(int i = 0; i < numRows; i++) { for(int j = 0; j < numCols; j++) { s += matrix[i][j] + " "; } s+="\n"; } return s; } }