1
class Board {
    Direction[][] board;
    Integer width;
    Integer height;

    Board(Integer width, Integer height) {
        this.width = width;
        this.height = height;

        board = new Direction[width][height];

        for (Integer y = 0; y < height; y++) {
            for (Integer x = 0; x < width; x++) {
                board[x][y] = Direction.Down;
            }
        }   
    }

    Direction direction(Integer x, Integer y) {
        return board[x][y];
    }

    void update(Integer x, Integer y) {
        board[x][y].next();
    }
}

The enumerated values in Direction are: Up or Down

Firstly, I want to initialise my board so that all the initial values at every place of the board are set to Down.

Secondly, I want to retrieve the value at a particular (x,y) position of of the board

Thirdly, I want to be able to update the value at that particular element of the board. The next() function is designed to change the value from Down to Up or vice versa. This is declared separately.

I believe I am doing something wrong as the code compiles, however my program doesn't work as it should.

9
  • "however my program doesn't work as it should." Could you specify? Commented Jun 27, 2011 at 0:23
  • Your "board" does not initialize correctly? Commented Jun 27, 2011 at 0:25
  • @toto Basically I get a blank screen with the relative background color to Down defined elsewhere, when I try to run the program. Nothing else occurs. Similarly, If I set to Direction.Up above, the other colour appears but nothing happens. Commented Jun 27, 2011 at 0:26
  • 1
    You need to be more specific about what is happening because from your description this sounds like a UI problem. Which implies that the code is not relevant to the problem. Commented Jun 27, 2011 at 0:29
  • 2
    Is there a reason you are using int instead of Integer? See this post that describes the difference between primitive types and reference types. Commented Jun 27, 2011 at 0:47

2 Answers 2

3

If the problem is your update method:

Values of an enum type should be immutable. Calling the next() method on it could change the enum object (depending on its implementation), but as you are using the same object in all of your array positions, all of those would change, too.

You might want to use something like this:

board[x][y] = board[x][y].next();

with

enum Direction {

    UP, DOWN;

    public Direction next() {
        if (this == UP)
           return DOWN;
        else
           return UP;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

or even simpler: return this == UP ? DOWN : UP;
@Bohemian I've got similar to what @Paŭlo Ebermann has, except it doesn't say public at the beginning. Would that make any difference?
@maclunian: The problem is not the next method, but how you call it. You need to use the result somehow, by assigning it to the right position of the board array.
1

Most likely, you want to change your update code to be:

board[x][y] = board[x][y].next();

Of course, I'm assuming a lot about what's not working with your code, but it's a common mistake I've seen before.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.