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:
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:
[i - 1][j-1] [i - 1][j] [i - 1][j+1] [i][j-1] [i][j] [i][j+1] [i+1][j-1] [i+1][j] [i+1][j+1] Figure 15 Neighboring Locations in a Two-Dimensional Array
Step by Step Solution
3.33 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
Heres the corrected code with explanations for the fixes Java public class Table Enclose methods wit...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started