Use JAVA Consider the following class public class Table pr

Use JAVA.

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 #1.

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.

Solution

Here is the modified code for you:

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 #1.
public double neighborAverage(int row, int column)
{
int n = values.length();
if(row == 0 && column == 0)
return values[0][1] + values[1][0] + values[1][1];
else if(row == 0 && column == n-1)
return values[0][n-2] + values[1][n-2] + values[1][n-1];
else if(row == n-1 && column == 0)
return values[n-2][0] + values[n-1][1] + values[n-1][1];
else if(row == n-1 && column == n-1)
return values[n-2][n-1] + values[n-2][n-2] + values[n-1][n-2];   
else if(row == 0)
return values[row][column-1] + values[row][column+1] + values[row+1][column -1] + values[row+1][column] + values[row+1][column+1];
else if(row == n-1)
return values[row][column-1] + values[row][column+1] + values[row-1][column-1] + values[row-1][column] + values[row-1][column+1];
else if(column == 0)
return values[row-1][column] + values[row+1][column] + values[row-1][column+1] + values[row][column+1] + values[row+1][column+1];
else if(column == n-1)
return values[row-1][column] + values[row+1][column] + values[row-1][column-1] + values[row][column-1] + values[row+1][column-1];
else
return 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];                
}
}

Use JAVA. Consider the following class: public class Table { private int[][] values; public Table(int rows, int columns) { values = new int[rows][columns]; } pu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site