Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

E7.16 Add a method to the Table class below that computes the average of the neighbors of a table element in the eight directions shown

E7.16 Add a method to the Table class below that computes the average of the neighbors of a table element in the eight directions shown in Figure 15: 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; } }

-----

-----

-----

/** * 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) { . . . } }

-----

-----

-----

Here are some ran tests

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");

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

ISBN: B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Describe the guidelines for appropriate use of the direct plan.

Answered: 1 week ago

Question

LO4 Identify a system for controlling absenteeism.

Answered: 1 week ago