Question: 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
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!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
