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

Overview In this challenge, you will create a class that contains a recursive me

ID: 3698598 • Letter: O

Question

Overview In this challenge, you will create a class that contains a recursive method named power and a main method to execute it. The power method evaluates integer exponents with the exponent indicating the number of times the base number is used as a factor (for example 45 4x4x4x 4x4, which results in 1024). The main method will allow a user to enter the values for base and exponent. Specifications PowerTesting,java The PowerTest class includes the power method and a main method. power) Create a method power that receives two integer arguments and uses recursion to solve the integer exponent value. 5) with 1024 as the An example of a call to method power would be power (4, value returned by the method. o o When the base value is raised to exponent 1, the answer is base. (This is the base case of the recursion.) For the power method, assume that exponent is an integer greater than or equal to 1. The recursion step should use this criteria: baseValuep baseValue x baseValue- o o main) Prompt for and read input values from the user for the base and exponent. Depending on the exponent value entered, report the correct information to the user: . . Call the power method when the exponent is greater than zero and provide the answer. o o Output a statement with the answer '1' when the exponent entered is 0. (Do not call the power method.) Provide an error message for a negative exponent entry. o After the first calculation and reported results, prompt the user to enter Y to perform another calculation or "N' to end the program . Additional Requirements Follow the Basic Coding Standards. .Include a comment section at the top of your program with your student ID, name, and title of the program. 19 MacBook Air

Explanation / Answer

Program


package powertest;
import java.util.Scanner;

public class PowerTest {

   
    public static void main(String[] args) {
         float a;
        float n;
        float res=0;
       String c,d;
do
{
        Scanner in = new Scanner(System.in);
        //Read base and exponent
        System.out.print("Enter base value : ");
        a = in.nextFloat();

        System.out.print("Enter exponent value : ");
        n = in.nextFloat();
if(n==0)           //When base value is raised to exponent 0, result is 1.
res=1;
else if(n==1)      //When base value is raised to exponent 1, result is base.
res=a;
else if (n<1)
     System.out.println("Error!!!Negative exponent entry: ");

else
res = power(a, n);

        System.out.println("Power is "+res);
        System.out.println("Do you want to continue??? Press Y to continue N to exit");
        d=in.nextLine();
         c=in.nextLine().toUpperCase();;
}while(c.equals("Y"));
   }

    public static float power(float a, float n) {
       
if(n==0)           //When base value is raised to exponent 0, result is 1.
return 1;
else
return a*power(a,n-1); //Recursively calculate power (a,n)
    
    }
   
}

Output

Enter base value : 4
Enter exponent value : 5
Power is 1024.0
Do you want to continue??? Press Y to continue N to exit
Y
Enter base value : 2
Enter exponent value : 3
Power is 8.0
Do you want to continue??? Press Y to continue N to exit
N