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
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.