Question

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Notes:

Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.

In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

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 row 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

The sample array can be represented by a SparseArray object, sparse, with the following instance variable values. The items in entries are in no particular order; one possible ordering is shown below.

image

Part A

(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 class SparseArrayEntry {
    private int row;
    private int col;
    private int value;
    
    public SparseArrayEntry(int r, int c, int v) {
        row = r;
        col = c;
        value = v;
    }
    
    // getters to get row, col, and value of entries
    public int getRow() { 
        return row; 
    }
    public int getCol() { 
        return col; 
    }
    public int getValue() { 
        return value; 
    }
}

public class SparseArray {
    private List<SparseArrayEntry> entries;
    public SparseArray() { 
        entries = new ArrayList<SparseArrayEntry>(); 
    }

    public void addEntry(SparseArrayEntry entry) {
        entries.add(entry); // add SparseArrayEntry to entries 
    }
    
    public int getValueAt(int row, int col) { 
        for (int i = 0; i < entries.size(); i++) { // iterate to get the row and col
            SparseArrayEntry entry = entries.get(i);
            int rowNumber = entry.getRow();
            int colNumber = entry.getCol();

            if (rowNumber == row && colNumber == col) { // check if entry matches row and col
                return entry.getValue();
            }
        }
        return 0; // if no entry is found
    }

    public static void main(String args[]) {
        SparseArray sparseArray = new SparseArray();
    
        sparseArray.addEntry(new SparseArrayEntry(1, 4, 4));
        sparseArray.addEntry(new SparseArrayEntry(2, 0, 1));
        sparseArray.addEntry(new SparseArrayEntry(3, 1, -9));
        sparseArray.addEntry(new SparseArrayEntry(1, 1, 5));

        System.out.println("Value at (3, 1): " + sparseArray.getValueAt(3, 1));
        System.out.println("Value at (3, 3): " + sparseArray.getValueAt(3, 3));
    }
    
}

SparseArray.main(null);

Value at (3, 1): -9
Value at (3, 3): 0

Part B

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

  • All entries in the list entries with column indexes matching col are removed from the list.
  • All entries in the list entries with column indexes greater than col are replaced by entries with column indexes that are decremented by one (moved one column to the left).
  • The number of columns in the sparse array is adjusted to reflect the column removed.

The sample object sparse from the beginning of the question is repeated for your convenience.

image

The shaded entries in entries, below, correspond to the shaded column above.

image

When sparse has the state shown above, the call sparse.removeColumn(1) could result insparse having the following values in its instance variables (since entries is in no particular order, itwould be equally valid to reverse the order of its two items). The shaded areas below show the changes.

image

image

image

public class SparseArrayEntry {
    private int row;
    private int col;
    private int value;
    
    public SparseArrayEntry(int r, int c, int v) {
        row = r;
        col = c;
        value = v;
    }
    
    // getters to get row, col, and value of entries
    public int getRow() { 
        return row; 
    }
    public int getCol() { 
        return col; 
    }
    public int getValue() { 
        return value; 
    }
}

public class SparseArray {
    private List<SparseArrayEntry> entries;
    private int numCols; // added declaration for numCols

    public SparseArray() { 
        entries = new ArrayList<SparseArrayEntry>(); 
        numCols = 0; // initialize numCols
    }

    public void addEntry(SparseArrayEntry entry) {
        entries.add(entry); // add SparseArrayEntry to entries 
        if (entry.getCol() >= numCols) {
            numCols = entry.getCol() + 1; // update numCols if needed
        }
    }
    
    public int getValueAt(int row, int col) { 
        for (int i = 0; i < entries.size(); i++) { // iterate to get the row and col
            SparseArrayEntry entry = entries.get(i);
            int rowNumber = entry.getRow();
            int colNumber = entry.getCol();

            if (rowNumber == row && colNumber == col) { // check if entry matches row and col
                return entry.getValue();
            }
        }
        return 0; // if no entry is found
    }

    public void removeColumn(int col) {
        int i = 0;

        while (i < entries.size()){
            SparseArrayEntry entry = entries.get(i);
            if (entry.getCol() == col)
            {
                entries.remove(i);
            }
            else if (entry.getCol() > col)
            {
                entries.set(i, new SparseArrayEntry(entry.getRow(), entry.getCol() - 1, entry.getValue()));
                i++;
            }
            else
            {
                i++;
            }
        }
        numCols--; // decrease numCols after removing a column
    }

    public static void main(String args[]) {
        SparseArray sparseArray = new SparseArray();
    
        sparseArray.addEntry(new SparseArrayEntry(1, 4, 4));
        sparseArray.addEntry(new SparseArrayEntry(2, 0, 1));
        sparseArray.addEntry(new SparseArrayEntry(3, 1, -9));
        sparseArray.addEntry(new SparseArrayEntry(1, 1, 5));

        System.out.println("Before:");
        printSparseArray(sparseArray);
    
        sparseArray.removeColumn(1);
    
        System.out.println("\nAfter:");
        printSparseArray(sparseArray);
    }
    
    private static void printSparseArray(SparseArray sparseArray) {
        for (int i = 0; i < sparseArray.numCols; i++) {
            for (int j = 0; j < sparseArray.numCols; j++) {
                System.out.print(sparseArray.getValueAt(i, j) + " ");
            }
            System.out.println();
        }
    }    
}

SparseArray.main(null);

Before:
0 0 0 0 0 
0 5 0 0 4 
1 0 0 0 0 
0 -9 0 0 0 
0 0 0 0 0 

After:
0 0 0 0 
0 0 0 4 
1 0 0 0 
0 0 0 0