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

5. ( Recursion.java ) Write a java program that has three static recursive metho

ID: 3804094 • Letter: 5

Question

5. (Recursion.java) Write a java program that has three static recursive methods and a main method. In your main method (5 marks) prompt for user input and display the results of each of your recursive methods as shown in the sample output below.

a) Write a method that uses recursion to compute the value of , where a and n are both arguments to the method. If n = 0, the method should return 1 as .   If n = 1, the method should return a as . If n is any other number … that’s for you to determine but remember, .        

b) Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. Hint: For base cases, consider a string that has 1 or fewer characters…how much work is there to reverse them? Otherwise a reversed string is the last letter of the original string plus the reverse of the rest. Try it out on paper first.

c) Write a recursive method that determines the number of digits in an integer, n. Hint: If n < 10, there is one digit. Otherwise, it has one more digit than n / 10.

For these questions and with recursion in general, the trick is to determine 1) the base case(s) that will cause the function to simply return and end the recursion and 2) the recursive case(s) that will cause the function to call itself with a smaller version of the same problem. All input and output should take place in your main method. *** Don’t forget to “eat the new line” if you use your scanner between numbers and strings.

java Recursion

Enter two numbers, base then exponent: 2 0

Result: 1

Enter a string to reverse: apples

Result: selppa

Enter a number: 123

Number of digits: 3

java Recursion

Enter two numbers, base then exponent: 100 1

Result: 100

Enter a string to reverse: Z     

Result: Z

Enter a number: 6

Number of digits: 1

java Recursion

Enter two numbers, base then exponent: 2 12

Result: 4096

Enter a string to reverse: i luv cosc!

Result: !csoc vul i

Enter a number: 1234567890

Number of digits: 10

Explanation / Answer

import java.util.*;

public class Result{

public static int findPower(int base, int ex)
{
if(ex==0)
return 1;
else if(ex==1)
return base;
else
return base*findPower(base,ex-1);
}
  
public static String reverseString(String str)
{
String reverse = "";
if(str.length() == 1){
return str;
}
else {
reverse += str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));
return reverse;
}
}

public static int numOfDigits(int num)
{
if (num > -10 && num < 10) {
return 1;
} else {
return numOfDigits(num / 10) + 1;
}
}
  
public static void main(String []args){
  
int base, exponent,power,digits,num;
  
String str = null;
String revStr = "";
  
Scanner s = new Scanner(System.in);
System.out.println("Enter two numbers : base and exponent");
base = s.nextInt();
exponent = s.nextInt();
  
power = findPower(base,exponent);
  
System.out.println("Result : " + power);
  
System.out.println("Enter string to reverse");
str = s.next();
  
revStr = reverseString(str);
  
System.out.println("Result : " + revStr);
  
System.out.println("Enter a number");
num = s.nextInt();
  
digits = numOfDigits(num);
  
System.out.println("Result : " + digits);
  
}
}

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