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: 3702733 • 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-4 × 4 × 4 × 4 × 4, 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.) o For the power method, assume that exponent is an integer greater than or equal to 1 o The recursion step should use this criteria baseValueexp baseValue x baseValueexp-1 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: o Call the power method when the exponent is greater than zero and provide the answer 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 o After the first calculation and reported results, prompt the user to enter 'Y' to perform another calculation or 'N' to end the program

Explanation / Answer

// PowerCalculation .java

// Java program to read data of various types using Scanner class.

import java.util.Scanner;

public class PowerCalculation {

public static void main(String[] args) {

int base , exponent;

// Declare the object and initialize with

// predefined standard input object

Scanner sc = new Scanner(System.in);

//reading base and exponent values

System.out.println("Enter a base value :");

base = sc.nextInt();

System.out.println("Enter an exponent( 0 or greater) :");

exponent = sc.nextInt();

if(exponent<0)

{

System.out.println("Invalid Exponent");

}

else

{

int result = power(base, exponent);

System.out.printf("%d^%d = %d", base, exponent, result);

}

  

}

//recursive method to calculate power

public static int power(int base, int exponent) {

//recursive call to function if exponetn not equal to zero

if (exponent != 0)

return (base * power(base, exponent - 1));  

else

return 1;

}

}

Output :

Enter a base value : -3
Enter an exponent( 0 or greater) : 5
-3^5 = -243