HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

xo squares in ASCII

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
asciisquaresstackoverflow

Problem

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");
    }
    
}

Solution

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 = 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));
}

Code Snippets

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();
    }
}
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));
}

Context

StackExchange Code Review Q#113699, answer score: 3

Revisions (0)

No revisions yet.