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

Write a java program that has three static recursive methods and a main method.

ID: 3804480 • Letter: W

Question

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 a^n where a and n are both arguments to the method. If n = 0, the method should return 1 as a^0 = 1. If n = 1, the method should return a as a^1 = a. If n is any other number ...that's for you to determine but remember, a^n = a times a^n-1. b) Write a method that uses recursion to return the reverse of a String that is passed to the method as an argument. c) Write a recursive method that determines the number of digits in an integer, n. For these questions and with recursion in general, the trick is to determine 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 cosc! Result: !csoc vul i Enter a number: 1234567890 Number of digits: 10

Explanation / Answer

import java.util.*;

class Recursion
{
   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in);
      
       System.out.println("Enter two numbers , base then exponent : ");
       int base = scan.nextInt();
       int exponent = scan.nextInt();
       System.out.println("Result : "+power(base,exponent));//call to recursive power function
      
      
      
       System.out.println("Enter a string to reverse : ");
       String str = scan.next();
       System.out.println("Result : "+revString(str));//call to revString function
      
       System.out.println("Enter a number : ");
       int num = scan.nextInt();
       System.out.println("Number of digits : "+countDigits(num));//call to countDigits function
      
      
   }
   public static int power(int base,int exponent)
   {
        if(exponent == 0) //base to the power 0 = 1
        return 1;
        else if(exponent == 1)// base to the power 1= base
        return base;
        else
      
            return (base*power(base,exponent-1));
      
   }
   public static String revString(String str)
   {
         if (str.length() <= 1)
            return str;
        return revString(str.substring(1)) + str.charAt(0);
      
   }
   public static int countDigits(int n)
   {
        if(n < 10)
        return 1;
        else
        return (1+countDigits(n/10));
   }
  
}


output:

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

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