Write a recursive method called printLettersForward that will accept a character
ID: 3697446 • Letter: W
Question
Write a recursive method called printLettersForward 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
The sequence of characters printed should be the same case as the parameter. If the parameter value is 'D' (uppercase), then the method should print:
ABCD
If the parameter value is not a letter of the alphabet, do not print anything. Do not return anything.
public static void printLettersForward(char c) { }
2. Copy the printLettersForward method and rename it printLettersBackwards. Change the method so that it prints the letters in reverse order. . If the parameter value is 'D' (uppercase), then the method should print:
DCBA
3. 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. Do not return anything. For example, if the test call to this method is triangle(5), the output should look like:
*
**
***
****
*****
public static void triangle(int n) { }
. 4. Copy the triangle method and rename it upsideDownTriangle. Change the method so that the longest line of asterisks prints first. If the parameter value is 5, the method should print:
*****
****
***
**
*
5. Write a method called powerOfTen that will find and return 10n for any integer n (n is the parameter). The method should return a double value.
Examples:
powerOfTen(3) should return 1000.0
powerOfTen(-3) should return 0.001
The method should not print anything.
public static double powerOfTen(int n) { }
Part B: Create a main method within the same program. Do not create a separate test program. Put a main method in Lab9.java. Add calls to the main to test each method as many times as needed to insure that all cases are working correctly.
This is a study worksheet for my upcoming exam, and Im not very good at recusive methods, and was wondering if anyone could show me how to do it. If you can I would be grateful, thank you in advanced to anyone who helps me.
Explanation / Answer
public class Lab9 {
// 1
public static void printLettersForward(char c) {
if (!Character.isLetter(c)) // if not letter then return
return;
if (Character.isUpperCase(c)) { // base case: if letter is upper case
// and letter is 'A'
if (c == 'A') {
System.out.print('A');
return;
}
} else {// base case: if letter is lower case and letter is 'a'
if (c == 'a') {
System.out.print('a');
return;
}
}
int intVal = c - '0';
printLettersForward((char) (intVal - 1 + 48)); // calling recursively
System.out.print(c); // print
}
// 2
public static void printLettersBackwards(char c) {
if (!Character.isLetter(c)) // if not letter then return
return;
if (Character.isUpperCase(c)) { // base case: if letter is upper case
// and letter is 'A'
if (c == 'A') {
System.out.print('A');
return;
}
} else {// base case: if letter is lower case and letter is 'a'
if (c == 'a') {
System.out.print('a');
return;
}
}
System.out.print(c); // print
int intVal = c - '0';
printLettersBackwards((char) (intVal - 1 + 48)); // calling recursively
}
// 3
public static void triangle(int n) {
if (n <= 0) // Base case
return;
if (n == 1) { // base case
System.out.println("*");
return;
}
triangle(n - 1); // recursive call
// print *
for (int i = 1; i <= n; i++)
System.out.print("* ");
System.out.println();
}
// 4
public static void upsideDownTriangle(int n) {
if (n <= 0) // Base case
return;
if (n == 1) { // base case
System.out.println("*");
return;
}
// print *
for (int i = 1; i <= n; i++)
System.out.print("* ");
System.out.println();
upsideDownTriangle(n - 1); // recursive call
}
//5
public static double powerOfTen(int n) {
double temp;
if( n == 0) // base case
return 1;
temp = powerOfTen(n/2);
if (n%2 == 0) // if n is even
return temp*temp;
else // if n is an odd
{
if(n > 0) // if n is positive
return 10*temp*temp;
else // if n is negative
return (temp*temp)/10;
}
}
public static void main(String[] args) {
printLettersForward('F');
System.out.println();
printLettersBackwards('F');
System.out.println();
triangle(5);
System.out.println();
upsideDownTriangle(5);
System.out.println();
System.out.println(powerOfTen(3));
System.out.println(String.format("%.3f",powerOfTen(-3)));
}
}
/*
Output:
ABCDEF
FEDCBA
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
1000.0
0.001
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.