Description
Java HandWritten Matrix Lab
Create a class IntMatrix with:
- constructor with parameters for #rows, #cols
- set method to set a single cell (three params). Print out an error if row or column is invalid.
- numCols and numRows methods that return #rows and #cols.
- sum method that returns sum of all cells in matrix.
- rowSum method that returns the sum of a particular row in the matrix
- min method that returns the minimum of all cells.
- toString that prints out as a matrix (rows and cols)
- equals method
- extra credit: addRows and addCols methods that dynamically resize the matrix.
Provide a main that tests all methods, see example below:
IntMatrix matrix = new IntMatrix(2,3);
matrix.set(0,0,99);
matrix.set(1,1,88);
matrix.set(5,3,77); // should print out error
System.out.println("matrix is: "+matrix);
System.out.println("matrix numRows is"+matrix.numRows());
System.out.println("matrix sum is"+matrix.sum());
System.out.println("matrix sum of 2nd row is"+matrix.rowSum(1));
System.out.println("matrix min is"+matrix.min());
IntMatrix matrix2= new IntMatrix(2,3);
matrix2.set(0,0,99);
matrix2.set(1,1,88);
IntMatrix matrix3= new IntMatrix(2,3);
matrix3.set(0,0,44);
matrix3.set(0,1,22);
if (matrix.equals(matrix2))
System.out.println("matrix equals matrix2"); // should print
if (matrix.equals(matrix3))
System.out.println("matrix equals matrix3"); // should not print
Explanation & Answer
The soluti...
Review
Review
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!