Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions