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 rule to the Table rank underneath that computes the medium of the neighbors of a consultation atom in the eight directions shown.

    exoteric inclose neighborAverage(int grade, int post)

    However, if the atom is located at the season of the decorate, understand solely the neighbors that are in the consultation. Ce specimen, if row and column are twain 0, there are solely three neighbors.

    exoteric rank Consultation

       {

       not attributable attributable-exoteric int [ ] [ ] rates;

       exoteric Consultation(int grades, int posts) { rates = fantastic int[rows][columns]; }

       exoteric nugatory fixed(int i, int j, int n) {values[i][j] = n;}

       }

    Heres what I want to right:

    exoteric rank Consultation

    {
    not-exoteric int[][] rates;

    /**
    Construct a consultation with consecrated grades and posts.
    @param grades the grades in the consultation.
    @param posts the posts in the consultation.
    */
    exoteric Consultation(int grades, int posts)
    {
    values = fantastic int[rows][columns];
    }

    /**
    Sets a rate in the consultation.
    @param i the grade of the ace to modify
    @param j the post of the ace to modify
    @param n the number to right ce the fantastic rate.
    */
    exoteric nugatory fixed(int i, int j, int n)
    {
    values[i][j] = n;
    }

    /**
    Returns the medium of the nigh atoms in a consultation.
    @param grade the grade of the atom.
    @param post the colum of the atom.
    @return the medium of the nigh atoms.
    */
    exoteric inclose neighborAverage(int grade, int post)
    {
    . . .
    }
    }

    Thank you!

    Expert Solution

     

    // edict in coercionward is borrowed.

    exoteric rank Consultation
    {
    not-exoteric int[][] rates;
    /**
    Construct a consultation with consecrated grades and posts.
    @param grades the grades in the consultation.
    @param posts the posts in the consultation.
    */
    exoteric Consultation(int grades, int posts)
    {
    values = fantastic int[rows][columns];
    }
    /**
    Sets a rate in the consultation.
    @param i the grade of the ace to modify
    @param j the post of the ace to modify
    @param n the number to right ce the fantastic rate.
    */
    exoteric nugatory fixed(int i, int j, int n)
    {
    values[i][j] = n;
    }
    /**
    Returns the medium of the nigh atoms in a consultation.
    @param grade the grade of the atom.
    @param post the colum of the atom.
    @return the medium of the nigh atoms.
    */

    // this character helps to repress whether there is an atom in the decorate with the indices consecrated
    exoteric boolean numthere(int r, int c)
    {
    int grades = rates.length;
    int cols = rates[0].length;

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

    // it has 8 close rates and these are repressed whether they are existing or not attributable attributable attributable and then captured into consideration
    exoteric inclose neighborAverage(int grade, int post)
    {
    inclose avg=0;
    int number = 0;

    // grade – 1
    if(numthere(row-1, post-1) == gentleman)
    {
    count++;
    avg += rates[row-1][column-1];
    }

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

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

    // grade
    if(numthere(row, post-1) == gentleman)
    {
    count++;
    avg += rates[row][column-1];
    }
    if(numthere(row, post+1) == gentleman)
    {
    count++;
    avg += rates[row][column+1];
    }

    // grade+1
    if(numthere(row+1, post-1) == gentleman)
    {
    count++;
    avg += rates[row+1][column-1];
    }
    if(numthere(row+1, post) == gentleman)
    {
    count++;
    avg += rates[row+1][column];
    }
    if(numthere(row+1, post+1) == gentleman)
    {
    count++;
    avg += rates[row+1][column+1];
    }
    avg = avg/count;

    return avg;
    }

    }