• 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

Here is my code for Fibonacci num. I get the right output. Can you please check is there any flaw?

 
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks correct to me.

As a matter of style, I would rather that the fibonacci method not be static.  See MainIsAPain (that's a link).
 
Saloon Keeper
Posts: 3415
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:As a matter of style, I would rather that the fibonacci method not be static.  See MainIsAPain (that's a link).


Why not? Why woud you need an instance to calculate a Fibonacci?

@Divyadharshini
Your code is according to the definition. But to make it a bit more flexible, can you add the two startvalues as parameters to your method?

And a serious problem with this code is that it is very inefficient. Suppose you want to calculate the sixth Fibonacci number. Can you determine how often a Fibonacci is calculated?
 
Rancher
Posts: 3305
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Knute - yeah, I agree with Piet - and I don't think MainIsAPain really addresses this question.  I know there's an argument to make almost all methods non-static for testability, to facilitate mocking them out.  But it also makes for a harder-to-use API in some cases, where there's no useful instance data to be encapsulated in an instance.
 
Knute Snortum
Sheriff
Posts: 6127
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!