Question about JAVA Programming?

2010-09-02 10:52 pm
Can anyone do the following problem?

Write a method that takes 2 integer parameters, and returns the result of the first parameter value raised to the power of the second parameter value. Do not use Java API.

For example, if the first parameter value is 2 and the second parameter value is 5, then the method will return 32, since 2^5 = 2 x 2 x 2 x 2 x 2 = 32.

Remember, the methods are needed to work in the general case. So the above is just a specific example. your method should be able to work for any parameter values given to it, not just for the values of 2 and 5. Hint: you will need to use a single loop, since you are repeatedly multiplying by a number.

For simplicity, you may assume that the second parameter value is always a positive number.

Check the result of your method by printing out the returned value in the main method, which uses the method you wrote.

Thank you!

回答 (3)

2010-09-02 10:58 pm
✔ 最佳答案
This will be horribly inefficient but since they mentioned a loop this is definitely how they want it.

How do you raise a power to an exponent?

Take the example 2⁵

First we have:

x = 1...0
x *= 2 = 2...1
x *= 2 = 4...2
x *= 2 = 8...3
x *= 2 = 16...4
x *= 2 = 32...5

this should give you an idea of how to make a loop...

Just because I think it's important that you start out doing things the right way, like the above person said, you should be able to figure out that you are performing the same operation over and over again (notice that 2 was the base NOT the exponent) so this is definitely a loop, furthermore you should be able to figure (from the exponent) how many times you need to do this...

Let's look @ the above, notice that you might naively say there are 6 iterations...but the first assignment x = 1 should be outside of the loop. After that we multiply by 2, 5 times. Since the number (1, 2, 3, 4, 5) is not used, it's really just a matter of counting 5 times multiplying by 2 each time.

WHENEVER YOU WANT TO COUNT SOMETHING OR DO SOMETHING AN INTEGER AMOUNT OF TIMES.

Always, start @ 0 and go to < n:

If you want to do n iterations in a loop then it should look like this:

for(int i = 0; i < n; ++i){}

NOT

for(int i =1; i <=n; ++i){}

Although these are equivalent, doing it the first way will prepare you for arrays and you will find that almost ALL loops in computer science can be zero-indexed...technically you could force all loops to start anywhere you wanted, but things will make more sense when you 0-index things.
2010-09-03 6:23 am
int function cal(int base, int exponent)
{
answer =1;
for(int i=0; i<exponent;i++){answer*=base;}
return answer;
}
2010-09-03 5:57 am
You're only starting out it seems, so I'll give you a hint, but I won't do it for you. You want to repeat an operation a number of times, where the number of times is another number. There's something you can use that loops until a condition.


收錄日期: 2021-05-01 13:18:21
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100902145239AAc90Hc

檢視 Wayback Machine 備份