Write a program named CheckerBoard.java. The checkerboard class will create and
ID: 3855521 • Letter: W
Question
Write a program named CheckerBoard.java. The checkerboard class will create and display a checkerboard using a two-dimensional array. The first dimension will be the X axis and the second dimension will be the Y axis. Display the board to the console by printing the letters “B” and “W” every other one, indicating black and white locations on the board. Below is an example of the expected output(ONLY THE LETTERS NOT ACTUAL COLORS):
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
Explanation / Answer
CheckerBoard.java
public class CheckerBoard {
public static void main(String[] args) {
//Declaring two dimensional array of type character
char check[][] = new char[8][8];
//Populating the 2-Dimensional array with 'B' or 'W' characters
for (int i = 1; i <= check.length; i++) {
for (int j = 1; j <= check[0].length; j++) {
if (i % 2 != 0) {
if (j % 2 != 0) {
check[i - 1][j - 1] = 'B';
} else {
check[i - 1][j - 1] = 'W';
}
} else {
if (j % 2 == 0) {
check[i - 1][j - 1] = 'B';
} else {
check[i - 1][j - 1] = 'W';
}
}
}
}
//Displaying the 2-dimensional array
for (int i = 1; i <= check.length; i++) {
for (int j = 1; j <= check[0].length; j++) {
System.out.printf("%c ", check[i - 1][j - 1]);
}
System.out.println(" ");
}
}
}
__________________
Output:
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.