Click to See Complete Forum and Search --> : Help writing recursion methods in java?


justin144
12-02-2010, 08:55 PM
Hi, I need some help with two methods that I am writing. here is what the methods are supposed to do.

Write a recursive method called printLetters that will accept a character parameter. The method should print all of the letters of the alphabet up to (including) the parameter value. For example, if the parameter passed to the method is 'f', the method should print:
abcdef
If the parameter is a lowercase letter then the characters printed should be lowercase, also.
If the parameter value is 'D' (uppercase), then the method should print uppercase letters: ABCD.

Here is the method I wrote, but it prints the letters backwards.

public static void printLetters(char c) {

... //error case
... if(c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
...... return;

... //base case
... //not sure what to put here
... System.out.print(c);

...//recursive step
...printLetters((char)(c-1));

}//end printLetters


Here are the instructions for the seconds method:
Write a recursive method called triangle that will display a triangle made of asterisks. The only parameter is an integer that determines the number of asterisks to print on the current line. (Hint: Recursion doesn't mean you can't have any loop in the method. It just means that recursion is controlling the overall process.) Do not return anything. For example, if the test call to this method is triangle(5), the output should look like:
*
**
***
****
*****

and here is the code that I wrote for it (again, it prints backwards):

public static void triangle(int n){
... //error case
... if(n < 1)
...... return;

... //base case
... if(n == 1){
...... System.out.println("*");
...... return;
... }//end if

... for(int i = 1; i <= n; i++)
...... System.out.print("*");
... System.out.println("");


... //recursion step
... triangle(n-1);

}//end triangle

zimonyi
12-03-2010, 12:38 PM
Hello,

in regards to your first problem, meaning the printLetters(char c) method, I would not make the printLetters() method recursive.

Instead I would call another private method that does the actual recursiveness (since you get the characters in the wrong order).

What you need is to start from the letter a (or A if you have upper case). In the class Character (wrapper-class for char) you have a method named isLowerCase(). With that you know if the character entered to your printLetters() is uppercase or not.

Then based on that you set a variable named "startChar" either to a or A.

Then you call your private static method (which is going to be recursive) with two arguments, the startChar and your char.

Then instead of doing -1 as you do now, you can do +1 on the startChar and go until both are the same at which time you simply return.

Hope that gives you some ideas. I will not write the code for you.

Archie

zimonyi
12-03-2010, 12:40 PM
In regards to your second problem, I think you should do the same as my suggestion for your first problem.

Archie