public class Quiz_2DArray01 {

public static String listToString(int[] list) {
String s = "";
for (int n : list) {
s = s + String.format("%4d", n);
}
return s;
}

/*
evaluateBoard() returns a new 2D-board with the same dimensions as the input 2D-board
Each square of the new board will be assigned the sum of its surrounding neighbors.
If a square at row r and column c is represented as board[r][c],
squares to its left use c-1
squares to its right use c+1
squares above use r-1
square below use r+1
Squares not on the board's edges have 8 neighbors BUT
Corner squares have 3 neighbors, and Non-Corner edge squares have 5 neighbors.
Therefore, you will need to check that a square's neighbor actually exists.
You do this by making sure that r-1, c-1, r+1 and c+1 - when used - are valid indexes.
*/
public static int[][] evaluateBoard(int[][] board) {
int[][] list = new int[10][10];

int[][] top = new int[10][10];
int[][] bottom = new int[10][10];
int[][] left = new int[10][10];
int[][] right = new int[10][10];

int[][] topLeft = new int[10][10];
int[][] topRight = new int[10][10];
int[][] bottomLeft = new int[10][10];
int[][] bottomRight = new int[10][10];

for(int r=0; r<10; r++) {
for(int c=0; c<10; c++) {
// ************************************************************************
// top-left-corner
top[r][c] = top[r-1][c];
bottom[r][c] = bottom[r+1][c];
left[r][c] = left[r][c-1];
right[r][c] = right[r][c+1];

topLeft[r][c] = topLeft[r-1][c-1];
topRight[r][c] = topRight[r-1][c+1];
bottomLeft[r][c] = bottomLeft[r+1][c-1];
bottomRight[r][c] = bottomRight[r+1][c+1];

if(r == 0 && c == 0) {
list[r][c] = board[r+1][c+1] + board[r+1][c] + board[r][c+1];
}
if(r == 0 || c == 0) {
list[r][c] = board[r+1][c+1] + board[r+1][c] + board[r][c+1];
}
// top-right-corner
if(r == 0 && c == 9) {
list[r][c] = board[r][c-1] + board[r+1][c-1] + board[r+1][c];
}
// bottom-left-corner
if(r == 9 && c == 0) {
list[r][c] = board[r-1][c+1] + board[r-1][c] + board[r][c+1];
}
// bottom-right-corner
if(r == 9 && c == 9) {
list[r][c] = board[r-1][c-1] + board[r-1][c] + board[r][c-1];
}
// ************************************************************************
// top-side
if(r == 0 && c >= 1 && c <= 8) {
list[r][c] = board[r+1][c-1] + board[r+1][c+1] + board[r+1][c] +
board[r][c-1] + board[r][c+1];
}
// bottom-side
if(r == 9 && c >= 1 && c <= 8) {
list[r][c] = board[r][c-1] + board[r][c+1] + board[r-1][c-1] +
board[r-1][c+1] + board[r-1][c];
}
// left-side
if(c == 0 && r >= 1 && r <= 8) {
list[r][c] = board[r-1][c] + board[r+1][c] + board[r][c+1] +
board[r-1][c+1] + board[r+1][c+1];
}
// right-side
if(c == 9 && r >=1 && r <= 8) {
list[r][c] = board[r-1][c] + board[r+1][c] + board[r-1][c-1] +
board[r+1][c-1] + board[r][c-1];
}
// ************************************************************************
// inside
if(r >= 1 && r <= 8 && c >= 1 && c <= 8) {
list[r][c] = board[r-1][c-1] + board[r-1][c] + board[r-1][c+1] + board[r][c+1] +
board[r+1][c+1] + board[r+1][c] + board[r+1][c-1] + board[r][c-1];
}
}
}
return list;
}

/*
initializeRandomBoard() fills each square of the 2D-board
with a random integer in the range 1-10 inclusive.
*/
public static void initializeRandomBoard(int[][] board) {
for(int r=0; r < 10; r++) {
for(int c=0; c < 10; c++) {
int randomNumber = 1 + (int)(Math.random() * 10);
board[r][c] = randomNumber;
}
}

}

public static void main(String[] args) {
int[][] sampleBoard = {
{ 5, 4, 7, 6,10, 7, 3, 2, 4, 8 },
{ 1, 9, 2, 4, 6, 7, 8, 5, 7,10 },
{ 8, 4,10, 6, 2, 5, 3, 4, 7, 2 },
{ 7, 1, 6, 7, 7, 7, 7, 9, 7, 3 },
{ 3, 9, 5, 2, 4, 9, 5, 3,10, 9 },
{ 3, 5, 2, 4, 2, 7, 7, 1, 6, 6 },
{ 1, 2, 2, 7, 8, 3, 6, 3, 3, 7 },
{ 5, 6, 9, 2, 5, 2, 5, 8, 2, 6 },
{ 1, 4,10, 5, 4, 4, 9, 1, 6, 6 },
{ 5, 9, 8, 3, 5, 8, 2, 7, 7, 6 }
};

System.out.println("sampleBoard");
for (int[] row : sampleBoard) {
System.out.println( listToString(row) );
};
System.out.println();

System.out.println("sampleBoardEvaluated");
int[][] sampleBoardEvaluated = evaluateBoard(sampleBoard);
for (int[] row : sampleBoardEvaluated) {
System.out.println( listToString(row) );
};
System.out.println();
System.out.println();

/*****************/

int[][] randomBoard = new int[10][10];
initializeRandomBoard( randomBoard );
System.out.println("randomBoard");
for (int[] row : randomBoard) {
System.out.println( listToString(row) );
};
System.out.println();

int[][] randomBoardEvaluated = evaluateBoard(randomBoard);
System.out.println("randomBoardEvaluated");
for (int[] row : randomBoardEvaluated) {
System.out.println( listToString(row) );
};
}

}

