5
\$\begingroup\$

When I finished this problem, I thought about how I initialized my loop iterator variables i and j. Originally I had i = 0 and j = 0, but then I had to initialize count to 1, instead of 0. And that just seemed a bit odd to me. A similar conundrum came to me when I was incrementing/decrementing count, and deciding if I wanted to take the sum with + count, or the difference with - count. I started by using count++, and it seems like I did so out of habit. So, what do you prefer when you're making these sorts of decisions? Is there a general guideline for this sort of thing? Something that'd make the decisions easier for me to make? Any feedback is appreciated.

Write a static method named xo that accepts an integer size as a parameter and prints a square of size by size characters, where all characters are "o" except that an "x" pattern of "x" characters has been drawn from the corners of the square. In other words, on the first line, the first and last characters are "x"; on the second line, the second and second-from-last characters are "x"; and so on. If 0 or less is passed for the size, no output should be produced.

public static void xo(int size) {
    
    if (size > 1) {
        int count = 0;
        for(int i = 1; i <= size; i++) {          
            for(int j = 1; j <= size; j++) {
                if((i == j) || (size - j - count == 0)) {
                    System.out.print("x");    
                } else {
                    System.out.print("o");
                }
            }
            count++;
            System.out.println();
        }
          
    } else if(size == 0) {
        
    } else if(size == 1) {
        System.out.println("x");
    }
    
}
\$\endgroup\$
  • \$\begingroup\$ Why is else if(size == 0) { } empty? \$\endgroup\$ – user34073 Dec 12 '15 at 3:46
  • \$\begingroup\$ Good question! I guess I could've just used an else. \$\endgroup\$ – cody.codes Dec 12 '15 at 3:58
3
\$\begingroup\$

You should strive to write your code without special cases. Having more code paths makes your code harder to analyze. Actually, your general case already works to cover everything.

What is the point of count? Isn't it always i - 1?

While for(int i = 1; i <= size; i++) is not wrong, it would be more idiomatic in Java to write for (int i = 0; i < size; i++) unless you had a good reason to count starting from 1.

Personally, I prefer a ternary conditional to better express the fact that one way or another, something will get printed. That's a matter of taste, though.

public static void xo(int size) {
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            System.out.print((row == col) || (row + col == size - 1) ? "x"
                                                                     : "o");
        }
        System.out.println();
    }
}

If you care about performance, then you would be better off building the entire result first, then printing the entire string in one System.out.print() call.

Here's one way to do it, but it's rather ugly.

public static void xo(int size) {
    char[] result = new char[size * (size + 1)];
    java.util.Arrays.fill(result, 'o');
    for (int i = 0; i < result.length; i += size + 2) {
        result[i] = 'x';
    }
    for (int i = result.length - size - 1; i >= 0; i -= size) {
        result[i] = 'x';
    }
    for (int rowEnd = size; rowEnd < result.length; rowEnd += size + 1) {
        result[rowEnd] = '\n';
    }
    System.out.print(new String(result));
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

As @Hoscho250 pointed out in a comment, this doesn't do anything:

else if(size == 0) {

}

you say that you could have just used an else, but that's not fixing anything. You can just leave this code out. Since you're neither printing anything nor raising an error, you can ignore any case where size is less than 1.

public static void xo(int size) {

    if (size > 1) {
        int count = 0;
        for(int i = 1; i <= size; i++) {          
            for(int j = 1; j <= size; j++) {
                if((i == j) || (size - j - count == 0)) {
                    System.out.print("x");    
                } else {
                    System.out.print("o");
                }
            }
            count++;
            System.out.println();
        }

    } else if(size == 1) {
        System.out.println("x");
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.