Question
How do you declare a public class Matrix in a file name Matrix.java? For the code below. public class Matrix { public static void main(String[]
How do you declare a public class Matrix in a file name Matrix.java? For the code below.
public class Matrix {
public static void main(String[] args) {
// creates a two-dimensional array with dimensions of 1010 and named matrix.
int[][] matrix = new int[10][10];
/* * On the diagonal of this matrix, put the numbers from 0 to 9 and the number 0
* everywhere else */
// for each row
for (int i = 0; i < 10; i++) {
// for each column for (int j = 0; j < 10; j++) { // if it is diagonal element store i otherwise 0 if (i == j) // put the number i(0-9) matrix[i][j] = i; } }
// initialize sum of diagonal element to 0 int sum = 0; for (int i = 0; i < 10; i++) { // for each column for (int j = 0; j < 10; j++) { // if it is diagonal element if (i == j) // add element to sum sum += matrix[i][j]; } }
// print the matrix System.out.println("Matrix is: "); for (int i = 0; i < 10; i++) { // for each column for (int j = 0; j < 10; j++) { System.out.print(matrix[i][j] + " ");
} System.out.println();
} // print the sum of diagonal elements System.out.println(" Sum of diagonal elements : " + sum);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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