Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Solution code : **Please explain every single step from the solution code** 2. (

ID: 3594642 • Letter: S

Question

Solution code :

**Please explain every single step from the solution code**

2. (25 points) Write a boolean method that will return true if the sum of the diagonal (upper-left to lower-right) of a two-dimension array is equal to the sum of a given row: diagonalEqualsRow int [1 [] myArray, int row) If the array is not a square matrix of the row number is out of bounds, throw an llegalArgumentException with an appropriate message For example myArray 13 57 9 23 4 68 015 1 3 4 5 27 8 7 6 3 2 9 myArra 1 3 3 2 4 2 5 6 1 2 3 4 7 8 9 4 myArray3 1 3 5 6 7 087 4 3 myArray4 13 3|2 4 2 5 6 1 2 3 4 7 8 9 4 diagonalEqualsRow (myArrayl, 0) returns true diagonalEqualsRow (myArray2, 2) returns true diagonalEqualsRow (myArray3, 0) throw an exception - not a square matrix diagonalEqualsRow (myArrayl, 6) throw an exception - illegal row

Explanation / Answer

public class Question 2 {

public static void main(String[] args) {

int a[][] = {{1,3,3,2},{4,2,5,6},{1,2,3,4},{7,8,9,4}}; //initialised array

if (diagonalEqualsRow(a,2)) //call the function if the function return true execute the statement below i.e print yes on the screen

System.out.println("Yes.")

}

public static boolean diagonalEqualsRow(int myArray[][],int row) {

if(row >= myArray.length) //check if the alloted row is out of the range of the matrix

throw new IllegalArgumentException("Row out of range.");

if(myArray.length != myArray[0].length) //check wheather the matrix is square or not

throw new IllegalArgumentException("Matrix is not square.");

int rowSum = 0; //initialised variables

int diagSum = 0;

for(int i=0; i<myArray.length;i++) { //loop track each diagonal element and the each element of the alloted row

rowSum += myArray[row][i]; //rowSum = rowSum + myArray[row][i] sum all the element of the row by keeping the alloted row constant and change the column of the row with i

diagSum +=myArray[i][i]; // diagSum = diagSum + myArray[i][i] sum all the diagonal element. myArray[i][i] = diagonal element

}

return rowSum == diagSum; // check condition:- return true if rowSum = diagSum

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote