Hello, please help me with these question in Java. Question Write a method integ
ID: 671642 • Letter: H
Question
Hello, please help me with these question in Java.
Question
Write a method integerPower(base, exponent) that returns the value base exponent
for example, integerPower(3,4) calculates 3 4 or 3*3*3*3.
Assume that exponent is a positive,non zero integer and that the base is an integer.
Use a for or a while loop statement to controll calculation.
Do not use any Math class methods.
Incorporate this integerPower method to another application that reads for base and exponents
and performs the calculation with the incorporated integerPower method.
(note: I believe by "incorporate" , they mean the integerPower method has to be in a seperate class than the application that reads and performs calculation with the method. So I believe the method has to be imported or called with an object created in the other class)
Thank you. Assistance is gladly appreciated!
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Power
{
public static int integerPower(int base,int exponent)
{
int value = 1;
for(int i=1;i<=exponent;i++)
value = value*base;
return value;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
System.out.println("2 powers 5 = "+ integerPower(2,5));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.