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");
  }
  
}
asked Dec 12, 2015 at 3:26
\$\endgroup\$
2
  • \$\begingroup\$ Why is else if(size == 0) { } empty? \$\endgroup\$ Commented Dec 12, 2015 at 3:46
  • \$\begingroup\$ Good question! I guess I could've just used an else. \$\endgroup\$ Commented Dec 12, 2015 at 3:58

2 Answers 2

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));
}
answered Dec 12, 2015 at 4:09
\$\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");
 }
}
answered Dec 14, 2015 at 12:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.