Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following class: public class Table { private int[][] values; publi

ID: 3662327 • Letter: C

Question

Consider the following class:
public class Table
{
private int[][] values;
public Table(int rows, int columns) { values = new int[rows][columns]; }
public void set(int i, int j, int n) { values[i][j] = n; }
}
Add a method that computes the average of the neighbors of a table element in the
eight directions shown in Figure 15.
public double neighborAverage(int row, int column)
However, if the element is located at the boundary of the array, only include the
neighbors that are in the table. For example, if row and column are both 0, there are
only three neighbors.

Explanation / Answer

public class Table { private int[][] values; public Table(int rows, int columns) { values = new int[rows][columns]; } public void set(int i, int j, int n) { values[i][j] = n; } public int getCell(int i, int j) { return values[i][j]; } public int getI() { return values.length; } public int getJ(int i) { return values[i].length; } public double neighborAverage(int rows, int columns) { double average = 0; int i = rows - 1; int j = columns - 1; // upper left corner if (i == 0 && j == 0) average = (values[i][j+1] + values[i+1][j] + values[i+1][j+1]) / 3.0; // top right else if (i == 0 && j == values[i].length - 1) average = (values[i][j-1] + values[i+1][j-1] + values[i+1][j]) / 3.0; // lower left else if (i == values.length - 1 && j == 0) average = (values[i-1][j] + values[i-1][j+1] + values[i][j+1]) / 3.0; // lower right else if (i == values.length - 1 && j == values[i].length - 1) average = (values[i-1][j-1] + values[i-1][j] + values[i][j-1]) / 3.0; // top middle else if (i == 0) { average = (values[i][j-1] + values[i][j+1] + values[i+1][j-1] + values[i+1][j] + values [i+1][j+1]) / 5.0; } // left middle else if (j == 0) { average = (values[i-1][j] + values[i-1][j+1] + values[i][j+1] + values[i+1][j] + values [i+1][j+1]) / 5.0; } // right middle else if (j == values[i].length) { average = (values[i-1][j-1] + values[i-1][j] + values[i][j-1] + values[i+1][j-1] + values [i+1][j]) / 5.0; } // bottom middle else if (i == values.length) { average = (values[i-1][j-1] + values[i-1][j] + values[i-1][j+1] + values[i][j-1] + values [i][j+1]) / 5.0; } // center else { average = (values[i-1][j-1] + values[i-1][j] + values[i-1][j+1] + values[i][j-1] + values[i][j+1] + values[i+1][j-1] + values[i+1][j] + values[i+1][j+1]) / 8.0; } return average; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote