-2

I have been surfing StackOverflow for about an hour looking for an answer to this really easy question, but it seems that none apply to this specific circumstance.

import java.awt.Color;
public class Question15 {

    public void fillCheckerBoard(Color[][] board){

        for(int n = 0; n < board.length; n++){

            for(int k = 0; k < board[0].length; k++){

                if((k%2==0 && n%2 ==0)||(k%2==1 && n%2 ==1)){
                    board[n][k] = Color.black;
                }
                else{
                    board[n][k] = Color.white;
                }

                if(board[k][n] == Color.black){
                    System.out.print("x");
                }
                else
                    System.out.print(" ");
            }
        }


    }

    public static void main(String[] args) {


        Color [][] a = new Color [4][5];
        Question15 b = new Question15(); 
        b.fillCheckerBoard(a);
        System.out.print(b);
    }

}

The method createCheckerBoard takes in a Color [][] array and creates a checkerboard to the dimensions specified within the 2D array.

In the main method I have created a 2D Color array called "a", and a new object called "b". I want to test out the fillCheckerBoard method out, using "a" as the input. Once "a" has been modified, I want to print "a" out to see if my fillCheckerBoard works. I made a Question15 object because as far as I know a void method needs an object in order to work.

What I have done in the void method only returns an error when I try to run the program. How can I test if my method can actually print out a checker board?

Ellen
  • 5
  • 1
  • 4
  • I don't understand the question. What are you expecting to happen, and what's actually happening? – shmosel Jan 27 '17 at 03:52
  • Did you just try to run your application? This code will compile and run, but will throw an ArrayIndexOutOfBoundsException due to 2 bugs (one is in the second loop, second is in checking board color when printing). – Ivan Gammel Jan 27 '17 at 03:54
  • 1
    Possible duplicate of [Java - printing two dimensional array](http://stackoverflow.com/questions/7782080/java-printing-two-dimensional-array)... also that does not cover other issues with the code and may not print exactly what you want... – Alexei Levenkov Jan 27 '17 at 03:55
  • You should add `System.out.println();` after the inner loop (that is, as the last line of the outer loop), if you want this to look like a board; otherwise, it will all just print as one long line. Also, change `board[k][n]` to `board[n][k]` in the second `if`, to avoid the `ArrayIndexOutOfBoundsException`. – Dawood ibn Kareem Jan 27 '17 at 04:01

3 Answers3

1

First of all your question is quite vague. But as far as I can understand you need to create a checkerboard and fill it with X's and O's. Now in order to create and display a checkerboard you will need Java applet class. one of the features of Java Applet class is that it does not require a main method. The following code creates a checker board according to the input.

import java.applet.Applet;
import java.awt.Color;

import java.awt.Graphics;

public class Checkerboard extends Applet {

private static final long serialVersionUID = 1L;

    public void paint(Graphics g) {

        int row;
        int col;
        int x, y;

        for (row = 0; row < 4; row++) {

            for (col = 0; col < 5; col++) {
                x = col * 40;
                y = row * 40;
                if ((row % 2) == (col % 2)) {
                    g.setColor(Color.white);
                } else {
                    g.setColor(Color.black);
                }
                g.fillRect(x, y, 40, 40);
            }

        }

    }
}

Or if you wish to use a main method to print your output then you wont be needing the Color class. you can just do it passing an ordinary 2 dim array as an argument. I have'nt modified your code much but please go through the following code and see what is it that you require.

import java.awt.Color;

public class Question15 {

    public void fillCheckerBoard(Color[][] board) {

        for (int row = 0; row < board.length; row++) {

            for (int column = 0; column < board[0].length; column++) {

                if ((row % 2) == (column % 2)) {
                    board[row][column] = Color.black;
                } else {
                    board[row][column] = Color.white;
                }

                if (board[row][column] == Color.black) {
                    System.out.print("X ");
                } else
                    System.out.print("O ");
            }
            System.out.println("");
        }

    }

    public static void main(String[] args) {

        Color[][] a = new Color[4][5];
        Question15 b = new Question15();
        b.fillCheckerBoard(a);
    }
}
0

Given that your method is altering the argument a passed in, you probably want to be printing that out instead. Try System.out.println(Arrays.deepToString(a)) instead of System.out.println(b)

Mshnik
  • 7,032
  • 1
  • 25
  • 38
  • It doesn't actually alter variable `a` does it? It passes `a`, but never returns anything. The variable altered would be the parameter `board` - which isn't returned. – ChickenFeet Jan 27 '17 at 03:57
  • well.. it is altering `a`, as `a` and `board` are two names for the same array in memory. Though guessing from your question, that's more a complicated concept than you want/need. – Mshnik Jan 27 '17 at 04:00
  • I thought java didn't pass by reference, rather by value. So there should two different blocks of memory for these two variables, right? – ChickenFeet Jan 27 '17 at 04:02
  • 1
    It doesn't change the value of the _variable_ `a`, but it does modify the object that `a` references. – Dawood ibn Kareem Jan 27 '17 at 04:03
  • 1
    You're correct, it's pass by value, but mutable object types (such as arrays) are still mutable. If you wrote `board = null` in `fillCheckerBoard(..)`, `a` would not be null, but changes to `board` will appear as changes to `a`. – Mshnik Jan 27 '17 at 04:04
  • (and btw, making changes to your mutable parameters is a really bad practice unless that's the specific point of the method) – Mshnik Jan 27 '17 at 04:04
-2

The below line is throwing ArrayIndexOutOfBoundException since you have only 4 rows in Color array and this line tries to access 5th row of color array

if(board[k][n] == Color.black){

May be there is some issue in the logic.

Ankit
  • 2,753
  • 1
  • 19
  • 26