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
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