Java. Only need the calculate sum part. Please use the template, thank you!

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java. Only need the calculate sum part. Please use the template, thank you!

Problem 10: Table
Given a Table class (Table.java ), fill in two methods: neighborAverage (int row, int
column) and sum(int i, boolean horizontal).See the template for more details.
Examples:
Table table =
new Table (4, 5); // 4 x 5 table
/ *
* Fill it with a sequence of values:
* 6 9 12 15 18
8 12 16 20 24
* 10 15 20 25 30
* 12 18 24 30 36
* /
for (int i = 0; i < 4; i++) {
for (int j
= 0; j < 5; j++) {
table.set(i, j, (3 + i)
(2 + j));
}
}
table.sum (0, true); // returns 60.0
table.sum (0, false); // returns 36.0
table.sum (1, true); // returns 80.0
table.sum (1, false); // returns 54.0
table.sum (2, true); // returns 100.0
table.sum (2, false); // returns 72.0
table.sum (3, true); // returns 120.0
table.sum (3, false); // returns 90.0
table.sum ( 4, false); // returns 108.0
Transcribed Image Text:Problem 10: Table Given a Table class (Table.java ), fill in two methods: neighborAverage (int row, int column) and sum(int i, boolean horizontal).See the template for more details. Examples: Table table = new Table (4, 5); // 4 x 5 table / * * Fill it with a sequence of values: * 6 9 12 15 18 8 12 16 20 24 * 10 15 20 25 30 * 12 18 24 30 36 * / for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { table.set(i, j, (3 + i) (2 + j)); } } table.sum (0, true); // returns 60.0 table.sum (0, false); // returns 36.0 table.sum (1, true); // returns 80.0 table.sum (1, false); // returns 54.0 table.sum (2, true); // returns 100.0 table.sum (2, false); // returns 72.0 table.sum (3, true); // returns 120.0 table.sum (3, false); // returns 90.0 table.sum ( 4, false); // returns 108.0
2
30 /**
Represents a table of values.
*/
*
4
6 public class Table {
7
8
private int[]I] values;
9.
100
/**
11
* Constructs a table with given rows and columns.
*
12
13
14
15
160
17
the rows in the table (assume always > 0)
*
@param rows
@param columns the columns in the table (assume always > 0)
*/
public Table(int rows, int columns) {
values = new int[rows][columns];
}
18
19
20
210
/**
22
Sets a value in the table.
*
23
@param i the row of the item to modify (assume always within the bounds of the table)
* @param j the column of the item to modify (assume always within the bounds of the table)
* @param n the number to use for the new value.
*
24
25
26
*/
27
280
29
public void set(int i, int j, int n) {
values[i][j]
}
= n;
30
31
32
330
/**
960
/ **
97
* Returns the sum of a row or column in elements in the table
98
99
* @param i
the row or column to sum. Assume it will always
L00
L01
L02
L03
L040
L05
L06
*
be within the bounds of the table
@param horizontal
@return the sum of the given row or column.
if true, then i represents a row, else a column.
*
*
public double sum(int i, boolean horizontal) {
return -1.0; // FIX ME
07
}
.08
Transcribed Image Text:2 30 /** Represents a table of values. */ * 4 6 public class Table { 7 8 private int[]I] values; 9. 100 /** 11 * Constructs a table with given rows and columns. * 12 13 14 15 160 17 the rows in the table (assume always > 0) * @param rows @param columns the columns in the table (assume always > 0) */ public Table(int rows, int columns) { values = new int[rows][columns]; } 18 19 20 210 /** 22 Sets a value in the table. * 23 @param i the row of the item to modify (assume always within the bounds of the table) * @param j the column of the item to modify (assume always within the bounds of the table) * @param n the number to use for the new value. * 24 25 26 */ 27 280 29 public void set(int i, int j, int n) { values[i][j] } = n; 30 31 32 330 /** 960 / ** 97 * Returns the sum of a row or column in elements in the table 98 99 * @param i the row or column to sum. Assume it will always L00 L01 L02 L03 L040 L05 L06 * be within the bounds of the table @param horizontal @return the sum of the given row or column. if true, then i represents a row, else a column. * * public double sum(int i, boolean horizontal) { return -1.0; // FIX ME 07 } .08
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY