You must implement a Sparse Matrix as described in class. Values stored in the matrix should be doubles. You must implement the following methods:
public class SparseMatrix { public SparseMatrix(int numRows, int numColumns) { // create a new, empty sparse matrix } public double get(int row, int column) { // return the value at the specified row and column } public void set(double value, int row, int column) { // set the value of the matrix at the provided position // if value is not zero you should add/edit a Node // if value is zero then you must delete the node // (and any row/column dummies that are no longer needed) } public String toString() { // return the matrix as a printable, multi-line String } }
Matrix multiplication is a common operation. You must implement the following two meth- ods:
public void multiplyBy(SparseMatrix other){ // multiply this matrix by other. } public void multiplyBy(double[][] other) { // multiply this sparse matrix by a normal 2D array matrix }
You must implement a main method that will create 2 sparse matrices interactively and multiply them together, printing the result of every operation. Interactive input must by done using the command-line; do NOT use JOptionPane.
First, main must ask the user to enter the size of the matrix A. Create two sparse matrices (A and B). B is the same size as A, but with the rows and columns reversed. So if A is 4 rows and 10 columns, B is 10 rows and 4 columns.
Then main will ask the user to set a value in either A or B. If the user types in ”A 1 3 4” then set A[1][3] = 4. Likewise entering ”B 2 1 8” sets B[2][1] = 8. This loop continues until the user enters the string ”done”.
After every operation you must print out both matrices. Finally, after the user as entered ”done” you must call ”A.multiplyBy(B)” and print the result of the multiplication.