Question: PLEASE HELP DEBUGGING- Every method works except for my addMatrices and multiplyMatrices methods.. I have zero idea where to go from here. I am trying

PLEASE HELP DEBUGGING- Every method works except for my addMatrices and multiplyMatrices methods.. I have zero idea where to go from here. I am trying to replace the linknode class to use my datastructure. After my code is the file SparseInterface PLEASE HELP DEBUGGING- Every method works except for my addMatrices and

import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.jar.Manifest;

public class SparseMatrix implements SparseInterface{ private int numRows; private int numCols; private List sparseMatrix; //constructor public SparseMatrix(){ setSize(5,5); } public SparseMatrix(int numRows,int numCols){ this.numRows=numRows; this.numCols=numCols; sparseMatrix=new LinkedList(); } public void clear(){ sparseMatrix.clear(); } public void setSize(int numRows , int numCols){ this.numRows=numRows; this.numCols=numCols; clear(); } public void addElement(int row,int col,int data){ if(row>= getNumRows() || col>=getNumCols()){ throw new IndexOutOfBoundsException(); } removeElement(row,col); sparseMatrix.add(new MatrixNode(row,col,data)); } public void removeElement(int row,int col){ if(row>=getNumRows() || col >= getNumCols()){ throw new IndexOutOfBoundsException(); } sparseMatrix.remove(new MatrixNode(row,col,0)); } public int getElement(int row,int col){ if(row>=getNumRows() || col >= getNumCols()){ throw new IndexOutOfBoundsException(); } int pos = sparseMatrix.indexOf(new MatrixNode(row,col,0)); if(pos ==-1){ throw new IllegalArgumentException("Element not found!"); } int value = ((sparseMatrix.get(pos)).data); return value; } public SparseInterface addMatrices(SparseInterface matrixToAdd){ SparseMatrix result = new SparseMatrix(getNumRows(),getNumCols()); SparseMatrix newlist = (SparseMatrix) matrixToAdd;

LinkNode list1 = sparseMatrix.getHead(); LinkNode list2 = newlist.sparseMatrix.getHead(); while (list1 != null || list2 != null) { if (list2 == null) { result.addElement(list1.getRow(), list1.getCol(), list1.getData()); list1 = list1.getNext(); } else if (list1 == null) { result.addElement(list2.getRow(), list2.getCol(), list2.getData()); list2 = list2.getNext(); }

else if (list1.getRow() list2.getRow()) { result.addElement(list2.getRow(), list2.getCol(), list2.getData()); list2 = list2.getNext(); } else if (list1.getRow() == list2.getRow() && list1.getCol()

else if (list1.getRow() == list2.getRow() && list1.getCol() > list2.getCol()) { result.addElement(list2.getRow(), list2.getCol(), list2.getData()); list2 = list2.getNext(); } else if (list1.getRow() == list2.getRow() && list1.getCol() == list2.getCol()) { result.addElement(list1.getRow(), list1.getRow(), list1.getData() + list2.getData()); list1 = list1.getNext(); list2 = list2.getNext(); } } return result; } public SparseInterface multiplyMatrices(SparseInterface matrixToMultiply){ if (getNumCols() == matrixToMultiply.getNumRows()) { SparseMatrix newList = (SparseMatrix) matrixToMultiply; SparseMatrix result = new SparseMatrix(getNumCols(), matrixToMultiply.getNumRows()); for (int i = 0; i ((MatrixNode)o).row){ return 1; } else{ if(column((MatrixNode)o).column){ return 1; } else{ return 0; } } } } }

********

public interface SparseInterface {

//STUDENTS: DO NOT ALTER. THIS WILL BE USED TO TEST YOUR CODE

/* Should clear the matrix of all entries (make all entries 0) */ public void clear();

/* Sets size of the matrix. Number of rows, number of columns. */

public void setSize(int numRows, int numCols);

public int getNumRows(); public int getNumCols();

/* Adds an element to the row and column passed as arguments (overwrites if element is already present at that position). Throws an error if row/column combination is out of bounds. Checks to see if element has a value of zero before creating */ public void addElement(int row, int col, int data);

/* Remove (make 0) the element at the specified row and column. Throws an error if row/column combination is out of bounds. */ public void removeElement(int row, int col);

/* Return the element at the specified row and column Throws an error if row/column combination is out of bounds. */ public int getElement(int row, int col);

/* Should return the nonzero elements of your sparse matrix as a string. The String should be k lines, where k is the number of nonzero elements. Each line should be in the format "row column data" where row and column are the "coordinate" of the data and all are separated by spaces. An empty matrix should return an empty string. The print should be from left to right and from top to bottom (like reading a book) i.e. the matrix

3 0 1 0 2 0 0 0 4

Should print as: 0 0 3 0 2 1 1 1 2 2 2 4

*/ public String toString();

/*takes another matrix as input and returns the sum of the two matrices*/ /*return NULL if sizes incompatible*/ public SparseInterface addMatrices(SparseInterface matrixToAdd); /*takes another matrix as input and returns the product of the two matrices*/ /*return NULL if sizes incompatible*/ public SparseInterface multiplyMatrices(SparseInterface matrixToMultiply); }

Implement a "sparse" matrix. Only matrix elements that are non-zero should be allocated in memory (or added to the matrix). This means you cannot implement your matrix with a 2D array. Implement methods to add two matrices and multiply two matrices. If you don't remember how matrix addition or matrix multiplication work, try looking at this. https://www.mathsisfun.com/algebra/matrix-introduction.html Implement the following interface: Sparse Interface Your code will be tested with the following code: Test Code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!