3
\$\begingroup\$

I managed to make a perfectly even cross with for loops.

I'm trying to make this cross easier to code. I have to keep it with for loops, but it's too long to implement.

public class CroixDemoAmmeliore {
    public static void main(String args[]) {

        int e = 5;
        int i;
        int j;
        int k;
        int l;

        for (k = 1; k <= e; k++) {
            for (i = 1; i <= e; i++) {
                System.out.print(" ");
            }
            for (j = 1; j <= e; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (k = 1; k <= e; k++) {
            for (i = 1; i <= e * 3; i++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (k = 1; k <= e; k++) {
            for (i = 1; i <= e; i++) {
                System.out.print(" ");
            }
            for (j = 1; j <= e; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ When i formatted your code, i saw that int l is unused. Is that intentional, or it might be something problematic? \$\endgroup\$ Jun 15, 2014 at 21:12
  • \$\begingroup\$ Oops, no that's just a mistake that does not change the result of the code. Thanks for pointing it out. \$\endgroup\$
    – user3742475
    Jun 15, 2014 at 21:17
  • \$\begingroup\$ You misspelled Amélioré with an extra m. Incidentally, Java allows non-ASCII characters in identifiers, though in practice using such characters would probably cause problems with filesystems, version control tools, and other programmers. \$\endgroup\$ Jun 18, 2014 at 6:01

2 Answers 2

2
\$\begingroup\$

First thing I would say is that you could reduce the number of variables you use. The iterator of a For loop, if instanced from outside the loop's context (as in your case), can be reused. Doing so you could already reduce the numbers of variables you use.

I do not understand what you mean by "easier to code", but if you mean that you seek a way to more easily re-use this code, you could either make a static method that would display a cross with specific parameters or an object with a display function.

Here is an exemple of a Cross object:

public class Cross {

    private int sectionSize;

    public Cross(int sectionSize) {
        this.sectionSize = sectionSize;
    }

    public void display() {
        final int height = sectionSize * 3;
        final char character = '*';
        //The iterators
        int i, j;

        //This loop controls the line
        for (i = 0; i < height; i++) {
            //Check wether or not to add spaces
            if (i < sectionSize || i > sectionSize * 2) {
                //Add spaces
                for (j = 0; j < sectionSize; j++)
                    System.out.print(" ");
                //Add the characters
                for (j = 0; j < sectionSize; j++)
                    System.out.print(character);
            }
            else
                for (j = 0; j < height; j++)
                    System.out.print(character);
            System.out.println();
        }
    }
}

Or in the format of a static method (which is actually the display method of the last object without the comments):

public static void drawCross(int sectionSize, char character) {
    final int height = sectionSize * 3;
    int i, j;

    for (i = 0; i < height; i++) {
        if (i < sectionSize || i > sectionSize * 2) {
            for (j = 0; j < sectionSize; j++)
                System.out.print(" ");
            for (j = 0; j < sectionSize; j++)
                System.out.print(character);
        }
        else
            for (j = 0; j < height; j++)
                System.out.print(character);
        System.out.println();
    }
}

Here is how you would use either of these solutions:

public static void main(String [] args) {
    final int sectionSize = 5;

    //Use of the Object
    Cross cross = new Cross(sectionSize);
    cross.display();

    //Use of the static method
    Cross.drawCross(sectionSize, '*');
}

Now, if it were not for the necessity of using For loops, I would have replaced the loop that adds the space with a simpler:

System.out.printf(("%," + sectionSize + "s"), "");

and then I would have added the characters of the cross in a loop. I hope this helps you.

\$\endgroup\$
1
\$\begingroup\$

If you view the canvas as a square and hide/show the symbol at the current coordinate accordingly, then the solution can be reduced to two loops.

public class Cross {
 private int width;
 private String output;

 public Cross(int size, String output){
   width = size;
   this.output = output;
 }

 public void draw() {

    for(int i = 0; i < width * 3; ++i){
      for(int j = 0; j < width * 3; ++j){
        System.out.print( isPaintable(i) || isPaintable(j) ? output : " ");
      }
      System.out.println("");
    }
 }

 private boolean isPaintable(int x){
    return x >= width && x < width * 2;
 }
}

Essentially, the same solution as already provided but with general application of De Morgan's and conversion to ternary statement.

\$\endgroup\$
3
  • \$\begingroup\$ Since size is being used for both the horizontal and vertical axes, I'd keep it named as size rather than renaming it to width. \$\endgroup\$ Jun 18, 2014 at 5:56
  • \$\begingroup\$ It would be clearer if output were called fill. It should probably be just a char. \$\endgroup\$ Jun 18, 2014 at 5:57
  • \$\begingroup\$ @200_success Valid points. \$\endgroup\$
    – Origineil
    Jun 18, 2014 at 13:30

Your Answer

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