Skip to main content
edited title
Link
James P.
  • 413
  • 2
  • 8
  • 14

Attempt at recursive method to draw diamond (recursive or not ?)

Tweeted twitter.com/#!/StackCodeReview/status/365577310914617344
Fixed a little grammar
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Attempt at recursive method to draw ascii diamond

I'm not so good with recursion, so I've decided to tackle an exercise consisting inof drawing this pattern using characters.:

      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Here is my code so far. What could be improved  ? Furthermore, doesis recursion suitsuitable for a task like this or not  ?

class Main
{
    public static void main (String[] args)
    {
        Main.drawDiamond("*", 0, 5 );
    }
    
    public static void drawDiamond( String seed, int turn, int centerLevel ){
        
        if( seed.length() <= 0 ){
            return;
        }
        
        char[] chars = new char[Math.abs(turn-centerLevel-1)];
        Arrays.fill(chars, ' ' );
        String spaces = new String(chars);
        
        System.out.print( spaces );
        
        System.out.println( seed );
        
        if( turn <= centerLevel ){
            seed += "**";
        } else {
            if( seed.length() >= 3 ) {
                 seed = seed.substring(1,seed.length()-1); 
            } else {
                seed = "";
            }
        }

        turn++;
        
        drawDiamond( seed, turn, centerLevel );
    }
}

Attempt at recursive method to draw ascii diamond

I'm not so good with recursion so decided to tackle an exercise consisting in drawing this pattern using characters.

      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Here is my code so far. What could be improved  ? Furthermore, does recursion suit a task like this or not  ?

class Main
{
    public static void main (String[] args)
    {
        Main.drawDiamond("*", 0, 5 );
    }
    
    public static void drawDiamond( String seed, int turn, int centerLevel ){
        
        if( seed.length() <= 0 ){
            return;
        }
        
        char[] chars = new char[Math.abs(turn-centerLevel-1)];
        Arrays.fill(chars, ' ' );
        String spaces = new String(chars);
        
        System.out.print( spaces );
        
        System.out.println( seed );
        
        if( turn <= centerLevel ){
            seed += "**";
        } else {
            if( seed.length() >= 3 ) {
                 seed = seed.substring(1,seed.length()-1); 
            } else {
                seed = "";
            }
        }

        turn++;
        
        drawDiamond( seed, turn, centerLevel );
    }
}

Attempt at recursive method to draw diamond

I'm not so good with recursion, so I've decided to tackle an exercise consisting of drawing this pattern using characters:

      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Here is my code so far. What could be improved? Furthermore, is recursion suitable for a task like this or not?

class Main
{
    public static void main (String[] args)
    {
        Main.drawDiamond("*", 0, 5 );
    }
    
    public static void drawDiamond( String seed, int turn, int centerLevel ){
        
        if( seed.length() <= 0 ){
            return;
        }
        
        char[] chars = new char[Math.abs(turn-centerLevel-1)];
        Arrays.fill(chars, ' ' );
        String spaces = new String(chars);
        
        System.out.print( spaces );
        
        System.out.println( seed );
        
        if( turn <= centerLevel ){
            seed += "**";
        } else {
            if( seed.length() >= 3 ) {
                 seed = seed.substring(1,seed.length()-1); 
            } else {
                seed = "";
            }
        }

        turn++;
        
        drawDiamond( seed, turn, centerLevel );
    }
}
Source Link
James P.
  • 413
  • 2
  • 8
  • 14

Attempt at recursive method to draw ascii diamond

I'm not so good with recursion so decided to tackle an exercise consisting in drawing this pattern using characters.

      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Here is my code so far. What could be improved ? Furthermore, does recursion suit a task like this or not ?

class Main
{
    public static void main (String[] args)
    {
        Main.drawDiamond("*", 0, 5 );
    }
    
    public static void drawDiamond( String seed, int turn, int centerLevel ){
        
        if( seed.length() <= 0 ){
            return;
        }
        
        char[] chars = new char[Math.abs(turn-centerLevel-1)];
        Arrays.fill(chars, ' ' );
        String spaces = new String(chars);
        
        System.out.print( spaces );
        
        System.out.println( seed );
        
        if( turn <= centerLevel ){
            seed += "**";
        } else {
            if( seed.length() >= 3 ) {
                 seed = seed.substring(1,seed.length()-1); 
            } else {
                seed = "";
            }
        }

        turn++;
        
        drawDiamond( seed, turn, centerLevel );
    }
}