Is this recursion?
- From: "Marion" <mchew02@xxxxxxxxxxx>
- Date: 30 Jan 2007 02:19:33 -0800
Hello all,
This is the ole reverse a string classroom assignment.
Recursion is soemthing I never quite understood till now but as I
perceive it, for a method to be considered recursive, it MUST have a
base case and a recursive step.
Fine, with this in mind, I wrote a simple program that reverses a word
that doesn't have a base step but it calls itself. Would this be
considered by the formal defintion of recursion?
To me, I see the base step as implied by doing nothing while you
decrease the parameter by one when you call the method again and THEN
you stop calling it recusively when you get to the last element of the
string. For example, consider the code:
public class StringReversal {
public static void main(String[] args) {
String aWord = "hello";
System.out.print ("\nReversing " + aWord + " to ");
reversal(aWord,aWord.length()-1);
} // end main
/**reverses a string of characters
* @param string is the string passed */
public static void reversal(String aWord,int position){
System.out.print(aWord.substring(position,position+1));
if (position != 0) {
reversal(aWord,position-1);
}
} // end Reversal method
}
I have a feeling the professor is probably not going to like this
because it doesn't fit the 'model' recursion defintion...no base case
and recursive step defined. I'm just wondering what other ppl
think. It's clear to me and it works! :p
Any thoughts appreciated.
.
- Follow-Ups:
- Re: Is this recursion?
- From: Chris Smith
- Re: Is this recursion?
- From: Patricia Shanahan
- Re: Is this recursion?
- From: Gordon Beaton
- Re: Is this recursion?
- Prev by Date: Re: Arraylist
- Next by Date: Re: Clicking on Sun Java Plugin installation evokes 3 basic java setup questions
- Previous by thread: Unknown number of inputs
- Next by thread: Re: Is this recursion?
- Index(es):
Relevant Pages
|