import java.io.PrintStream; import java.util.Scanner; /** * A class to manage a matrix of double values */ public class Matrix { /** * Reference to the actual matrix of doubles, * store in dynamic memory segment (heap), * null if this is an invalid matrix */ private double matrix[][] = null; /** * Default constructor */ public Matrix() { // Keeps this.matrix reference null, // therefore, it is an invalid matrix } /** * Constructs an null matrix of the given number of rows and columns * @param rows Number of rows. Must be positive. * @param columns Number of columns. Must be positive */ public Matrix(int rows, int columns) { // Check for valid dimensions if ( rows > 0 && columns > 0 ) { // Dimensions are valid, create the actual matrix this.matrix = new double[rows][columns]; } } /** * Read the dimensions and values from the given input * @param matrix */ public void read(Scanner input) { // Read the dimensions of the matrix int rows = input.nextInt(); int cols = input.nextInt(); // Create the matrix in heap, and keep a reference this.matrix = new double[rows][cols]; // Read all values for the matrix for ( int row = 0; row < this.matrix.length; ++row ) { // Read all values for current row for ( int col = 0; col < this.matrix[row].length; ++col ) { this.matrix[row][col] = input.nextDouble(); } } } /** * Print this matrix in the given output * @param output The file where print this matrix */ public void print(PrintStream output) { // Print each row for ( int row = 0; row < matrix.length; ++row ) { // Print each column in the current row for ( int col = 0; col < matrix[row].length; ++col ) { // Print the cell value output.printf("%.2f ", matrix[row][col]); } // Separate this row from next by a new line character output.println(); } } /** * Adds this matrix with another * * @param other Other matrix to be added with this * @return A reference to the resulting matrix object containing * the sum of this and the other matrix. If matrixes have different * dimensions, a null reference will be retorned */ public Matrix add(Matrix other) { // The resulting matrix of adding this one with other Matrix result = null; // Only matrixes of same size can be added if ( this.matchesSize(other) ) { // Create the matrix object to store the resulting sum result = new Matrix( this.getRowCount(), this.getColumnCount() ); // Add the rows of both matrixes. Because the three matrixes // (this, other, and result) have the same dimensions, we can use // the dimensions of anyone to control the loops for ( int row = 0; row < this.matrix.length; ++row ) { // Add all columns in this row for ( int col = 0; col < this.matrix[row].length; ++col ) { // The result cell is the sum of respectives cells in // this and other matrixes result.matrix[row][col] = this.matrix[row][col] + other.matrix[row][col]; } } } // Notice that we return null if matrixes do not match their size return result; } /** * Returns true if this matrix and other have the same amount of * rows and columns * * @param other Other matrix to be checked against this for size * @return true if this matrix and other have the same dimensions */ public boolean matchesSize(Matrix other) { return this.getRowCount() == other.getRowCount() && this.getColumnCount() == other.getColumnCount(); } /** * Return the number of rows that this matrix has * @return The number of rows, 0 if this is an invalid matrix */ public int getRowCount() { return this.matrix != null ? this.matrix.length : 0; } /** * Return the number of columns that this matrix has * @return The number of rows, 0 if this is an invalid matrix */ public int getColumnCount() { // Avoid to check lengths for null matrix references // We assume all rows have the same amount of columns, // therefore, we use the first row length return this.matrix != null && this.matrix.length > 0 ? this.matrix[0].length : 0; } }