You are writing a program that allows the user to input data to a grid (a matrix
ID: 3675632 • Letter: Y
Question
You are writing a program that allows the user to input data to a grid (a matrix) of N by N cells. You will define the size of your Spreadsheet in your program as a static final variable. Your software will support the following operations….
(1)Input data; Ask the user for an x and y position in your spreadsheet, and then some (double) value z. Put the z-value in your matrix at the (x,y) position.
(2)Row Sum : Ask the user for a row and then your software produces the sum of every element in that row.
Step 1: Input Data Using Scanner
If the user chooses to input data, you should ask the user for the x, y coordinate pair where they would like to put the data, and ask the user for the actual double value as well. This might correspond to “case 1” in your switch statement inside your main.
Step 2: Sum a Row
Add to the switch/case statement so that the user may select to sum up a row. Once the user indicates this, build a function called sumRow(int targetRow) that takes a target row and sums it.
Step 3: Sum a Column
Add more to the switch/case statement so that the user may select to sum up a column. Once the user indicates this, build a function called sumCol(int targetCol) that takes a target column and sums it up.
Step 4: Average a Row
Build another case in your switch so the user can select to average a specified row. Once the user selects this option, reuse the function in step 2 to produce your average. (hint: divide by N)
Step 5: Average a Column
Build another case in your switch so the user can select to average a specified column. Once the user selects this option, reuse the function in step 3 to produce your average. (hint: divide by N)
Step 6: Sum the Whole Set
Build another case in your switch so the user can select to sum the whole matrix. Once the user selects this option, reuse the function in step 2 to produce your sum.
Step 7: Average the Whole Set
Build another case in your switch so the user can select to average the whole matrix. Once the user selects this option, reuse the functions in any of the previous steps to produce your average.
(3)Column Sum: Ask the user for a column and then produce the sum of the elements in that column.
(4)Row Average: Ask the user for a row and then produce the average of this row (using the row sum function, not building from scratch)
(5)Column Average: Ask the user for a column and then produce the average of every element in this column.
(6)Total Sum : the sum of every element in the matrix. Build the total sum by calling the row or column sum functions, not from scratch.
(7)Total Average: the average of every element in the matrix. Build the total average by reusing Row/Col Average or Row/Col Sum functions already completed above.
|0.00||0.00|
|0.00||0.00|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
1
Enter X Y Value:
0 0 1.1
|1.10||0.00|
|0.00||0.00|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
1
Enter X Y Value:
0 1 2.2
|1.10||0.00|
|2.20||0.00|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
1
Enter X Y Value:
1 0 3.3
|1.10||3.30|
|2.20||0.00|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
1
Enter X Y Value:
1 1 4.4
|1.10||3.30|
|2.20||4.40|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
2
Enter Row:
0
Sum : 4.40
|1.10||3.30|
|2.20||4.40|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
5
Enter Column:
0
Average : 1.65
|1.10||3.30|
|2.20||4.40|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
6
Sum : 11.00
|1.10||3.30|
|2.20||4.40|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
7
Average : 2.75
|1.10||3.30|
|2.20||4.40|
(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit
8
Good Bye!
Explanation / Answer
import java.util.*;
import java.io.*;
public class Main {
private static Scanner reader = new Scanner(System.in);
private static final int N = 2;
private static double[][] matrix = new double[N][N];
public static void main(String[] args) {
boolean keepRunning = true;
while (keepRunning) {
System.out.println("(1) Input (2) Row Sum (3) Col Sum (4) Row Ave (5) Col Ave (6) Sum (7) Average (8) Quit");
int userOption = reader.nextInt();
int row, col;
switch(userOption) {
case 1:
System.out.println("Enter X Y value:");
int x = reader.nextInt();
int y = reader.nextInt();
double z = reader.nextDouble();
matrix[y][x] = z;
printMatrix();
break;
case 2:
System.out.println("Enter row:");
row = reader.nextInt();
System.out.println("Row Sum: " + sumRow(row));
break;
case 3:
System.out.println("Enter col:");
col = reader.nextInt();
System.out.println("Col Sum: " + sumCol(col));
break;
case 4:
System.out.println("Enter row:");
row = reader.nextInt();
System.out.println("Row Average: " + avgRow(row));
break;
case 5:
System.out.println("Enter col:");
col = reader.nextInt();
System.out.println("Col Average: " + avgCol(col));
break;
case 6:
System.out.println("Sum all: " + sumAll());
break;
case 7:
System.out.println("Average all: " + avgAll());
break;
case 8:
System.out.println("Thank you!");
keepRunning = false;
break;
default:
System.out.println("Invalid option!");
}
}
}
public static double sumRow(int targetRow) {
double sum = 0.0;
for (int i = 0; i < N; ++i) {
sum += matrix[targetRow][i];
}
return sum;
}
public static double sumCol(int targetCol) {
double sum = 0.0;
for (int i = 0; i < N; ++i) {
sum += matrix[i][targetCol];
}
return sum;
}
public static double avgRow(int targetRow) {
return sumRow(targetRow) / N;
}
public static double avgCol(int targetCol) {
return sumCol(targetCol) / N;
}
public static double sumAll() {
double sum = 0.0;
for (int i = 0; i < N; ++i) {
sum += sumRow(i);
}
return sum;
}
public static double avgAll() {
return sumAll() / (N * N);
}
public static void printMatrix() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
System.out.print("|" + matrix[i][j] + "|");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.