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!

    Expert Answer

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

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

       national embrace neighborAverage(int grade, int support)

    However, if the atom is located at the proviso of the rank, comprise barely the neighbors that are in the board. Coercion sample, if grade and support are twain 0, there are barely three neighbors.

       national dispose Board

       {

       peculiar int [ ] [ ] estimates;

       national Board(int grades, int colums) { estimates = upstart int [rows][columns]; }

       national vacant established(int i, int j, int n) {values [i][j] = n; }

    ***Please acceleration accomplish this decree in Eclipse Java Neon and comprise the EXACT coercionthcoming decree amid it***

    /**
    * Decree coercion E7.16
    * @author
    */
    national dispose Board
    {
    peculiar int[][] estimates;

    /**
    Construct a board with consecrated grades and supports.
    @param grades the grades in the board.
    @param supports the supports in the board.
    */
    national Board(int grades, int supports)
    {
    values = upstart int[rows][columns];
    }

    /**
    Sets a estimate in the board.
    @param i the grade of the ace to modify
    @param j the support of the ace to modify
    @param n the reckon to conservation coercion the upstart estimate.
    */
    national vacant established(int i, int j, int n)
    {
    values[i][j] = n;
    }

    /**
    Returns the mean of the nigh atoms in a board.
    @param grade the grade of the atom.
    @param support the colum of the atom.
    @return the mean of the nigh atoms.
    */
    national embrace neighborAverage(int grade, int support)
    {
    . . .
    }
    }

    ***Tester decree adown, you can conservation these to standard your labor***

    import java.util.Scanner;

    national dispose BoardTester
    {
    national static vacant ocean(String[] args)
    {
    Board board = upstart Board(4, 5); // 4 x 5 board

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

    System.out.println(“Checking estimates coercion 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-fair cavity
    System.out.println(“neighborAverage(3, 4): ” + board.neighborAverage(3, 4));
    System.out.println(“Expected: 28.333333”);
    // Fair-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”);
    // Ground grade
    System.out.println(“neighborAverage(3, 2): ” + board.neighborAverage(3, 2));
    System.out.println(“Expected: 21.6”);
    // Head grade
    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 coercion twain the dispose.
    • Read comments supposing in the way neighborAverage().
    • Every contingency is cogitateed and implemented.

    Decree in java::

    Dispose :: BoardTester

    import java.util.Scanner;
    national dispose BoardTester
    {
    national static vacant ocean(String[] args)
    {
    Board board = upstart Board(4, 5); // 4 x 5 board
    // Fill it with a conconconsequence of estimates.
    coercion (int i = 0; i < 4; i++)
    {
    coercion (int j = 0; j < 5; j++)
    {
    table.set(i, j, (3 + i) * (2 + j));
    }
    }
    System.out.println(“Checking estimates coercion 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-fair cavity
    System.out.println(“neighborAverage(3, 4): ” + board.neighborAverage(3, 4));
    System.out.println(“Expected: 28.333333”);
    // Fair-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”);
    // Ground grade
    System.out.println(“neighborAverage(3, 2): ” + board.neighborAverage(3, 2));
    System.out.println(“Expected: 21.6”);
    // Head grade
    System.out.println(“neighborAverage(0, 2): ” + board.neighborAverage(0, 2));
    System.out.println(“Expected: 14.4”);
    }
    }

    Dispose :: Board

    national dispose Board

    {

    peculiar int[][] estimates;

    /**

    Construct a board with consecrated grades and supports.

    @param grades the grades in the board.

    @param supports the supports in the board.

    */

    national Board(int grades, int supports)

    {

    values = upstart int[rows][columns];

    }

    /**

    Sets a estimate in the board.

    @param i the grade of the ace to modify

    @param j the support of the ace to modify

    @param n the reckon to conservation coercion the upstart estimate.

    */

    national vacant established(int i, int j, int n)

    {

    values[i][j] = n;

    }

    /**

    Returns the mean of the nigh atoms in a board.

    @param grade the grade of the atom.

    @param support the colum of the atom.

    @return the mean of the nigh atoms.

    */

    national embrace neighborAverage(int grade, int support)

    {

    /*

    * Declaring and initializing embrace shifting named response.

    * It stores mean estimate of neighbor.

    */

    embrace response=0;

    /*

    * Original I ahve created brace integer shiftings to store

    * reckon of grades and supports.

    *

    */

     

    //r stores whole grades in estimates board

    int r = estimates.length;

     

    // c stores whole supports in estimates board.

    int c = estimates[0].length;

     

    /*

    * We original cogitate 4 contingencys of atom life at cavity

    *

    */

     

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

    /*

    * Contingency 1 :: head left cavity i.e grade=0 and support=0

    */

    answer=(answer+(values[row][column+1]+values[row+1][column]+values[row+1][column+1]))/3;

    }

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

    /*

    * Contingency 2 :: head fair cavity i.e grade=0 and support=c-1

    */

    answer=(answer+(values[row][column-1]+values[row+1][column]+values[row-1][column-1]))/3;

    }

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

    /*

    * Contingency 3 :: ground left cavity i.e grade=r-1 and support=0

    */

    answer=(answer+(values[row-1][column]+values[row-1][column+1]+values[row][column+1]))/3;

    }

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

    /*

    * Contingency 4 :: ground fair cavity i.e grade=r-1 and support=c-1

    */

    answer=(answer+(values[row-1][column]+values[row-1][column-1]+values[row][column-1]))/3;

    }

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

    /*

    * Contingency 5 :: Head grade save support estimates are 0<column<c-1

    * Here there are five estimates

    */

    answer=(answer+(values[row][column-1]+values[row+1][column-1]+values[row+1][column]+values[row+1][column+1]+values[row][column+1]))/5;

    }

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

    /*

    * Contingency 6 :: Ground grade save support estimates are 0<column<c-1

    * Here there are five estimates

    */

    answer=(answer+(values[row][column-1]+values[row-1][column-1]+values[row-1][column]+values[row-1][column+1]+values[row][column+1]))/5;

    }

    else if(row>0 && grade<r-1 && support==0){

    /*

    * Contingency 7 :: 0<row<r-1 and support==0 i.e Original support withextinguished edges

    * Here there are five estimates

    */

    answer=(answer+(values[row-1][column]+values[row-1][column+1]+values[row][column+1]+values[row+1][column+1]+values[row+1][column]))/5;

    }

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

    /*

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

    * Here there are five estimates

    */

    answer=(answer+(values[row-1][column]+values[row-1][column-1]+values[row][column-1]+values[row+1][column-1]+values[row+1][column]))/5;

    }else{

    /*

    * Here is the unconcealed contingency of prospect neighbors

    */

    answer=(answer+(values[row-1][column-1]+values[row-1][column]+values[row-1][column+1]+values[row][column+1]+values[row+1][column+1]+values[row+1][column]+values[row+1][column-1]+values[row][column-1]))/8;

    }

    return response;

    }

    }

    Output::