• Post Reply Bookmark Topic Watch Topic
  • New Topic

power without Math.power -- method returning always 0.0  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I try to implement by hand a method calculating a power (with a positive exponent here). I don't know why the method always return 0.0. For exemple, in the following code, I'm expecting 2^3 to be 8.
Any clue about why it is not working ? Thanks in advance



 
Vaibhav Sagar
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the code on line 14 is wrong(inside the for loop). result = result *n means that you are setting result = 0*3, because result is an instance field that was initialized to 0.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you should not be using any static field at all. Just return the result.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check your expression. result * n does not do what you think it does.

2 to the power 5 is (2 * 2 * 2 * 2 * 2) which is 32
If base is 2 and exponent is 5, is base * exponent the correct formula?
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also think about the case what the answer would be if the number to the power of 0. That wouldn't work as expected in your case.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all, I overlooked lot of things. Yes, static variable is ugly here.

Now this is working :

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Also think about the case what the answer would be the number to the power of 0. That wouldn't work expected in your case.

...or negative.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Also think about the case what the answer would be the number to the power of 0. That wouldn't work expected in your case.

Here it was meant just for positive exponent, but sure case where exponent = 0 or <0 should be dealt with.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some other things.

Class names suppose to start with an upper case. Please fix that.
Another thing, don't place [] after variable name, place it after the type.
Because if you ever need to use variable arguments public static void main (String args...), compiler would produce you an error.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line also contains lots of issues. Mainly indentation and some formatting issues too. That line should look like:
or
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what is triggering people to put as many as possible lines to 1 line.
Less lines does not mean better or more readable. Latter is most likely opposite.

Some languages as JavaScript uses such approach in order to optimize code and put everything in one line by removing all white spaces, comments, so the script file would be as small as possible.
You should not be worrying about that when you write Java code.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's such a tiny code that I did not put so much efforts in its presentation but I'll definitely stick to the good practises.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you clean it up, remember Single Responsibility Principle: classes/methods should do one and only one thing well. Display and calculation are mixed in your power method. Put the System.out statement in your main method instead of in power. The power method should just be concerned with the calculation, that's all.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:It's such a tiny code that I did not put so much efforts in its presentation but I'll definitely stick to the good practises.
No worries

And welcome to the Ranch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!