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

Eclipse Java Neon Add a method to the Table class below that computes the averag

ID: 3882754 • Letter: E

Question

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!

Explanation / Answer

// code in bold is added.

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.
*/

// this function helps to check whether there is an element in the array with the indices given
public boolean numthere(int r, int c)
{
int rows = values.length;
int cols = values[0].length;

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

// it has 8 neighboring values and these are checked whether they are existing or not and then taken into consideration
public double neighborAverage(int row, int column)
{
double avg=0;
int count = 0;

// row - 1
if(numthere(row-1, column-1) == true)
{
count++;
avg += values[row-1][column-1];
}

if(numthere(row-1, column) == true)
{
count++;
avg += values[row-1][column];
}

if(numthere(row-1, column+1) == true)
{
count++;
avg += values[row][column+1];
}

// row
if(numthere(row, column-1) == true)
{
count++;
avg += values[row][column-1];
}
if(numthere(row, column+1) == true)
{
count++;
avg += values[row][column+1];
}

// row+1
if(numthere(row+1, column-1) == true)
{
count++;
avg += values[row+1][column-1];
}
if(numthere(row+1, column) == true)
{
count++;
avg += values[row+1][column];
}
if(numthere(row+1, column+1) == true)
{
count++;
avg += values[row+1][column+1];
}
avg = avg/count;

return avg;
}

}
         
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