File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes exponent loop  w/out math .pow. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exponent loop  w/out math .pow." Watch "exponent loop  w/out math .pow." New topic

exponent loop w/out math .pow.

lillie field

Joined: Apr 05, 2008
Posts: 2
im trying to make a calculator without using math.pow. im writing my class and i seem to be stuck. here's my code.

public static int getRaiseToThePowerOf(int rpo, int n1, int n2)
rpo = 1;

if (n2 < 0)
System.out.println("Error: Exponent Must Be >=0");


else if (n2 == 0)
rpo = 1;

else if (n2 == 1)
rpo = n1;

return n1 * rpo(n1, n2-1);

when i try to compile i get an error saying that the compiler can't find symbol for "rpo" in the line "return n1 * rpo(n1, n2-1);". should i rename all "rpo" to "getRaiseToThePowerOf"? i'm stuck. please help.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

because compiler consider "rpo" as method. which is not exist actually.

should i rename all "rpo" to "getRaiseToThePowerOf"? i'm stuck. please help.

Yes, but you need to provide three parameter to getRaiseToThePowerOf method,

see your method signature.

[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by lillie field:

Welcome to JavaRanch!

Note that regardless of what you pass to the method as "rpo," that value is lost, because "rpo" is immediately reassigned to 1.
[ April 05, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Rob Spoor

Joined: Oct 27, 2005
Posts: 20194

Correct. Another problem is that you have paths that will return nothing. Observe:

Also, just printing something is not a good idea. Here's a small improvement:

[ April 06, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
lillie field

Joined: Apr 05, 2008
Posts: 2
thanks so much for your help!
I agree. Here's the link:
subject: exponent loop w/out math .pow.
jQuery in Action, 3rd edition