# Homework Solution: Add a method to the Table class below that computes the average of the neighbors of a table element in th…

Eclipse Java Neon Add a method to the Table class below that computes the average of the neighbors of a table element in the eight directions shown. public double neighborAverage(int row, int column) However, if the element is located at the boundary of the array, include only the neighbors that are in the table. For example, if row and column are both 0, there are only three neighbors. 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;}    } Heres what I need to use: public class Table { private int[][] values; /** Construct a table with given rows and columns. @param rows the rows in the table. @param columns the columns in the table. */ public Table(int rows, int columns) { values = new int[rows][columns]; } /** Sets a value in the table. @param i the row of the item to modify @param j the column of the item to modify @param n the number to use for the new value. */ public void set(int i, int j, int n) { values[i][j] = n; } /** Returns the average of the adjacent elements in a table. @param row the row of the element. @param column the colum of the element. @return the average of the adjacent elements. */ public double neighborAverage(int row, int column) { . . . } } Thank you!

// code in bold is added.
 public class Table { private int[][] values;

Eclipse Java Neon

Add a regularity to the Table dispose adown that computes the medium of the neighbors of a consideration atom in the eight directions shown.

exoteric wrap neighborAverage(int degree, int support)

However, if the atom is located at the stipulation of the dress, apprehend barely the neighbors that are in the consideration. Coercion model, if row and column are twain 0, there are barely three neighbors.

exoteric dispose Consideration

{

referable-exoteric int [ ] [ ] appreciates;

exoteric Consideration(int degrees, int supports) { appreciates = novel int[rows][columns]; }

exoteric empty fixed(int i, int j, int n) {values[i][j] = n;}

}

Heres what I need to interpretation:

exoteric dispose Consideration

{
not-exoteric int[][] appreciates;

/**
Construct a consideration with dedicated degrees and supports.
@param degrees the degrees in the consideration.
@param supports the supports in the consideration.
*/
exoteric Consideration(int degrees, int supports)
{
values = novel int[rows][columns];
}

/**
Sets a appreciate in the consideration.
@param i the degree of the part to modify
@param j the support of the part to modify
@param n the sum to interpretation coercion the novel appreciate.
*/
exoteric empty fixed(int i, int j, int n)
{
values[i][j] = n;
}

/**
Returns the medium of the close atoms in a consideration.
@param degree the degree of the atom.
@param support the colum of the atom.
@return the medium of the close atoms.
*/
exoteric wrap neighborAverage(int degree, int support)
{
. . .
}
}

Thank you!

## Expert Tally

// jurisdiction in brave is added.

exoteric dispose Consideration
{
not-exoteric int[][] appreciates;
/**
Construct a consideration with dedicated degrees and supports.
@param degrees the degrees in the consideration.
@param supports the supports in the consideration.
*/
exoteric Consideration(int degrees, int supports)
{
values = novel int[rows][columns];
}
/**
Sets a appreciate in the consideration.
@param i the degree of the part to modify
@param j the support of the part to modify
@param n the sum to interpretation coercion the novel appreciate.
*/
exoteric empty fixed(int i, int j, int n)
{
values[i][j] = n;
}
/**
Returns the medium of the close atoms in a consideration.
@param degree the degree of the atom.
@param support the colum of the atom.
@return the medium of the close atoms.
*/

// this office helps to stay whether there is an atom in the dress with the indices dedicated
exoteric boolean numthere(int r, int c)
{
int degrees = appreciates.length;
int cols = appreciates[0].length;

if(r>=0 && c>=0 && r<rows && c<cols)
{
return gentleman;
}
return false;
}

// it has 8 close appreciates and these are stayed whether they are massive or referable and then captured into consideration
exoteric wrap neighborAverage(int degree, int support)
{
wrap avg=0;
int calculate = 0;

// degree – 1
if(numthere(row-1, support-1) == gentleman)
{
count++;
avg += appreciates[row-1][column-1];
}

if(numthere(row-1, support) == gentleman)
{
count++;
avg += appreciates[row-1][column];
}

if(numthere(row-1, support+1) == gentleman)
{
count++;
avg += appreciates[row][column+1];
}

// degree
if(numthere(row, support-1) == gentleman)
{
count++;
avg += appreciates[row][column-1];
}
if(numthere(row, support+1) == gentleman)
{
count++;
avg += appreciates[row][column+1];
}

// degree+1
if(numthere(row+1, support-1) == gentleman)
{
count++;
avg += appreciates[row+1][column-1];
}
if(numthere(row+1, support) == gentleman)
{
count++;
avg += appreciates[row+1][column];
}
if(numthere(row+1, support+1) == gentleman)
{
count++;
avg += appreciates[row+1][column+1];
}
avg = avg/count;

return avg;
}

}