I get the error saying Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10
at Quiz_2DArray01.evaluateBoard(Quiz_2DArray01.java:41)
at Quiz_2DArray01.main(Quiz_2DArray01.java:136)

Note I am using Eclipse
This is what the OUTPUT should look like.
sampleBoard
5 4 7 6 10 7 3 2 4 8
1 9 2 4 6 7 8 5 7 10
8 4 10 6 2 5 3 4 7 2
7 1 6 7 7 7 7 9 7 3
3 9 5 2 4 9 5 3 10 9
3 5 2 4 2 7 7 1 6 6
1 2 2 7 8 3 6 3 3 7
5 6 9 2 5 2 5 8 2 6
1 4 10 5 4 4 9 1 6 6
5 9 8 3 5 8 2 7 7 6

sampleBoardEvaluated
14 24 25 29 30 34 29 27 32 21
30 41 50 49 47 44 36 38 42 28
22 44 39 44 49 47 52 53 47 34
25 52 44 42 42 42 45 46 47 35
25 32 36 37 45 46 50 52 44 32
20 27 36 32 44 44 37 43 42 35
21 33 37 34 32 42 36 38 39 23
14 34 38 50 35 44 36 35 40 24
29 53 46 46 34 40 37 46 43 27
14 28 31 32 24 24 29 25 26 19

randomBoard (Note: the following will be different each run)
3 3 2 7 2 1 1 5 8 8
4 3 2 9 3 4 5 8 3 8
10 1 1 2 1 4 3 4 1 8
10 4 4 5 5 5 10 4 1 9
10 8 6 2 1 2 10 1 6 5
3 9 4 4 5 6 5 5 7 10
1 3 10 8 2 10 5 10 3 6
1 10 7 3 4 5 2 1 9 2
9 1 8 3 10 8 2 6 2 10
1 6 10 5 2 4 10 10 5 4

randomBoardEvaluated
10 14 24 18 24 15 23 25 32 19
20 26 28 20 30 20 30 30 50 28
22 38 30 30 37 36 44 35 45 22
33 50 29 22 22 36 33 36 38 21
34 50 40 34 34 47 38 48 42 33
31 45 50 38 35 40 49 47 46 27
26 45 48 39 45 34 44 37 50 31
24 40 46 52 49 43 47 39 40 30
19 52 45 49 34 39 46 41 47 22
16 29 23 33 30 32 30 25 32 17

