8 Replies - 23405 Views - Last Post: 16 May 2008 - 03:48 AM Rate Topic: -----

#1 NavyRookieCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • 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)
Was This Post Helpful? 0
  • +
  • -

#3 NavyRookieCoder  Icon User is offline

  • New D.I.C Head

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

Re: Recursive Star Pattern Method

Posted 14 May 2008 - 02:51 AM

View Postcfoley, 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.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 NavyRookieCoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 1

#6 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Recursive Star Pattern Method

Posted 14 May 2008 - 09:28 PM

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


Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Recursive Star Pattern Method

Posted 15 May 2008 - 04:26 PM

View Postcfoley, 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)
	  {


Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • 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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1