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

JAVA: public class Problem2 { //declarations/initializations int[][] myArray; //

ID: 3699175 • Letter: J

Question

JAVA:

public class Problem2 {

//declarations/initializations
int[][] myArray;

//--------------------------------------
//Constructor: Initialize myArray with the
//given values of 1-9.
//--------------------------------------
public Problem2()
{
myArray = new int[3][3];

}
//--------------------------------------
//return the sum of all elements by rows
//--------------------------------------
public int sumAllElementsByRows()
{

}   

//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------

public int[] sumAllElementsByColumns()
{   

}
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------

public int sumAllReversedDiag()
{
}

public static void main(String[] args)
{
Problem2 myProblem2 = new Problem2();
System.out.println("Sum by rows: "
+ myProblem2.sumAllElementsByRows());
System.out.println("Sum by columns: "
+ myProblem2.sumAllElementsByColumns());
System.out.println("Sum reversed diagonal elements: "
+ myProblem2.sumAllReversedDiag());
} // end main
} // end class

2. For this problem you need to write 3 methods that take integer typed two-dimensional array and return the following quantities: (20 points) Sum of all elements in the array by row Sum of all elements in the array by column Sum of reversed diagonal elements You also need to write a constructor to initialize myArray with the given values in the table below. You can assume that array has equal number of columns and rows (i.e. squared) Example: For the two-dimensional array:

Explanation / Answer

Problem2.java

import java.util.Arrays;

public class Problem2 {

//declarations/initializations

int[][] myArray;

//--------------------------------------

//Constructor: Initialize myArray with the

//given values of 1-9.

//--------------------------------------

public Problem2()

{

myArray = new int[3][3];

for(int i=0,k=1;i<myArray.length;i++) {

for(int j=0;j<myArray.length;j++,k++) {

myArray[i][j] = k;

}

}

}

//--------------------------------------

//return the sum of all elements by rows

//--------------------------------------

public int sumAllElementsByRows()

{

int sum = 0;

for(int i=0,k=1;i<myArray.length;i++) {

for(int j=0;j<myArray.length;j++,k++) {

sum+=myArray[i][j];

}

}

return sum;

}   

//--------------------------------------

//return the sum of the values in the given column

//--------------------------------------

public int[] sumAllElementsByColumns()

{   

int col[] = new int[myArray.length];

for(int i=0;i<myArray.length;i++) {

int sum = 0;

for(int j=0;j<myArray.length;j++) {

sum+=myArray[j][i];

}

col[i]=sum;

}

return col;

}

//--------------------------------------

//return the sum of the values in the other ("reverse") diagonal

//--------------------------------------

public int sumAllReversedDiag()

{

int sum = 0;

for(int i=0;i<myArray.length;i++) {

sum+=myArray[myArray.length-1-i][i];

}

return sum;

}

public static void main(String[] args)

{

Problem2 myProblem2 = new Problem2();

System.out.println("Sum by rows: "

+ myProblem2.sumAllElementsByRows());

System.out.println("Sum by columns: "

+ Arrays.toString(myProblem2.sumAllElementsByColumns()));

System.out.println("Sum reversed diagonal elements: "

+ myProblem2.sumAllReversedDiag());

} // end main

} // end class

Output:

Sum by rows: 45
Sum by columns: [12, 15, 18]
Sum reversed diagonal elements: 15