Instructions
Requirements and Specifications
Description : public class Matrix {
static boolean checkMatrix(int [][]m) {
Complete this method based on the screenshot instructions
The J Unit test is performed on the following four criteria:
import static org.junit.Assert.*;
import org.junit.Test;
public class MatrixTest {
@Test
public void testBasic() {
int[][] in1 = { {1, 5, 9}, {8, 3, 4}, {6, 7, 2} };
int [][] in2 = { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} };
int [][] in3 = {{11, 18, 25, 2, 9}, {10, 12, 19, 21, 3}, {4, 6, 13, 20, 22}, {23, 5, 7, 14, 16}, {17, 24, 1, 8, 15}
};
int[][] in4 = { {1, 5, 9}, {3, 8, 4}, {6, 7, 2} };
int[][] in5 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
assertTrue(Matrix.checkMatrix(in1));
assertTrue(Matrix.checkMatrix(in2));
assertTrue(Matrix.checkMatrix(in3));
assertFalse(Matrix.checkMatrix(in4));
assertFalse(Matrix.checkMatrix(in5));
}
@Test
public void testDimensions() {
int[][] in1 = { {1, 5, 9}, {8, 3, 4}, {6, 7, 2}, {0, 0, 0} };
int[][] in2 = { {1, 5, 9, 0}, {8, 3, 4, 0}, {6, 7, 2, 0} };
int[][] in3 = { {1, 5, 9}, {8, 3, 4, 0}, {6, 7, 2} };
assertFalse(Matrix.checkMatrix(in1));
assertFalse(Matrix.checkMatrix(in2));
assertFalse(Matrix.checkMatrix(in3));
}
@Test
public void testUnique() {
int[][] in2 = { {1, 2, 3}, {3, 1, 2}, {2, 3, 1} };
assertFalse(Matrix.checkMatrix(in2));
}
@Test
public void testDomain() {
int[][] in2 = { {2, 6, 10}, {9, 4, 5}, {7, 8, 3} };
assertFalse(Matrix.checkMatrix(in2));
}
}
Screenshots of output
Source Code
public class Matrix {
static boolean checkMatrix(int [][]m) {
// check nxn dimensions
for (int i = 0; i < m.length; i++)
if (m[i].length != m.length)
return false;
int n2 = m.length*m.length;
// check 0 < mij <= n^2
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m.length; j++)
if (m[i][j] <= 0 || m[i][j] > n2)
return false;
// check no duplicated values
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m.length; j++) {
for (int k = 0; k < m.length; k++)
for (int l = 0; l < m.length; l++)
if (i != k && j != l && m[i][j] == m[k][l])
return false;
}
// check row sums are equal
int prevsum = -1;
for (int i = 0; i < m.length; i++) {
int sum = 0;
for (int j = 0; j < m.length; j++)
sum += m[i][j];
if (prevsum == -1)
prevsum = sum;
else if (sum != prevsum)
return false;
}
// check col sums are equal
prevsum = -1;
for (int j = 0; j < m.length; j++) {
int sum = 0;
for (int i = 0; i < m.length; i++)
sum += m[i][j];
if (prevsum == -1)
prevsum = sum;
else if (sum != prevsum)
return false;
}
return true;
}
}