I already have the code for the two assignments below but there are some errors in the
Question:
I already have the code for the two assignments below but there are some errors in the code, please help me fix them
The assignments:
•• 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; }
}
Next, write a tester class to run this program
•• E7.17/Given the Table class of Exercise •• E7.16 , add a method that returns the sum of the ith row (if horizontal is true) or column (if horizontal is false):
public double sum(int i, boolean horizontal)
Then, write a tester class to run this program
The code:
//As no additional data is provided
//Assuming table_name as table
//And size as rows x cols
//Moreover you can modify accordingly
public double neighborAverage(int row, int column){
double res = 0;
int count = 0;
int total_dir = 8;
int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
for(int i = 0; i int x,y;
x = row - dir[i][0];
y = column - dir[i][1];
if(x >= 0 && x = 0 && y res += table[x][y];
count += 1;
}
}
return (res/count);
}
public double sum(int i, boolean horizontal){
double res = 0;
//Assuming given i is valid
if(horizontal){
for(int j = 0; j res += table[i][j];
}
}else{
for(int j = 0; j res += table[j][i];
}
}
return res;
}
The code and the errors:
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang