11 Replies - 1297 Views - Last Post: 31 August 2009 - 02:29 AM Rate Topic: -----

#1 aremid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-August 09

recursion

Posted 28 August 2009 - 11:19 PM

how to write using recursive method for print out example number 123456789987654321. and make 9 as the begin number
Is This A Good Question/Topic? 0
  • +

Replies To: recursion

#2 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Re: recursion

Posted 28 August 2009 - 11:32 PM

public class recursion 
{
	private final int max = 9; // Constant
	private static boolean flag = false;	
	void myMethod( int counter)
	{
	   if(flag)
	   {
		System.out.print(counter);
		if (counter == 1)
			return;
		myMethod(--counter);
	   }
	   else
	   {
		   
		  System.out.print(counter);
		  if (counter == max)
		  {
			 flag = true;
			 myMethod(counter);
			 return;
		  }
		  myMethod(++counter);
	   }
	} 
	
	public static void main(String[] args)
	{
		new recursion().myMethod(1);
	}
}



Obviously you can spice it up. This was just a quick hack.

This post has been edited by trixt.er: 28 August 2009 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: recursion

Posted 28 August 2009 - 11:38 PM

Is this a double post with different accounts or just two people with the same assignment, both ignoring the rules?

And trixter, your making this more complicated than it needs to be.

This post has been edited by syfran: 28 August 2009 - 11:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Re: recursion

Posted 28 August 2009 - 11:44 PM

View Postsyfran, on 28 Aug, 2009 - 10:38 PM, said:

And trixter, your making this more complicated than it needs to be.

Quick hack.
Was This Post Helpful? 0
  • +
  • -

#5 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Re: recursion

Posted 28 August 2009 - 11:53 PM

public class recursion 
{
	private static boolean flag = false;	
	void myMethod(int counter)
	{
 	   System.out.print(counter);
	   if (counter == 9 || flag)
	   {
		   if (counter == 1)  // Base case.
			   return;
		   flag = true;
		   myMethod(--counter);
	   }
	   else
		   myMethod(++counter);
	} 
	
	public static void main(String[] args)
	{
		new recursion().myMethod(1);
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 trixt.er   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 432
  • Joined: 28-September 08

Re: recursion

Posted 29 August 2009 - 12:00 AM

public class recursion 
{
	private static boolean flag = true;	
	void myMethod(int counter)
	{
 	   System.out.print(counter);
 	   if (counter != 9 && flag)
 		  myMethod(++counter);
 	   else if (counter != 1)
 	   {
 		  flag = false;
 		  myMethod(--counter);
 	   }
	} 
	public static void main(String[] args)
	{
		new recursion().myMethod(1);
	}
}



That was fun!
Was This Post Helpful? 0
  • +
  • -

#7 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: recursion

Posted 29 August 2009 - 12:00 AM

Trixter, the goal here isn't to hand out code. It's to help people to learn programming, you can't do that unless you try.
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: recursion

Posted 30 August 2009 - 06:51 PM

View Postaremid, on 28 Aug, 2009 - 10:19 PM, said:

how to write using recursive method for print out example number 123456789987654321. and make 9 as the begin number

why 9 ?
based on what rule ?
Was This Post Helpful? 0
  • +
  • -

#9 vaitalaziz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 09

Re: recursion

Post icon  Posted 31 August 2009 - 02:18 AM

Write a recursive method that could print out the numbers 123456789987654321. Go to the next line after each displayed number. Assume that number 9 is a begin number. In your program, identify the base and recursive case.
Was This Post Helpful? 0
  • +
  • -

#10 Travis1012   User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 226
  • Joined: 05-August 09

Re: recursion

Posted 31 August 2009 - 02:21 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and other members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:Posted Image

Thank you for helping us help you.

This post has been edited by Travis1012: 31 August 2009 - 02:22 AM

Was This Post Helpful? 0
  • +
  • -

#11 syfran   User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: recursion

Posted 31 August 2009 - 02:26 AM

Please don't tell me this is the same guy posting for the third time...
Was This Post Helpful? 0
  • +
  • -

#12 Travis1012   User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 226
  • Joined: 05-August 09

Re: recursion

Posted 31 August 2009 - 02:29 AM

View Postsyfran, on 31 Aug, 2009 - 06:26 PM, said:

Please don't tell me this is the same guy posting for the third time...


Possibly, or it could be a strange coincidence.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1