A two-dimensional array of integers in which most elements are zero is called a sparse array. Because most elements have a value of zero, memory can be saved by storing only the non-zero values along with their ro and column indexes. The following complete SparseArrayEntry class is used to represent non-zero elements in a sparse array. A SparseArrayEntry object cannot be modified after it has been constructed.

Image

The SparseArray class represents a sparse array. It contains a list of SparseArrayEntry objects, each of which represents one of the non-zero elements in the array. The entries representing the non-zero elements are stored in the list in no particular order. Each non-zero element is represented by exactly one entry in the list.

Image

The following table shows an example of a two-dimensional sparse array. Empty cells in the table indicate zero values.

Image

A)

Write the SparseArray method getValueAt. The method returns the value of the sparse array element at a given row and column in the sparse array. If the list entries contains an entry with the specified row and column, the value associated with the entry is returned. If there is no entry in entries corresponding to the specified row and column, 0 is returned. In the example above, the call sparse.getValueAt(3, 1) would return -9, and sparse.getValueAt(3, 3) would return 0.

Complete method getValueAt below. Image

public int getValueAt(int row, int col) {
    for (SparseArray i:entries ){
    if (i.getrow== row && i.getcol== col){
        return i.getValue();
    }
    else { 
        return 0;
    }
}
}
|       for (SparseArray i:entries ){

cannot find symbol

  symbol:   variable entries



|       for (SparseArray i:entries ){

cannot find symbol

  symbol:   class SparseArray



|   public int getValueAt(int row, int col) {

|       for (SparseArray i:entries ){

|       if (i.getrow== row && i.getcol== col){

|           return i.getValue();

|       }

|       else { 

|           return 0;

|       }

|   }

|   }

missing return statement

Can not show this part running, because this requires the methods from the provided class (which are written by CB in comments, not real java. Let’s assume this is correct, I will get more feedback when I peer grade)

B)

Write the SparseArray method removeColumn. After removing a specified column from a sparsearray:

Repeated Class information for mine, and peer grader’s convenience :

Image

public void removeColumn(int col){
    int x = 0; 
    while (x < entries.size()){
        if (x.getCol() == col){
            entries.remove(x);
        }
        
        else if (i.getCol() > col) {
            entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));
            i ++; 
        }
        else {
            i ++; 
        }
        numCols = numCols - 1; 
    }
}
|       while (x < entries.size()){

cannot find symbol

  symbol:   variable entries



|           if (x.getCol() == col){

int cannot be dereferenced



|               entries.remove(x);

cannot find symbol

  symbol:   variable entries



|           else if (i.getCol() > col) {

cannot find symbol

  symbol:   variable i



|               entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));

cannot find symbol

  symbol:   variable i



|               entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));

cannot find symbol

  symbol:   class SparseArrayEntry



|               entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));

cannot find symbol

  symbol:   variable i



|               entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));

cannot find symbol

  symbol:   variable i



|               entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));

cannot find symbol

  symbol:   variable i



|               entries.set(i, new SparseArrayEntry(i.getRow(), i.getCol() -1, i.getValue));

cannot find symbol

  symbol:   variable entries



|               i ++; 

cannot find symbol

  symbol:   variable i



|               i ++; 

cannot find symbol

  symbol:   variable i



|           numCols = numCols - 1; 

cannot find symbol

  symbol:   variable numCols



|           numCols = numCols - 1; 

cannot find symbol

  symbol:   variable numCols

I was a little confused on the second bullet, the else if . Are we replacing the new col with the contents of the one before it? Are we deleting the one before it so is the new col just replacing the old one? Or will the duplicates exist?