JAVA Matrix 8. Write a method called sumCorners that computes the sum of the fou
ID: 3877420 • Letter: J
Question
JAVA
Matrix
8. Write a method called sumCorners that computes the sum of the four corners of a given matrix.
9. Write a method called diagonalsSum that computes the sum of the main diagonals (the values that create
an X shape in a square matrix).
10. Write a method called isIdentity that determines whether the given matrix is an identity matrix. An
identity matrix is defined by:
a. A square matrix.
b. All the elements of the principal diagonal are ones.
c. All other elements are zeros.
11. Write a method called interiorSum that computes the sum of all elements not in the first and last row as
well as the first and last column.
~~~~~~~~~~~~~~~ Part II: Test Matrix method ~~~~~~~~~~~~~~~~~~~~`
~~~~~~~~~Step #8: Test 'sumCorners' method
Matrix has:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
After performing 'sumCorners', result is: 42
SKELETON CODE
public class Matrix {
// Computes the sum of the four corners of the matrix
public static int sumCorners(int[][] m) {
int result = 0;
// Single row / column matrix
// Single row / multi-column matrix
// Multi-row / single-column matrix
// General matrix
return result;
}
public static boolean isIdentity(int[][] m) {
//to be completed
return true;
}
public static int interiorSum(int[][] m) {
int sum = 0;
//to be completed
return sum;
}
public static int diagonalsSum(int[][] m) {
int sum = 0;
//to be completed
return sum;
}
public static String printMatrix(int[][] mx) {
String result = "";
for (int i = 0; i < mx.length; i++) {
result += " ";
for (int j = 0; j < mx[0].length; j++) {
result += mx[i][j] + " ";
}
}
return result;
}
}
Explanation / Answer
package A1Q3;
public class Matrix {
// Computes the sum of the four corners of the matrix
public static int sumCorners(int[][] m) {
if(m.length==1 && m[0].length==1)
return m[0][0];
else if(m.length==1 && m[0].length>1)
return m[0][0] + m[0][m[0].length-1];
if(m.length>1 && m[0].length==1)
return m[0][0] + m[m.length-1][0];
else
return m[0][0] + m[0][m[0].length-1] + m[0][0] + m[0][m[0].length-1];
}
public static boolean isIdentity(int[][] m) {
boolean flag=false;
for(int i=0;i<m.length;i++)
{
for(int j=0;j<m[i].length;j++)
{
if((i==j&&m[i][j]!=1) || (i!=j&&m[i][j]!=0))
{
flag=true;
break;
}
}
if(flag)
break;
}
return flag;
}
public static int interiorSum(int[][] m) {
int sum = 0;
for(int i=0;i<m.length;i++)
{
for(int j=0;j<m[i].length;j++)
{
sum = sum + m[i][j];
}
}
return sum - sumCorners(m) ;
}
public static int diagonalsSum(int[][] m) {
int sum = 0;
//to be completed
for(int i=0;i<m.length;++i){
for(int j=0;j<m[i].length;++j){
if(i==j)
sum = sum + m[i][j];
}
}
return sum;
}
public static String printMatrix(int[][] mx) {
String result = "";
for (int i = 0; i < mx.length; i++) {
result += " ";
for (int j = 0; j < mx[0].length; j++) {
result += mx[i][j] + " ";
}
}
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.