Please write this Java program as simple as possible and with comments throughou
ID: 3733245 • Letter: P
Question
Please write this Java program as simple as possible and with comments throughout explaining what is happening. Thank you
Program 1(30 points: Design and implement a Java program for programming exercise 8.1, page 305 (name it SumArrayColumns) as described in the problem statement. Write method sumColumn) as specified. To test this method, the main method of your program prompts the user to enter a 3-by-4 matrix and displays the sum of each column (by calling method sumcolumn (). The method sumcolumn () must be designed to hand le two-dimensional arrays with any number of rows and columns. Design the main method of your program to handle all input and output and to allow the user to re-run the program with different sets of inputs (i.e., use a loop). Document your code and organize the output using appropriate formatting techniques
Explanation / Answer
SumArrayColumns.java
import java.util.Scanner;
public class SumArrayColumns {
public static void main(String[] args) {
double d[][] = new double[3][4];
Scanner scan = new Scanner(System.in);
System.out.println("Enter a 3-by-4 matrix row by row: ");
for(int i=0; i<d.length; i++){
for(int j=0; j<d[i].length; j++){
d[i][j] = scan.nextDouble();
}
}
for(int i=0; i<4; i++){
System.out.println("Sum of the elements at column "+i+" is "+sumColumn(d,i));
}
}
public static double sumColumn(double[][] m, int columnIndex){
double sum = 0;
for(int i=0; i<m.length;i++){
sum = sum + m[i][columnIndex];
}
return sum;
}
}
Output:
Enter a 3-by-4 matrix row by row:
1.5 2 3 4
5.5 6 7 8
9.5 1 31
1
Sum of the elements at column 0 is 16.5
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 41.0
Sum of the elements at column 3 is 13.0