File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exponent loop w/out math .pow.

 
lillie field
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
lillie field
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic