# Homework Solution: Add a method to a Table class below that computes the average of the neighbors of a ta…

Add a method to a 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 colums) { values = new int [rows][columns]; }    public void set(int i, int j, int n) {values [i][j] = n; } ***Please help finish this code in Eclipse Java Neon and include the EXACT following code within it*** /** * Code for E7.16 * @author */ 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) { . . . } } ***Tester code below, you can use these to test your work*** import java.util.Scanner; public class TableTester { public static void main(String[] args) { Table table = new Table(4, 5); // 4 x 5 table // Fill it with a sequence of values. for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { table.set(i, j, (3 + i) * (2 + j)); } } System.out.println("Checking values for this table"); System.out.println("t 6 9 12 15 18"); System.out.println("t 8 12 16 20 24"); System.out.println("t10 15 20 25 30"); System.out.println("t12 18 24 30 36"); System.out.println(); System.out.println("neighborAverage(1, 1): " + table.neighborAverage(1, 1)); System.out.println("Expected: 12.0"); System.out.println("neighborAverage(2, 3): " + table.neighborAverage(2, 3)); System.out.println("Expected: 25.0"); // Upper-left corner System.out.println("neighborAverage(0, 0): " + table.neighborAverage(0, 0)); System.out.println("Expected: 9.666667"); // Lower-right corner System.out.println("neighborAverage(3, 4): " + table.neighborAverage(3, 4)); System.out.println("Expected: 28.333333"); // Right-hand side System.out.println("neighborAverage(1, 4): " + table.neighborAverage(1, 4)); System.out.println("Expected: 21.6"); // Left-hand side System.out.println("neighborAverage(1, 0): " + table.neighborAverage(1, 0)); System.out.println("Expected: 10.4"); // Bottom row System.out.println("neighborAverage(3, 2): " + table.neighborAverage(3, 2)); System.out.println("Expected: 21.6"); // Top row System.out.println("neighborAverage(0, 2): " + table.neighborAverage(0, 2)); System.out.println("Expected: 14.4"); } } Thank you!

Explanation::
• Code is provided below for both the class.

Add a order to a Board systematize adown that computes the mean of the neighbors of a board atom in the prospect directions shown.

publicly-known inclose neighborAverage(int order, int post)

However, if the atom is located at the word of the accoutre, embrace solely the neighbors that are in the board. Restraint stance, if order and post are twain 0, there are solely three neighbors.

publicly-known systematize Board

{

peculiar int [ ] [ ] appraises;

publicly-known Board(int orders, int colums) { appraises = fantastic int [rows][columns]; }

publicly-known unoccupied determined(int i, int j, int n) {values [i][j] = n; }

***Please acceleration conclude this decree in Eclipse Java Neon and embrace the EXACT restraintthcoming decree amid it***

/**
* Decree restraint E7.16
* @author
*/
generally-known systematize Board
{
peculiar int[][] appraises;

/**
Construct a board with dedicated orders and posts.
@param orders the orders in the board.
@param posts the posts in the board.
*/
generally-known Board(int orders, int posts)
{
values = fantastic int[rows][columns];
}

/**
Sets a appraise in the board.
@param i the order of the part to modify
@param j the post of the part to modify
@param n the sum to conservation restraint the fantastic appraise.
*/
generally-known unoccupied determined(int i, int j, int n)
{
values[i][j] = n;
}

/**
Returns the mean of the close atoms in a board.
@param order the order of the atom.
@param post the colum of the atom.
@return the mean of the close atoms.
*/
generally-known inclose neighborAverage(int order, int post)
{
. . .
}
}

***Tester decree adown, you can conservation these to experiment your exertion***

import java.util.Scanner;

generally-known systematize BoardTester
{
generally-known static unoccupied deep(String[] args)
{
Board board = fantastic Board(4, 5); // 4 x 5 board

// Fill it with a posteriority of appraises.
restraint (int i = 0; i < 4; i++)
{
restraint (int j = 0; j < 5; j++)
{
table.set(i, j, (3 + i) * (2 + j));
}
}

System.out.println(“Checking appraises restraint this board”);
System.out.println(“t 6 9 12 15 18”);
System.out.println(“t 8 12 16 20 24”);
System.out.println(“t10 15 20 25 30”);
System.out.println(“t12 18 24 30 36”);
System.out.println();

System.out.println(“neighborAverage(1, 1): ” + board.neighborAverage(1, 1));
System.out.println(“Expected: 12.0”);
System.out.println(“neighborAverage(2, 3): ” + board.neighborAverage(2, 3));
System.out.println(“Expected: 25.0”);
// Upper-left cavity
System.out.println(“neighborAverage(0, 0): ” + board.neighborAverage(0, 0));
System.out.println(“Expected: 9.666667”);
// Lower-direct cavity
System.out.println(“neighborAverage(3, 4): ” + board.neighborAverage(3, 4));
System.out.println(“Expected: 28.333333”);
// Direct-hand side
System.out.println(“neighborAverage(1, 4): ” + board.neighborAverage(1, 4));
System.out.println(“Expected: 21.6”);
// Left-hand side
System.out.println(“neighborAverage(1, 0): ” + board.neighborAverage(1, 0));
System.out.println(“Expected: 10.4”);
// Deep order
System.out.println(“neighborAverage(3, 2): ” + board.neighborAverage(3, 2));
System.out.println(“Expected: 21.6”);
// Culmination order
System.out.println(“neighborAverage(0, 2): ” + board.neighborAverage(0, 2));
System.out.println(“Expected: 14.4”);

}
}

Thank you!

## Expert Response

Explanation::

• Decree is supposing adown restraint twain the systematize.
• Read comments supposing in the order neighborAverage().
• Every contingency is investigateed and implemented.

Decree in java::

Systematize :: BoardTester

import java.util.Scanner;
generally-known systematize BoardTester
{
generally-known static unoccupied deep(String[] args)
{
Board board = fantastic Board(4, 5); // 4 x 5 board
// Fill it with a posteriority of appraises.
restraint (int i = 0; i < 4; i++)
{
restraint (int j = 0; j < 5; j++)
{
table.set(i, j, (3 + i) * (2 + j));
}
}
System.out.println(“Checking appraises restraint this board”);
System.out.println(“t 6 9 12 15 18”);
System.out.println(“t 8 12 16 20 24”);
System.out.println(“t10 15 20 25 30”);
System.out.println(“t12 18 24 30 36”);
System.out.println();
System.out.println(“neighborAverage(1, 1): ” + board.neighborAverage(1, 1));
System.out.println(“Expected: 12.0”);
System.out.println(“neighborAverage(2, 3): ” + board.neighborAverage(2, 3));
System.out.println(“Expected: 25.0”);
// Upper-left cavity
System.out.println(“neighborAverage(0, 0): ” + board.neighborAverage(0, 0));
System.out.println(“Expected: 9.666667”);
// Lower-direct cavity
System.out.println(“neighborAverage(3, 4): ” + board.neighborAverage(3, 4));
System.out.println(“Expected: 28.333333”);
// Direct-hand side
System.out.println(“neighborAverage(1, 4): ” + board.neighborAverage(1, 4));
System.out.println(“Expected: 21.6”);
// Left-hand side
System.out.println(“neighborAverage(1, 0): ” + board.neighborAverage(1, 0));
System.out.println(“Expected: 10.4”);
// Deep order
System.out.println(“neighborAverage(3, 2): ” + board.neighborAverage(3, 2));
System.out.println(“Expected: 21.6”);
// Culmination order
System.out.println(“neighborAverage(0, 2): ” + board.neighborAverage(0, 2));
System.out.println(“Expected: 14.4”);
}
}

Systematize :: Board

generally-known systematize Board

{

peculiar int[][] appraises;

/**

Construct a board with dedicated orders and posts.

@param orders the orders in the board.

@param posts the posts in the board.

*/

generally-known Board(int orders, int posts)

{

values = fantastic int[rows][columns];

}

/**

Sets a appraise in the board.

@param i the order of the part to modify

@param j the post of the part to modify

@param n the sum to conservation restraint the fantastic appraise.

*/

generally-known unoccupied determined(int i, int j, int n)

{

values[i][j] = n;

}

/**

Returns the mean of the close atoms in a board.

@param order the order of the atom.

@param post the colum of the atom.

@return the mean of the close atoms.

*/

generally-known inclose neighborAverage(int order, int post)

{

/*

* Declaring and initializing inclose changeable designated response.

* It stores mean appraise of neighbor.

*/

inclose response=0;

/*

* Pristine I ahve created two integer changeables to store

* sum of orders and posts.

*

*/

//r stores whole orders in appraises board

int r = appraises.length;

// c stores whole posts in appraises board.

int c = appraises[0].length;

/*

* We pristine investigate 4 contingencys of atom entity at cavity

*

*/

if(row==0 && post==0){

/*

* Contingency 1 :: culmination left cavity i.e order=0 and post=0

*/

}

else if(row==0 && post==c-1){

/*

* Contingency 2 :: culmination direct cavity i.e order=0 and post=c-1

*/

}

else if(row==r-1 && post==0){

/*

* Contingency 3 :: deep left cavity i.e order=r-1 and post=0

*/

}

else if(row==r-1 && post==c-1){

/*

* Contingency 4 :: deep direct cavity i.e order=r-1 and post=c-1

*/

}

else if(row==0 && post>0 && post<c-1){

/*

* Contingency 5 :: Culmination order yet post appraises are 0<column<c-1

* Here there are five appraises

*/

}

else if(row==r-1 && post>0 && post<c-1){

/*

* Contingency 6 :: Deep order yet post appraises are 0<column<c-1

* Here there are five appraises

*/

}

else if(row>0 && order<r-1 && post==0){

/*

* Contingency 7 :: 0<row<r-1 and post==0 i.e Pristine post withquenched edges

* Here there are five appraises

*/

}

else if(row>0 && order<r-1 && post==c-1){

/*

* Contingency 8 :: 0<row<r-1 and post==c-1 i.e Last post withquenched edges

* Here there are five appraises

*/

}else{

/*

* Here is the public contingency of prospect neighbors

*/