Question: Computing a positive integer power of a number is easily seen as a recursive process. Consider an: D If n = 0 , an is
Computing a positive integer power of a number is easily seen as a recursive process. Consider an:
D If n an is by definition
D If n an is a an
File Power.java contains a main program that reads in integers base and exp and calls method power to
compute baseexp. Fill in the code for power to make it a recursive method to do the power computation. The
comments provide guidance.
Power.java
Reads in two integers and uses a recursive power method
to compute the first raised to the second power.
import java.util.Scanner;
public class Power
public static void mainString args
int base, exp;
int answer;
Scanner scan new ScannerSystemin;
System.out.printWelcome to the power program! ;
System.out.printlnPlease use integers only.";
get base
System.out.printEnter the base you would like raised to a power: ;
base scan.nextInt;
get exponent
System.out.printEnter the power you would like it raised to: ;
exp scan.nextInt;
answer power baseexp;
System.out.printlnbase raised to the exp is answer;
Computes and returns baseexp
public static int powerint base, int exp
int pow;
if the exponent is set pow to
Computing a positive integer power of a number is easily seen as a recursive process. Consider an:
D If n an is by definition
D If n an is a an
File Power.java contains a main program that reads in integers base and exp and calls method power to
compute baseexp. Fill in the code for power to make it a recursive method to do the power computation. The
comments provide guidance.
Power.java
Reads in two integers and uses a recursive power method
to compute the first raised to the second power.
import java.util.Scanner;
public class Power
public static void mainString args
int base, exp;
int answer;
Scanner scan new ScannerSystemin;
System.out.printWelcome to the power program! ;
System.out.printlnPlease use integers only.";
get base
System.out.printEnter the base you would like raised to a power: ;
base scan.nextInt;
get exponent
System.out.printEnter the power you would like it raised to: ;
exp scan.nextInt;
answer power baseexp;
System.out.printlnbase raised to the exp is answer;
Computes and returns baseexp
public static int powerint base, int exp
int pow;
if the exponent is set pow to
otherwise set pow to basebaseexp
return pow
Power.java
Reads in two integers and uses a recursive power method
to compute the first raised to the second power.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
