9.1.7 Checkerboard V2 | Answers
The fluorescent lights of the computer lab hummed, a soundtrack to the mounting panic of a dozen Intro to Java students. It was Friday, 3:45 PM. The deadline for the "Nested Loops" unit was looming like a storm cloud.
Common Errors and How to Fix Them
Even with the correct code, students often hit frustrating roadblocks. Here’s a quick troubleshooting table: 9.1.7 checkerboard v2 answers
- Using nested loops (rows and columns)
- Determining square color based on
(row + column) % 2 == 0 - Drawing with a graphics library or printing with characters (
XandO, or#and)
Key Concepts You Must Understand
- Nested Loops: An outer loop for rows, an inner loop for columns.
- Modulus Operator (
%): Used to decide the color.(row + column) % 2 == 0is the classic checkerboard formula. - Coordinates: Each square’s
(x, y)position is calculated by multiplying the loop counter by the square width/height. - GRect: The
GRectclass from the ACM graphics library is used to create rectangles.
| Error Message / Symptom | Likely Cause | Solution |
|-------------------------|--------------|----------|
| Square is not filled | Missing setFilled(true) | Add the line before setting the color. |
| All squares are the same color | Incorrect modulus logic | Use (row + col) % 2 == 0. Check your starting color. |
| ArrayIndexOutOfBoundsException | Using <= instead of < in loop | Ensure loops run 0 to 7 (not 0 to 8). |
| Nothing appears on screen | Forgot to call add(square) | After creating and coloring, call add(square). |
| Squares overlap or have gaps | Incorrect coordinate math | x = col * size, y = row * size. Don’t add extra offset. |
| Autograder fails on style | Missing constants or comments | Use private static final int for magic numbers (8, 50). | The fluorescent lights of the computer lab hummed,