Codehs: 9.1.6 Checkerboard V1

import acm.graphics.*; import acm.program.*; import java.awt.*; public class CheckerboardV1 extends GraphicsProgram {

Copy the code above, paste it into the CodeHS editor, and run it. You should see a perfect 8×8 checkerboard. If you run into issues, double-check your spelling of Color.GRAY (remember: American English spelling) and ensure you have imported acm.graphics.* and java.awt.* . 9.1.6 checkerboard v1 codehs

// Define constants for better readability private static final int ROWS = 8; private static final int COLUMNS = 8; private static final int SQUARE_SIZE = 50; private static final int WINDOW_WIDTH = COLUMNS * SQUARE_SIZE; // 400 private static final int WINDOW_HEIGHT = ROWS * SQUARE_SIZE; // 400 import acm

public void run() { // Set the canvas size setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Loop through each row for (int row = 0; row < ROWS; row++) { // Loop through each column in the current row for (int col = 0; col < COLUMNS; col++) { // Calculate the top-left corner of the square int x = col * SQUARE_SIZE; int y = row * SQUARE_SIZE; // Create the square (rectangle) GRect square = new GRect(x, y, SQUARE_SIZE, SQUARE_SIZE); // Determine the color based on parity if ((row + col) % 2 == 0) { square.setFillColor(Color.GRAY); } else { square.setFillColor(Color.BLACK); } // Make sure the square is filled with the color square.setFilled(true); // Add the square to the canvas add(square); } } } } Some versions of the CodeHS exercise use red instead of gray. If your prompt says "red and black", simply change the color in the conditional: // Define constants for better readability private static

Happy coding!

If you are working through the CodeHS Java course (specifically the "9.1.6 Checkerboard v1" problem), you have likely encountered a classic programming challenge: creating a checkerboard pattern. This exercise is a rite of passage for learning nested loops, conditional logic, and graphical object placement.