Help with Class Methods(Probably simple)
Jake here, im new to this forum, im also new to java and recently got a bit stuck in a programming exercise for my module. I know it'll probably be really easy to some people but i thought id share it because im still only learning.The question is as follows:
Write a class method which calculates the power of one number raised to another. The method would be used as follows:
Code :
int x = getPower(2, 5); // 2 to the power of 5 = 32
int x = getPower(3, 4); // 3 to the power of 4 = 81
Use this method to print the first 20 powers of 2.
The class method i will use will be something like this:
Code :
static int getPower(int num, int power){
}
Im basically just confused about how i would go about doing this and whether or not i need to use a for loop. Any help would be greatly appreciated.
Thanks and il look forward to talking with you all throughout the forum :)
Re: Help with Class Methods(Probably simple)
How would you do it manually?
For example:
if power = 1 return the number.
Mult the number by itself
if power = 2 return the product
otherwise mult the above result by the number
incr the power number
if power number = desired, done
otherwise go back and do it again
Re: Help with Class Methods(Probably simple)
Thanks for your reply :). Ive tried working it by what you've said. This is what i have so far
Code :
class Powers{
static int getPower(int num, int power){
if(power==1){
num = num;
}
else if(power==2){
num = num*num;
}
????
return num;
}
public static void main(String [] args){
int x = getPower(2, 2);
System.out.println(x);
}
}
This works fine and everything, but im still confused as to what to do next in the getPower() method. The question states that I have to list the first 20 powers of 2 so im presuming it requires a for loop somewhere (maybe where the ??? are) instead of using more if statements. Could you or anybody else help me with this bit of code? Apologies if im being noobish ha like i said im still learnin :)
Re: Help with Class Methods(Probably simple)
First suggestion, use a new variable to hold the results, don't reuse num.
You need a loop where you multiply the result by num while using the value of power to control the number of times thru the loop.
Look at this:
power=2 result= num*num
power=3 result=num*num*num
power=4 result=num*num*num*num
See how for each higher value of power you multiply the result by num one more time.
Re: Help with Class Methods(Probably simple)
Alternatively, you can do something like this:
a^b = c
b * ln(a) = ln(c)
where ln is the natural logarithm.
Now all you have to do is use a logarithm table and extrapolate to an answer, or use the "nearest neighbor" method with a logarithm table (alternatively you can call the exp() method to do the first method for you).
Without using this, though, you would want to use a loop of some kind (either a while or a for loop), and iterate from 0 to the exponent number. Inside the loop you just need to multiply the result (originally 1) by the base.
Re: Help with Class Methods(Probably simple)
I'd suggest doing the multiplication in a loop, as Norm suggests.
If you're allowed to use the Java library, you could just call Math.pow(num, power) :-"
Re: Help with Class Methods(Probably simple)
Thanks alot i solved it just there with all of your help :) It was just a small question out of an exercise sheet that confused me and I hate moving on and leaving questions unsolved. Incase you were wondering this was my solution.
Code :
class Powers{
static int pow(int x, int y){
int result = 1;
for(int z = 0; z < y; z++){
result = result * x;
}
return result;
}
public static void main(String [] args){
int num;
for(int i = 1; i < 21; i++){
num = pow(2, i);
System.out.println("2 to the power of " + i + " = " + num);
}
}
}
Feel free to suggest some tips for future coding as i am here to continue learning more :) Thanks again.
Re: Help with Class Methods(Probably simple)
Quote:
Originally Posted by
dlorde
I'd suggest doing the multiplication in a loop, as Norm suggests.
If you're allowed to use the Java library, you could just call Math.pow(num, power) :-"
Yea i only read that after i used the first method. This is how i done it the other way
Code :
import java.math.*;
class PowersAlt{
static int pow(int num, int power){
int x = (int)Math.pow(num,power);
return x;
}
public static void main(String [] args){
int num;
for(int i=1;i<21;i++){
num = pow(2, i);
System.out.println("2 to the power of " + i + " = " + num);
}
}
}
Re: Help with Class Methods(Probably simple)
Your second method of coding it only calls another method. It doesn't do anything.
Re: Help with Class Methods(Probably simple)
Quote:
Originally Posted by
Norm
Your second method of coding it only calls another method. It doesn't do anything.
I know, I didnt use that, I used the first method. I only posted it to show how I would get the same answer as the previous method. Because the question stated the method to be in the form num = pow(2, i); or num = getPower(2, i); when being called in the main method