File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exponent loop  w/out math .pow." Watch "exponent loop  w/out math .pow." New topic
Author

exponent loop w/out math .pow.

lillie field
Greenhorn

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;
}

else
{
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.


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

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
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
lillie field
Greenhorn

Joined: Apr 05, 2008
Posts: 2
thanks so much for your help!
 
 
subject: exponent loop w/out math .pow.
 
Similar Threads
mechanism of equals(Object)?
TIJ 3 problem at simpeltest
Fibonacci
trouble with operator
[HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done