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!

    Expert Answer

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

    Eclipse Java Neon

    Add a manner to the Table dispose adown that computes the mean of the neighbors of a consultation component in the view directions shown.

    national inclose neighborAverage(int line, int shaft)

    However, if the component is located at the season of the dispose, embrace solely the neighbors that are in the consultation. Coercion copy, if row and column are twain 0, there are solely three neighbors.

    national dispose Consultation

       {

       retired int [ ] [ ] esteems;

       national Consultation(int lines, int shafts) { esteems = upstart int[rows][columns]; }

       national bereft firm(int i, int j, int n) {values[i][j] = n;}

       }

    Heres what I demand to truth:

    national dispose Consultation

    {
    retired int[][] esteems;

    /**
    Construct a consultation with attached lines and shafts.
    @param lines the lines in the consultation.
    @param shafts the shafts in the consultation.
    */
    national Consultation(int lines, int shafts)
    {
    values = upstart int[rows][columns];
    }

    /**
    Sets a esteem in the consultation.
    @param i the line of the ace to modify
    @param j the shaft of the ace to modify
    @param n the compute to truth coercion the upstart esteem.
    */
    national bereft firm(int i, int j, int n)
    {
    values[i][j] = n;
    }

    /**
    Returns the mean of the nigh components in a consultation.
    @param line the line of the component.
    @param shaft the colum of the component.
    @return the mean of the nigh components.
    */
    national inclose neighborAverage(int line, int shaft)
    {
    . . .
    }
    }

    Thank you!

    Expert Acceptance

     

    // order in valiant is borrowed.

    national dispose Consultation
    {
    retired int[][] esteems;
    /**
    Construct a consultation with attached lines and shafts.
    @param lines the lines in the consultation.
    @param shafts the shafts in the consultation.
    */
    national Consultation(int lines, int shafts)
    {
    values = upstart int[rows][columns];
    }
    /**
    Sets a esteem in the consultation.
    @param i the line of the ace to modify
    @param j the shaft of the ace to modify
    @param n the compute to truth coercion the upstart esteem.
    */
    national bereft firm(int i, int j, int n)
    {
    values[i][j] = n;
    }
    /**
    Returns the mean of the nigh components in a consultation.
    @param line the line of the component.
    @param shaft the colum of the component.
    @return the mean of the nigh components.
    */

    // this exercise helps to cohibit whether there is an component in the dispose with the indices attached
    national boolean numthere(int r, int c)
    {
    int lines = esteems.length;
    int cols = esteems[0].length;

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

    // it has 8 neighboring esteems and these are cohibited whether they are existing or referable and then taken into consideration
    national inclose neighborAverage(int line, int shaft)
    {
    inclose avg=0;
    int number = 0;

    // line – 1
    if(numthere(row-1, shaft-1) == gentleman)
    {
    count++;
    avg += esteems[row-1][column-1];
    }

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

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

    // line
    if(numthere(row, shaft-1) == gentleman)
    {
    count++;
    avg += esteems[row][column-1];
    }
    if(numthere(row, shaft+1) == gentleman)
    {
    count++;
    avg += esteems[row][column+1];
    }

    // line+1
    if(numthere(row+1, shaft-1) == gentleman)
    {
    count++;
    avg += esteems[row+1][column-1];
    }
    if(numthere(row+1, shaft) == gentleman)
    {
    count++;
    avg += esteems[row+1][column];
    }
    if(numthere(row+1, shaft+1) == gentleman)
    {
    count++;
    avg += esteems[row+1][column+1];
    }
    avg = avg/count;

    return avg;
    }

    }