Question: cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that
cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i = 0; i < matrix.length; i++) { output += matrix[i]; if (i != matrix.length - 1) { output += ", "; } } return output + " ]"; } public Matrix add(Matrix om) { Matrix newMatrix = null; if(om.matrix[0].length == this.matrix[0].length) { newMatrix = new Matrix(new int[matrix.length][matrix.length]); for (int i = 0; i < om.matrix[0].length; i++) for (int j = 0; j < om.matrix[0].length; j++) newMatrix.matrix[i][j] = om.matrix[i][j] + this.matrix[i][j]; } return newMatrix; } public Matrix sub(Matrix om) { { Matrix newMatrix = null; if(om.matrix[0].length == this.matrix[0].length) { newMatrix.matrix = new int[matrix.length][matrix.length]; for (int i = 0; i < om.matrix[0].length; i++) for (int j = 0; j < om.matrix[0].length; j++) newMatrix.matrix[i][j] = om.matrix[i][j] - this.matrix[i][j]; } return newMatrix; } } public Matrix multi(Matrix om) { return null; } public Matrix scalar(Matrix om) { return null; } public boolean equal(Matrix om) { return false; } public Matrix transpose(Matrix om) { return null; } class Dimension{ private int row, column; public Dimension(int[][] a){ row = a.length; column = a[0].length; } public int getRow() { return row; } public int getColumn() { return column; } public boolean equals(Dimension d) { return d.row == this.row && d.column == this.column; } } in App class public class Lab1App { public static void main(String[] args) { Matrix a = new Matrix(new int [][] {{1,2},{5,3}}); Matrix b = new Matrix(new int [][] {{3,2},{4,5}}); System.out.println(a.add(b)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
