Skip to main content Link Menu Expand (external link) Document Search Copy Copied

FRQ 3 • 14 min read

Description

Final FRQ 3

b


3

Assignment name: 2015 Practice Exam FRQ: Question 3: Part a: getValueAt

a.

Answer:

public int getValueAt(int row, int col) {
    for (SparseArrayEntry entry : entries) {
        if (entry.getRow() == row && entry.getCol() == col) {
            return entry.getValue();
        }
    }
    return 0;  // return 0 if no entry found at the specified row and column
}


b.

public void removeColumn(int col) {
    // create an iterator for the 'entries' collection
    Iterator<SparseArrayEntry> iterator = entries.iterator();

    // iterate over each entry in the 'entries' collection
    while (iterator.hasNext()) {
        // get the current entry
        SparseArrayEntry entry = iterator.next();

        // if the column of the current entry matches the column to be removed
        if (entry.getCol() == col) {
            iterator.remove();
        } 
        // if the column of the current entry is greater than the column to be removed
        else if (entry.getCol() > col) {
            // create a new entry with the same row and value as the current entry,
            // but with the column decremented by 1
            int newRow = entry.getRow();
            int newCol = entry.getCol() - 1;
            int newValue = entry.getValue();

            iterator.remove();

            // add the new entry to the 'entries' collection
            entries.add(new SparseArrayEntry(newRow, newCol, newValue));
        }
    }

    numCols--;
}

import java.util.List;

public class YourSparseArrayClass {

    private List<SparseArrayEntry> entries;
    private int numCols;

    // Constructor
    public YourSparseArrayClass() {
        this.entries = new ArrayList<>();
        // Initialize other properties as needed
    }

    // Other methods and constructor...

    public void removeColumn(int col) {
        // create an iterator for the 'entries' collection
        Iterator<SparseArrayEntry> iterator = entries.iterator();

        // iterate over each entry in the 'entries' collection
        while (iterator.hasNext()) {
            // get the current entry
            SparseArrayEntry entry = iterator.next();

            // if the column of the current entry matches the column to be removed
            if (entry.getCol() == col) {
                iterator.remove();
            } 
            // if the column of the current entry is greater than the column to be removed
            else if (entry.getCol() > col) {
                // create a new entry with the same row and value as the current entry,
                // but with the column decremented by 1
                int newRow = entry.getRow();
                int newCol = entry.getCol() - 1;
                int newValue = entry.getValue();

                iterator.remove();

                // add the new entry to the 'entries' collection
                entries.add(new SparseArrayEntry(newRow, newCol, newValue));
            }
        }

        numCols--;
    }

    public static void main(String[] args) {
        YourSparseArrayClass sparseArray = new YourSparseArrayClass();

        // Add some entries to the sparse array
        sparseArray.entries.add(new SparseArrayEntry(0, 0, 1));
        sparseArray.entries.add(new SparseArrayEntry(0, 1, 2));
        sparseArray.entries.add(new SparseArrayEntry(1, 0, 3));
        sparseArray.entries.add(new SparseArrayEntry(1, 1, 4));

        // Display the entries before removing a column
        System.out.println("Entries before removing a column:");
        sparseArray.entries.forEach(entry -> {
            System.out.println("Row: " + entry.getRow() + ", Col: " + entry.getCol() + ", Value: " + entry.getValue());
        });

        // Remove a column
        sparseArray.removeColumn(1);

        // Display the entries after removing a column
        System.out.println("\nEntries after removing a column:");
        sparseArray.entries.forEach(entry -> {
            System.out.println("Row: " + entry.getRow() + ", Col: " + entry.getCol() + ", Value: " + entry.getValue());
        });
    }
}

class SparseArrayEntry {
    private int row;
    private int col;
    private int value;

    public SparseArrayEntry(int row, int col, int value) {
        this.row = row;
        this.col = col;
        this.value = value;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    public int getValue() {
        return value;
    }
}

|   

|   

|   public class YourSparseArrayClass {

|   

|       private List<SparseArrayEntry> entries;

|       private int numCols;

|   

|       // Constructor

|       public YourSparseArrayClass() {

|           this.entries = new ArrayList<>();

|           // Initialize other properties as needed

|       }

|   

|       // Other methods and constructor...

|   

|       public void removeColumn(int col) {

|           // create an iterator for the 'entries' collection

|           Iterator<SparseArrayEntry> iterator = entries.iterator();

|   

|           // iterate over each entry in the 'entries' collection

|           while (iterator.hasNext()) {

|               // get the current entry

|               SparseArrayEntry entry = iterator.next();

|   

|               // if the column of the current entry matches the column to be removed

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

|                   iterator.remove();

|               } 

|               // if the column of the current entry is greater than the column to be removed

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

|                   // create a new entry with the same row and value as the current entry,

|                   // but with the column decremented by 1

|                   int newRow = entry.getRow();

|                   int newCol = entry.getCol() - 1;

|                   int newValue = entry.getValue();

|   

|                   iterator.remove();

|   

|                   // add the new entry to the 'entries' collection

|                   entries.add(new SparseArrayEntry(newRow, newCol, newValue));

|               }

|           }

|   

|           numCols--;

|       }

|   

|       public static void main(String[] args) {

|           YourSparseArrayClass sparseArray = new YourSparseArrayClass();

|   

|           // Add some entries to the sparse array

|           sparseArray.entries.add(new SparseArrayEntry(0, 0, 1));

|           sparseArray.entries.add(new SparseArrayEntry(0, 1, 2));

|           sparseArray.entries.add(new SparseArrayEntry(1, 0, 3));

|           sparseArray.entries.add(new SparseArrayEntry(1, 1, 4));

|   

|           // Display the entries before removing a column

|           System.out.println("Entries before removing a column:");

|           sparseArray.entries.forEach(entry -> {

|               System.out.println("Row: " + entry.getRow() + ", Col: " + entry.getCol() + ", Value: " + entry.getValue());

|           });

|   

|           // Remove a column

|           sparseArray.removeColumn(1);

|   

|           // Display the entries after removing a column

|           System.out.println("\nEntries after removing a column:");

|           sparseArray.entries.forEach(entry -> {

|               System.out.println("Row: " + entry.getRow() + ", Col: " + entry.getCol() + ", Value: " + entry.getValue());

|           });

|       }

|   }

Unresolved dependencies:

   - class SparseArrayEntry