Question 3 - 2D Arrays

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

import java.util.ArrayList;

class SeatScoreEntry {
    private int row;
    private int column;
    private int score;

    //this is a constructor to start SeatScoreEntry
    public SeatScoreEntry(int row, int column, int score) {
        this.row = row;
        this.column = column;
        this.score = score;
    }
    //setters and getters for rows
    public int getRow() {
        return row;
    }
//setters and getters for column
    public int getColumn() {
        return column;
    }
//setters and getters for score
    public int getScore() {
        return score;
    }
}

class ClassroomScores {
    private ArrayList<SeatScoreEntry> scores;
    //new constructor for classroom scores
    public ClassroomScores() {
        scores = new ArrayList<>();
    }
    //this is how a score is added
    public void addScore(SeatScoreEntry scoreEntry) {
        scores.add(scoreEntry);
    }
     
    // Method to get the score at a specific seat
    public int getScoreAt(int row, int column) {
        for (SeatScoreEntry entry : scores) {
            if (entry.getRow() == row && entry.getColumn() == column) {
                return entry.getScore();
            }
        }
        return 0; //this is for if no score is found for the seat
    }
}

public class Main {
    public static void main(String[] args) {
        ClassroomScores classroom = new ClassroomScores();
//adding score entries to the classroom
        classroom.addScore(new SeatScoreEntry(1, 1, 85));
        classroom.addScore(new SeatScoreEntry(2, 3, 90));
        classroom.addScore(new SeatScoreEntry(4, 2, 75));
//scores and seats being printed
        System.out.println("Score at seat (1, 1): " + classroom.getScoreAt(1, 1)); // Output: 85
        System.out.println("Score at seat (2, 3): " + classroom.getScoreAt(2, 3)); // Output: 90
        System.out.println("Score at seat (4, 2): " + classroom.getScoreAt(4, 2)); // Output: 75
        System.out.println("Score at seat (3, 3): " + classroom.getScoreAt(3, 3)); // Output: 0 (Absent or empty seat)
    }
}

Main.main(null);
Score at seat (1, 1): 85
Score at seat (2, 3): 90
Score at seat (4, 2): 75
Score at seat (3, 3): 0

Part A Explanation

I decided to focus on a different example other than poker. I used a classroom setting to show the use of a sparse array. Each SeatScoreEntry represents a student’s score in a specific seat, and the ClassroomScores class manages these entries. The getScoreAt method retrieves the score for a given seat, returning 0 if the seat is empty or the student was absent, demonstrating a practical application of sparse arrays in managing classroom data.

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.

import java.util.ArrayList;
import java.util.Iterator;

class GardenPlotEntry {
    private int row;
    private int col;
    private int plants;
//constructor 
    public GardenPlotEntry(int row, int col, int plants) {
        this.row = row;
        this.col = col;
        this.plants = plants;
    }
//setters and getters for row, col, and copies
    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    public int getPlants() {
        return plants;
    }
}
//garden and plots
class Garden {
    //array for all garden plots stored
    private ArrayList<GardenPlotEntry> plots;
    private int numRows;
    private int numCols;

    public Garden(int numRows, int numCols) {
        plots = new ArrayList<>();
        this.numRows = numRows;
        this.numCols = numCols;
    }
//add a new plot to garden
    public void addPlot(GardenPlotEntry plot) {
        plots.add(plot);
    }

    public void removeColumn(int col) {
        Iterator<GardenPlotEntry> iterator = plots.iterator();
        while (iterator.hasNext()) {
            GardenPlotEntry plot = iterator.next();
            if (plot.getCol() == col) {
                iterator.remove();
            } else if (plot.getCol() > col) {
                // Update the column index for plots to the right of the removed column
                plot = new GardenPlotEntry(plot.getRow(), plot.getCol() - 1, plot.getPlants());
            }
        }
        numCols--; // Adjust the total number of columns in the garden
    }

    public int getPlantsAt(int row, int col) {
        for (GardenPlotEntry plot : plots) {
            if (plot.getRow() == row && plot.getCol() == col) {
                return plot.getPlants();
            }
        }
        return 0; // Return 0 if no plants are found at the specified location
    }

    public int getNumRows() {
        return numRows;
    }

    public int getNumCols() {
        return numCols;
    }
}

public class Main {
    public static void main(String[] args) {
        //create a new garden 3x3
        Garden garden = new Garden(3, 3);

        // Adding some plots with plants
        garden.addPlot(new GardenPlotEntry(1, 1, 10));
        garden.addPlot(new GardenPlotEntry(1, 2, 15));
        garden.addPlot(new GardenPlotEntry(2, 2, 5));
        garden.addPlot(new GardenPlotEntry(3, 1, 20));
        garden.addPlot(new GardenPlotEntry(3, 3, 8));

        // Removing column 2 from the garden
        garden.removeColumn(2);

        // Testing the getPlantsAt method
        System.out.println("Plants at (1, 1): " + garden.getPlantsAt(1, 1)); // Output: 10
        System.out.println("Plants at (1, 2): " + garden.getPlantsAt(1, 2)); // Output: 0 (removed)
        System.out.println("Plants at (2, 2): " + garden.getPlantsAt(2, 2)); // Output: 5
        System.out.println("Plants at (3, 1): " + garden.getPlantsAt(3, 1)); // Output: 20
        System.out.println("Plants at (3, 3): " + garden.getPlantsAt(3, 3)); // Output: 8

        System.out.println("NumRows: " + garden.getNumRows()); // Output: 3
        System.out.println("NumCols: " + garden.getNumCols()); // Output: 2
    }
}

Main.main(null);
Plants at (1, 1): 10
Plants at (1, 2): 0
Plants at (2, 2): 0
Plants at (3, 1): 20
Plants at (3, 3): 8
NumRows: 3
NumCols: 2

Part B Explanation:

This version reimagines the SparseArray as a Garden class, managing a sparse array of GardenPlotEntry objects that represent the number of plants in each plot of a garden grid. The removeColumn method simulates the effect of removing a garden column, including updating the positions of the remaining plots and adjusting the garden’s column count. This example demonstrates how the concept of sparse arrays can be applied to different scenarios beyond the original classroom or array-based contexts.