Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

exponent loop w/out math .pow.

 
Greenhorn
Posts: 2
  • 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.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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 ]
 
Sheriff
Posts: 21773
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much for your help!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!