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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!