1

i'm a student who just started learning java in college. So in today's lesson my teacher gave me some assignments, including one that i must write a program, using the StdIn.Draw library, that when you type in a number N, it prints out an NxN chessboard with height and width N also. I decided to use an NxN array and set certain elements into a value (e.g true) and others into another value. After that, i used the library to draw the chessboard with respective to the array i just created. It looks like this:

class chessboard {
 public static void main(String[] args) {
    int N = StdIn.readInt();
    boolean[][] hi = new boolean[N][N];
    double r = 1 / N;
    int x = 0;
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){

            if ((int)(i+j)%2 == 0)
                hi[i][j] = true;
            else {
                hi[i][j] = false;
            }
        }
    }
    StdDraw.setXscale(0.0, 5.0);
    StdDraw.setYscale(0.0, 5.0);
    int i = 0;
    int j = 0;
    double a = 1.0;
    double b = 1.0;
    while ((a < N) && (i < N)){
        while ((b < N) && (j < N)){
            if (hi[i][j] != true){
                StdDraw.setPenColor(StdDraw.BLACK);
                StdDraw.filledSquare(a ,b, r);
            }
            else {
                StdDraw.setPenColor(StdDraw.RED);
                StdDraw.filledSquare(a ,b, r);
            }
            b = b + r;
            j++;
        }
        b = 1.0;//reset b after the inner while loop
        a = a + r;
        i++;
    }
 }
}

When i test run it, it just prints out a white blank screen, nothing else. I couldn't find anything wrong with my algorithm, and i have tried to adjust the size of the initial a and b coordinate and the X and Y scale many times, but it just doesn't work. I tried to ask my friends about it, but they don't know either.

This is my screenshot of the problem:

enter image description here

Sorry if my English is bad, for i'm not a native speaker. And please if you could answer then keep it simple for me because i just started learning to code. Thank you.

Oh another thing. I'm studying based on this book: http://introcs.cs.princeton.edu/java/home/

BackSlash
  • 21,927
  • 22
  • 96
  • 136

2 Answers2

2

This uses integer division as both operands are int.

double r = 1 / N; // = 0; !!

So cast an operand, or change it to:

double r = 1.0 / N;

Also for the inner while-loop initialize the counter there, you did a reset of b, but not of j after the while loop. With a for-loop one could write:

double a = 1.0;
for (int i = 0; i < N; ++i) {
    double b = 1.0;
    for (int j = 0; j < N; ++j) {
        StdDraw.setPenColor(hi[i][j] ? StdDraw.RED: StdDraw.BLACK);
        StdDraw.filledSquare(a ,b, r);
        b += r;
    }
    a += r;
}

Not sure about the positioning.

BTW

        if ((int)(i+j)%2 == 0)
            hi[i][j] = true;
        else {
            hi[i][j] = false;
        }

becomes

        hi[i][j] = (i + j) % 2 == 0;

And

       if (hi[i][j] != true){
       // Better:
       //if (!hi[i][j]) {
            StdDraw.setPenColor(StdDraw.BLACK);
            StdDraw.filledSquare(a ,b, r);
        }
        else {
            StdDraw.setPenColor(StdDraw.RED);
            StdDraw.filledSquare(a ,b, r);
        }

becomes

        StdDraw.setPenColor(hi[i][j] ? StdDraw.RED : StdDraw.BLACK);
        StdDraw.filledSquare(a ,b, r);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thank you! The program printed out something, but it is not what i expected: http://i.imgur.com/AG0Fo9Z.png why the upper last 2 block are always the same colour? – marcofleuranza Sep 28 '16 at 09:20
  • I found that j was not reset after the loop; changed the answer with a for-loop. – Joop Eggen Sep 28 '16 at 09:37
0

There is a problem with the line

double r = 1 / N;

Because 1 and N are both integers, Java will perform integer division, which means it will keep the integer part of the division result and discard any remainder.

You can have Java perform floating point division instead by making the numerator a double:

double r = 1.0d / N;

Reference: Integer division: How do you produce a double?

Community
  • 1
  • 1
mattinbits
  • 10,370
  • 1
  • 26
  • 35
  • `1.0` is already a `double`, no need for the `d`. It is needed instead when you don't have decimals (because otherwise it would be treated as integer). So, either `1.0` or `1d` will work, `1.0d` is redundant – BackSlash Sep 28 '16 at 08:29