# Recursive Star Pattern Method

Page 1 of 1

## 8 Replies - 41907 Views - Last Post: 16 May 2008 - 03:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=51949&amp;s=7ccc118c01d0cdc98e9fc5fc95189fde&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 NavyRookieCoder

Reputation: 1
• Posts: 3
• Joined: 12-May 08

# Recursive Star Pattern Method

Posted 13 May 2008 - 01:46 PM

I have to write two methods for a recursive star pattern. The first method is:
``` public static void printStars1(int lines)
{
if (lines > 0)
{
for (int i = 0; i < lines; i++)
System.out.print("*");
System.out.println();

printStars1 (lines - 1);

for (int i = lines; i > 0; i--)
System.out.print("*");
System.out.println();
}

}

```

It produces a pattern like this when called (when the main method enters the number of lines:

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

The other method is supposed to look like this:
*
**
***
****
****
***
**
*

I first thought I should change the parameters of my loop, but that didn't work. I then moved the recursive call to the beginning of the method, but that only makes the top half of the pattern. I feel like I am overlooking somethin simple. Can anyone point me in the right direction?

Thanks.

Is This A Good Question/Topic? 0

## Replies To: Recursive Star Pattern Method

### #2 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Recursive Star Pattern Method

Posted 13 May 2008 - 05:28 PM

Are you allowed to change the method signature? Maybe something like:

public static void printStars2(int lines, int maxLines)

### #3 NavyRookieCoder

Reputation: 1
• Posts: 3
• Joined: 12-May 08

## Re: Recursive Star Pattern Method

Posted 14 May 2008 - 02:51 AM

cfoley, on 13 May, 2008 - 05:28 PM, said:

Are you allowed to change the method signature? Maybe something like:

public static void printStars2(int lines, int maxLines)

I don't think so. Both of the signatures have to remain the same.

### #4 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Recursive Star Pattern Method

Posted 14 May 2008 - 04:43 AM

In that case I would use 2 methods... but somehow I don't think that would be permitted solution to the problem.

```public static void printStars1(int lines)
{
doPrintStars2( 1, lines );
}

private static void doPrintStars2( int stars, int max )
{
if (lines <= max 0)
{
for (int i = 0; i < stars; i++)
System.out.print("*");
System.out.println();

printStars2 (stars + 1, max);

for (int i = 0; i > stars; i++)
System.out.print("*");
System.out.println();
}

}
```

Could you post the exact text of the problem? It'll help me see exactly what's allowed.

### #5 NavyRookieCoder

Reputation: 1
• Posts: 3
• Joined: 12-May 08

## Re: Recursive Star Pattern Method

Posted 14 May 2008 - 08:33 PM

Here it is:

Write a recursive method to generate a pattern of stars such as the following:

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

Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive method to generate the pattern. For example, specifying the number of lines to be 4 generates the preceding pattern.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Recursive Star Pattern Method

Posted 14 May 2008 - 09:28 PM

NavyRookieCoder, on 14 May, 2008 - 08:33 PM, said:

Here it is:

Write a recursive method to generate a pattern of stars such as the following:

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

Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive method to generate the pattern. For example, specifying the number of lines to be 4 generates the preceding pattern.

```import java.util.Scanner;

public class Star {

public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of stars: ");
int n = sc.nextInt();
printStar(1, n);
}

static void printStar(int n, int max) {

for(int i = 0; i < n; i++)
System.out.print("*");
System.out.println();
if(n < max)
printStar(n+1, max);
for(int i = 0; i < n; i++)
System.out.print("*");
System.out.println();
}
}

```

### #7 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Recursive Star Pattern Method

Posted 15 May 2008 - 04:19 PM

Yeah, that was my first suggestion. Where does it say you can't change the method signature?

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Recursive Star Pattern Method

Posted 15 May 2008 - 04:26 PM

cfoley, on 15 May, 2008 - 04:19 PM, said:

Yeah, that was my first suggestion. Where does it say you can't change the method signature?

Sorry cfoley

you just had a typo in your posted code that cannot make it compile for the newbie

```{
if (lines <= max 0)
{

```

### #9 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Recursive Star Pattern Method

Posted 16 May 2008 - 03:48 AM

Yeah, that would screw it up. I should really check my code before posting.

OK, I've done another one that uses an unmodified method signature. I don't suggest you submit it because it's a hacky horrible solution but it was annoying me so I had to solve it. After writing the code I thought I might as well post it. Basically, it embeds the maximum number of stars in the stars variable.

Say the maximum is 6 and the current is 2, the number would be 600002. The first time it is called, it knows to take the number as the maximum. So, calling it with a 4 would give the following values of "stars" on each recursion.

4
400002
400003
400004
400005 - nothing gets displayed this time

```import java.util.Scanner;

public class StarryStarryNight {

public static void main( String args[] ) {

Scanner sc = new Scanner( System.in );
System.out.print( "Enter number of stars: " );
int n = sc.nextInt();
printStars2( n );

} // main

public static void printStars2( int stars ) {

int factor = 100000;

int maxStars = stars / factor;
int starsThisLine = stars % factor;

if ( maxStars == 0 ) {
maxStars = stars;
starsThisLine = 1;
} // if

if ( starsThisLine <= maxStars ) {

for( int i = 0; i < starsThisLine; i++ ) {
System.out.print( "*" );
} // for
System.out.println();

printStars2( ( maxStars * factor ) + starsThisLine + 1 );

for( int i = 0; i < starsThisLine; i++ ) {
System.out.print( "*" );
} // for
System.out.println();

} // if

} // printStars2

} // class

```