Overview In this challenge, you will create a class that contains a recursive me
ID: 3701719 • 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 4-4 x 4 x 4x 4 x 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 o An example of a call to method power would be power (4, 5) with 1024 as the value returned by the method. 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 baseValueex o o 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 o Output a statement with the answer '1' when the exponent entered is 0 (Do not call the power method.) o Provide an error message for a negative exponent entry . 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.Explanation / Answer
package chegg;
import java.util.Scanner;
public class PowerTest {
public static void main(String[] args) {
// declaration of variable
long num, pwr,result;
//Scanner for user input
Scanner scannner = new Scanner(System.in);
System.out.println("Enter Number to get power");
//here taking value from user
num = scannner.nextLong();
System.out.println("Enter Power of Number 0 or greater than 0");
//here taking power value from user
pwr = scannner.nextLong();
//creating object as method is non static
PowerTest powerTest = new PowerTest();
//called power method and received return value
result = powerTest.power(num, pwr);
//printing output to the console
System.out.println(pwr+" Power of number :"+num+ " is :"+result);
}
//method power which take parameter num and power to calculate power of number and return value
public long power(long num, long power) {
long value = 1;
//if power is 0 then value will be 1
if(power ==0)
{
value = 1;
}
// power greater than 0
else
{
for(int i = 0; i<power;i++)
{
//num num * num the no of power iteration will go
value = value * num ;
}
}
return value;
}
}
Thank You!! feel free to comment if you find difficulty. you can do it with math function easily but requirement was this so done this.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.