I have my code, but when I run the Junit test it shows 1 failure. I just need to
ID: 3915056 • Letter: I
Question
I have my code, but when I run the Junit test it shows 1 failure. I just need to know which one is it.
////////////Here is the Code/////////////////////
public class Lab10{
public static void main (String[] args) {
}
public static void addTo10(int [][] array) {
for(int i=0; i<array.length; i++) {
// to find the index of value 0 in the row
int zeroindex = -1;
// to count sum of the row
int rowSum = 0;
// move in the complete row
for(int j=0; j<array[i].length; j++) {
rowSum += array[i][j];
// check if this entry is 0, store its index
if(array[i][j] == 0) {
zeroindex = j;
}
}
// if there is any 0 in the row, store the value in such
// a manner, that the complete row sum is 10
if(zeroindex != -1) {
array[i][zeroindex] = 10 - rowSum;
}
}
}
public static void setHints(int [][] array) {
for(int i=0; i<array.length; i++) {
// move in the complete row
for(int j=0; j<array[i].length; j++) {
// if it is a mine
if(array[i][j] == -1) {
// check upper row
if(i-1 >= 0) {
if(array[i-1][j] != -1) {
array[i-1][j] += 1;
}
if((j-1 >= 0) && (array[i-1][j-1] != -1)) {
array[i-1][j-1] += 1;
}
if((j+1 < array[i-1].length) && (array[i-1][j+1] != -1)) {
array[i-1][j+1] += 1;
}
}
// check bottom row
if(i+1 < array.length) {
if(array[i+1][j] != -1) {
array[i+1][j] += 1;
}
if((j-1 >= 0) && (array[i+1][j-1] != -1)) {
array[i+1][j-1] += 1;
}
if((j+1 < array[i+1].length) && (array[i+1][j+1] != -1)) {
array[i+1][j+1] += 1;
}
}
// check current row left
if((j-1 >= 0) && (array[i][j-1] != -1)) {
array[i][j-1] += 1;
}
// check current row right
if((j+1 < array[i].length) && (array[i][j+1] != -1)) {
array[i][j+1] += 1;
}
}
}
}
}
}
////////////////Junit Test Lab10Test///////////////
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class Lab10Test {
@Test
public void testIsEmpty() {
int[][] actual = {};
int[][] expected = {};
Lab10.addTo10(actual);
assertArrayEquals("Incorrect result", expected, actual);
}
@Test
public void testOneRow() {
int[][] actual = { { 1, 3, 0, 1 } };
int[][] expected = { { 1, 3, 5, 1 } };
Lab10.addTo10(actual);
assertArrayEquals("Incorrect result", expected, actual);
}
@Test
public void testSeveralRows1() {
int[][] actual = { { 0, 1, 2, 3, 4 }, { 1, 1, 0, 1, 1 }, { 5, 3, 1, 2, 0 } };
int[][] expected = { { 0, 1, 2, 3, 4 }, { 1, 1, 6, 1, 1 }, { 5, 3, 1, 2, -1 } };
Lab10.addTo10(actual);
assertArrayEquals("Incorrect result", expected, actual);
}
@Test
public void testSeveralRows2() {
int[][] actual = { { 0, 1 }, { 9, 0 }, { 0, 19 }, { -1, 0 } };
int[][] expected = { { 9, 1 }, { 9, 1 }, { -9, 19 }, { -1, 11 } };
Lab10.addTo10(actual);
assertArrayEquals("Incorrect result", expected, actual);
}
@Test
public void testSeveralRowsOfVariousLengths() {
// Hint: Use the number of elements on each row independently!
int[][] actual = { { 0, 1, 1, 1 }, { 2, 0 }, { 3, 3, 0, 3, 3 } };
int[][] expected = { { 7, 1, 1, 1 }, { 2, 8 }, { 3, 3, -2, 3, 3 } };
Lab10.addTo10(actual);
assertArrayEquals("Incorrect result", expected, actual);
}
@Test
public void testSetHintNumbersOneMine() {
int[][] actual = { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, -1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
int[][] expected = { { 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0 }, { 0, 1, -1, 1, 0, 0 }, { 0, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
Lab10.setHints(actual);
assertArrayEquals("Incorrect result", expected, actual);
}
@Test
public void testSetHintNumbersTwoMinesAwayFromThemselvesAndBoundaries() {
int[][] array = { { 0, 0, 0, 0, 0, 0 }, { 0, -1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, -1, 0 }, { 0, 0, 0, 0, 0, 0 } };
int[][] expected = { { 1, 1, 1, 0, 0, 0 }, { 1, -1, 1, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0 }, { 0, 0, 0, 1, 1, 1 },
{ 0, 0, 0, 1, -1, 1 }, { 0, 0, 0, 1, 1, 1 } };
Lab10.setHints(array);
assertArrayEquals("Incorrect result", expected, array);
}
@Test
public void testSetHintNumbersThreeMinesCloseToThemselvesAndBoundaries() {
int[][] array = { { 0, -1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { -1, 0, 0, 0, 0, 0 }, { -1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
int[][] expected = { { 1, -1, 1, 0, 0, 0 }, { 2, 2, 1, 0, 0, 0 }, { -1, 2, 0, 0, 0, 0 }, { -1, 2, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
Lab10.setHints(array);
assertArrayEquals("Incorrect result", expected, array);
}
@Test
public void testSetHintNumbersSeveralMines() {
int[][] array = { { -1, 0, 0, 0, 0, -1 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, -1, -1, 0, 0 }, { 0, 0, -1, -1, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }, { -1, 0, 0, 0, 0, -1 } };
int[][] expected = { { -1, 1, 0, 0, 1, -1 }, { 1, 2, 2, 2, 2, 1 }, { 0, 2, -1, -1, 2, 0 },
{ 0, 2, -1, -1, 2, 0 }, { 1, 2, 2, 2, 2, 1 }, { -1, 1, 0, 0, 1, -1 } };
assertArrayEquals("Incorrect result", expected, array);
}
}
Explanation / Answer
Explanation:
In the below code, you missed the method call of setHints.
Fix it and it will remove the failure of the test case.
Edit of the failure test:
@Test
public void testSetHintNumbersSeveralMines()
{
int[][] array = { { -1, 0, 0, 0, 0, -1 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, -1, -1, 0, 0 }, { 0, 0, -1, -1, 0, 0 },{ 0, 0, 0, 0, 0, 0 }, { -1, 0, 0, 0, 0, -1 } };
int[][] expected = { { -1, 1, 0, 0, 1, -1 }, { 1, 2, 2, 2, 2, 1 }, { 0, 2, -1, -1, 2, 0 },{ 0, 2, -1, -1, 2, 0 }, { 1, 2, 2, 2, 2, 1 }, { -1, 1, 0, 0, 1, -1 } };
setHints(array);
assertArrayEquals("Incorrect result", expected, array);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.