The code you provided is giving an ArrayIndexOutOfBoundsException because it is trying to access elements in arrays using negative indexes. You need to make some adjustments in the evaluateBoard method to fix this issue.

Here are the steps to fix the issue:

1. In the evaluateBoard method, initialize the top, bottom, left, right, topLeft, topRight, bottomLeft, and bottomRight arrays with the same dimensions as the input board.

2. Inside the for loop, update the values of the top, bottom, left, and right arrays using the corresponding indexes for neighboring squares.

- For example, to update the top array, use: `top[r][c] = board[r-1][c];`.
- Make sure to add a check to avoid accessing invalid indexes. For example, if r == 0, you cannot access board[r-1][c] because it will give an ArrayIndexOutOfBoundsException.

3. Similarly, update the values of the topLeft, topRight, bottomLeft, and bottomRight arrays using the corresponding indexes for neighboring squares. Again, make sure to add checks to avoid accessing invalid indexes.

4. Update the conditions inside the if statements to correctly calculate the sum of surrounding neighbors for each square on the board. Use the values from the top, bottom, left, and right arrays as necessary.

5. Make sure to correctly assign the calculated sum to the list array.

Here is the corrected evaluateBoard method:

```java
public static int[][] evaluateBoard(int[][] board) {
int[][] list = new int[10][10];

int[][] top = new int[10][10];
int[][] bottom = new int[10][10];
int[][] left = new int[10][10];
int[][] right = new int[10][10];

int[][] topLeft = new int[10][10];
int[][] topRight = new int[10][10];
int[][] bottomLeft = new int[10][10];
int[][] bottomRight = new int[10][10];

for (int r = 0; r < 10; r++) {
for (int c = 0; c < 10; c++) {
if (r > 0) {
top[r][c] = board[r - 1][c];
if (c > 0) {
topLeft[r][c] = board[r - 1][c - 1];
}
if (c < 9) {
topRight[r][c] = board[r - 1][c + 1];
}
}
if (r < 9) {
bottom[r][c] = board[r + 1][c];
if (c > 0) {
bottomLeft[r][c] = board[r + 1][c - 1];
}
if (c < 9) {
bottomRight[r][c] = board[r + 1][c + 1];
}
}
if (c > 0) {
left[r][c] = board[r][c - 1];
}
if (c < 9) {
right[r][c] = board[r][c + 1];
}

int sum = 0;
// Corners
if (r == 0 && c == 0) {
sum = board[r + 1][c + 1] + board[r][c + 1] + board[r + 1][c];
} else if (r == 0 && c == 9) {
sum = board[r][c - 1] + board[r + 1][c - 1] + board[r + 1][c];
} else if (r == 9 && c == 0) {
sum = board[r - 1][c + 1] + board[r][c + 1] + board[r - 1][c];
} else if (r == 9 && c == 9) {
sum = board[r - 1][c - 1] + board[r - 1][c] + board[r][c - 1];
}
// Sides
else if (r == 0 || c == 0) {
sum = board[r + 1][c + 1] + board[r + 1][c] + board[r][c + 1];
if (c == 0) {
sum += top[r][c] + bottom[r][c];
}
} else if (r == 9 || c == 9) {
sum = board[r - 1][c - 1] + board[r - 1][c] + board[r][c - 1];
if (r == 9) {
sum += left[r][c] + right[r][c];
}
}
// Inside
else {
sum = topLeft[r][c] + top[r][c] + topRight[r][c] + left[r][c] +
right[r][c] + bottomLeft[r][c] + bottom[r][c] + bottomRight[r][c];
}

list[r][c] = sum;
}
}
return list;
}
```

After making these changes, the program should run without giving an ArrayIndexOutOfBoundsException error. Let me know if you need further assistance.