15 min read

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.

Part 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.

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;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    public int getValue() {
        return value;
    }
}

public class SparseArray {
    private int numRows;
    private int numCols;
    
    private List<SparseArrayEntry> entries;
    public SparseArray(){ 
        entries = new ArrayList<SparseArrayEntry>(); 
    }
    public int getNumRows(){ 
        return numRows; 
    }
    public int getNumCols(){ 
        return numCols; 
    }
    public void setNumRows(int numRows) {
        this.numRows = numRows;
    }
    
    public void setNumCols(int numCols) {
        this.numCols = numCols;
    }
    
    public void addEntry(SparseArrayEntry entry) {
        entries.add(entry);
    }
    // Part A
    // Needed help here
    public int getValueAt(int row, int col)
    {
        for(SparseArrayEntry entry : entries)
            if(entry.getRow() == row && entry.getCol() == col)
                return entry.getValue();
    
        return 0;
    }
    // Part B
    // Needed help here
    public void removeColumn(int col) {
        for(int i = entries.size() - 1; i >= 0; i--) {
            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()));
        }
        numCols--;
    }
    
    public static void main(String[] args) {
        SparseArray sparse = new SparseArray();

        sparse.setNumRows(4);
        sparse.setNumCols(4);

        sparse.addEntry(
            new SparseArrayEntry(0, 1, 1)
        );
        sparse.addEntry(
            new SparseArrayEntry(1, 2, 2)
        );
        sparse.addEntry(
            new SparseArrayEntry(2, 3, 3)
        );
        sparse.addEntry(
            new SparseArrayEntry(3, 1, -9)
        );

        System.out.println("Value at (3,1) before removing column: " + sparse.getValueAt(3, 1));
        System.out.println("Value at (2,3) before removing column: " + sparse.getValueAt(2, 3));

        System.out.println("Number of columns before removing one: " + sparse.getNumCols());
        sparse.removeColumn(1);

        System.out.println("Value at (3,0) after removing column: " + sparse.getValueAt(3, 0));
        System.out.println("Value at (2,2) after removing column: " + sparse.getValueAt(2, 2));

        System.out.println("Number of columns after removing one: " + sparse.getNumCols());
    }
} 

SparseArray.main(null)
Value at (3,1) before removing column: -9
Value at (2,3) before removing column: 3
Number of columns before removing one: 4
Value at (3,0) after removing column: 0
Value at (2,2) after removing column: 3
Number of columns after removing one: 3

Summary

In my opinion, this was the hardest FRQ out of the 4, and I really had no idea what was going on. I’m not sure if that is the general consensus, and if #3 was considered the hardest, but it was for me, this is a topic that I need to review before the exam, and possibly find questions similar to this one. For this question, I had to ask a lot of questions in order to help me fully understand what was going on. There was just so much initializing code, and once I understood that I tried to start part a, but it was hard. I referred to this Computer Science Professor who had done this problem and he helped me to understand what was really going and explained why he used the for loop. Also I think that after looking at the official Scoring Guidelines, their answer is much more complicated that what was done here, but I hope I would still get 3/3 for this. I think that the key topic was about Arrays again and manipulating them, and this specific question was focused on manipulating different elements of the array. I think that this algorithm used relates to the FRQ because it shows us how we can access different elements of the list in order to find out how the problem can be solved.

Stats: Time: Over 90 minutes

Date Completed: February 25th, 2023

CollegeBoard Scoring: 5/9

Personal Ranking: 4

Scoring

This question was worth 9 points

part a:

  • +1 for Accesses all necessary elements of entries
  • +1 for Identifies elements of entries at row index col, if exists
  • +1 Returns identified value or returns 0 if no entry exists in entries with row index row and column index col

part b:

  • +1 for Initializes instance variable within constructor using parameter
  • +6 for getHint
    • +1 Accesses all letters in both guess and hidden word in loop
    • +4 Process letters within loop
      • +1 Extracts and compares corresponding single letters from guess and hidden word
      • +1 Tests whether guess letter occurs in same position in both guess and hidden word
      • +1 Tests whether guess letter occurs in hidden word but not in same position as in guess
      • +1 Adds correct character exactly once to the hint string based on the test result
    • +1 Declares, initializes, and returns constructed hint string

question specific penalties:

  • -0 Uses get to access letters from strings
  • -0 Consistently uses incorrect name instead of instance variable name for hidden word