Question: 1) Write to class Debigging, a method setHints (below) that accepts a 2 dimensional array of integers containing -1s and 0s and modifies it according
1) Write to class "Debigging", a method "setHints" (below) that accepts a 2 dimensional array of integers containing -1s and 0s and modifies it according to the following (minesweeper) criteria: to hint of its presence, for each mine (a value -1) increment by 1 the value of ll its surrounding squares; if another mine exists in one of these squares, its value is left unchanged. public static void setHints( int [] [] array) { //your code here } the figure below ilustrates the state of an array at the begginig of the method (left) and at the end of the method (right) after all the hint values have been added.
Rather, the provided debugging class attempts to solve the problem when you ran the test cases you will notice none of them pass. Debugging.java contains three bugs within its source code preventing the correct answer from being achieved. The objective of this lab is to use the debugger provided in eclipse along with the information provided by the test cases to detect and correct these errors. when you discover and correct an error, you must insert a comment describing the error and when did you correct the error. when you have corrected all the bugs in Debugging.java and all of the given test cases pass, upload your corrected Debugging.java file You must correct the provided Debugging.java class. you cannot create your own solution to given problem.
Debugging.java below:
______________________________________________________________
public class Debugging{
public static void setHints(int[][] array){
//Iterate over every element in our 2D Array
for(int i = 0; i < array.length + 1; i++) {
for(int j = 0; j < array.length + 1; j++) {
//If we find a -1 element,
if(array[i][j] == -1) {
//Set our row modifiers to -1. (Because we want every surrounding element. So we'll start with the element that is a row back
// and a column back from the -1 element)
int rowModifier = -1;
int columnModifier = -1;
//Iterate while the row modifiers are less than or equal to 1. We want to check all elements surrounding our -1. If -1 was
//at [3][3] in the array, this loop will allow us to check: [2][2],[2][3],[2][4],[3][2],[3][4],[4][2],[4][3],[4][4]
while(rowModifier <= 1 && columnModifier <= 1) {
//Add the row and column modifiers to their corresponding i and j values.
int rowIndex = i + rowModifier;
int columnIndex = j + columnModifier;
//Now that we have an index to modify, we check if the index is valid / if it is our -1. If it's not valid or it's our
//-1 we do nothing. Otherwise, we increment the value of element by 1
if(!(rowModifier == 0 && columnModifier == 0) && (isValidIndex(rowIndex,columnIndex,array) == true)) {
array[rowIndex][columnIndex] = 1;
}
//If the column modifier is less than 1 increment it, else, set the columnModifier to -1 and increment the rowModifier
if(columnModifier < 1) {
columnModifier++;
}
else {
columnModifier = -1;
rowModifier++;
}
}
}
}
}
}
//Check to see if the given index is valid. We check to make sure the given index is within the bounds of the given 2D array
//We also check if the given index is a -1. If it is we assert the index is invalid
public static boolean isValidIndex(int rowIndex, int columnIndex, int[][] anArray) {
boolean toReturn = false;
//Check if either the row or column index is less than zero (Can't be valid if that's true)
if((rowIndex < 0) || (columnIndex < 0)){
toReturn = false;
}
//Else, check if the row is greater than or equal to the length of the array. (If it is, it would cause an array out of bounds exception)
else if(rowIndex >= anArray.length) {
toReturn = false;
}
//Else, check if the column is greater than or equal to the length of the row in the array. (Besides causing an array out of bounds
// exception, we check these two separately so that if the rowIndex is out of bounds, we don't try and use it to get a column because THAT
// would cause an array out of bounds exception as well)
else if(columnIndex >= anArray[rowIndex].length) {
toReturn = false;
}
//Else check if the element at the given index is equal to -1.
else if(anArray[rowIndex][columnIndex] == -1) {
toReturn = false;
}
//Return our result. If none of the above conditions are met, this should return true. If any of the above conditions are met,
//this should return false
return toReturn;
}
} _____________________________________________________
DebuggingTest.java below:
__________________________________________________
import static org.junit.Assert.*;
import org.junit.Test;
public class DebuggingTest {
@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 } }; Debugging.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 } }; Debugging.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 } }; Debugging.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 } }; Debugging.setHints( array ); assertArrayEquals( "Incorrect result", expected, array ); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
