Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recursion Practice Exercise For each of the following recursion problems, write

ID: 3790494 • Letter: R

Question

Recursion Practice Exercise

For each of the following recursion problems, write the requested method and then a segment of code in a driver class that verifies that your solution is working correctly.

Recall that for every recursive method you need to include the following three elements:

Base case

Recursive call (one or more)

Approach your base case with your recursive call(s)

Problems

1. Write a recursive method that accepts an integer n and prints the first n positive integers to the Java console. For example:

n, (n – 1), (n – 2), ..., 3, 2, 1

For example, if n = 10, then the following would be printed to the Java console:

10 9 8 7 6 5 4 3 2 1

2. Write a recursive method that accepts an integer n and prints all multiples of 3 from n down to 3.

For example, if I was given n = 20 then I would send the following output to the Java console:

18 15 12 9 6 3

Note: do not assume that n begins as a multiple of 3

3.   Write a recursive method that accepts an integer n and returns the sum of the first n positive integers.

1 + 2 + 3 + + (n-2) + (n-1) + n

For example, if n = 5, then the method should return:

1 + 2 + 3 + 4 + 5 = 15

4. Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line.

Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string.

For example, if the input was “the cat purred”, then the method would print the following to the Java console:

the
cat
purred

Challenge Problem

Write a recursive method that takes a string as an input and returns a boolean value that tells you whether the string is a palindrome. A palindrome is a word/sentence that when read backwards is the same as the original word/sentence.

Some example palindromes: “civic”, “radar”, “deleveled”, “a man, a plan, a canal: Panama” (ignore the punctuation)

Explanation / Answer

ReccursiveTest.java


public class ReccursiveTest {

  
   public static void main(String[] args) {
       printNumbers(10);
       printMultiplesOf3(20);
       System.out.println(getSum(5));
       System.out.println(isPalindrome("civic"));
       System.out.println(isPalindrome("deleveled"));
       System.out.println(isPalindrome("abcab"));
       displayEachWord("the cat purred");
   }
   public static void printNumbers(int n){
       if(n == 0){
           System.out.println();
           return;
       }
       else{
           System.out.print(n+" ");
           printNumbers(n-1);
       }
   }
   public static void printMultiplesOf3(int n){
       if(n == 0){
           System.out.println();
           return;
       }
       else{
           if(n % 3 == 0){
           System.out.print(n+" ");
           }
           printMultiplesOf3(n-1);
       }
   }
   public static int getSum(int n){
       if(n == 1){
           return 1;
       }
       else{
           return n + getSum(n-1);
       }
   }
   public static boolean isPalindrome(String str){
       // if length is 0 or 1 then String is palindrome
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length()-1))

return isPalindrome(str.substring(1, str.length()-1));
return false;
   }
   public static void displayEachWord(String s){
       if(s.indexOf(" ") == -1){
           System.out.println(s);
           return;
       }
       else{
           System.out.println(s.substring(0,s.indexOf(" ") ));
           displayEachWord(s.substring(s.indexOf(" ")+1, s.length() ));
       }
   }
}

Output:

10 9 8 7 6 5 4 3 2 1
18 15 12 9 6 3
15
true
true
false
the
cat
purred

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote