• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Newbie "Fibonacci Sequence"

 
Greenhorn
Posts: 13
Mac Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Again. To reiterate, I am a newbie attempting to learn Java through iTunes Stanford CS106a and Eric Roberts' "Art and Science of Java." I am not a student nor am I enrolled in any class. I am attempting a programming exercise (chp. 4, ex. 9) to display the values in the Fibonacci sequence from F0 to F15. I understand the concept, but, for some reason my equation is simply creating a resulting string of numbers that simply increase by 2's. Very frustrating. As, I know it is supposed to be the sum of the previous F and the F that precedes that one to total the new F number. It seems so simple yet I seem to be far off. As usual, I have worked my code for your review. Any and all help is always greatly appreciated!

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jb Anderson wrote:I understand the concept, but, for some reason my equation is simply creating a resulting string of numbers that simply increase by 2's.


Those two statements sound contradictory.

As, I know it is supposed to be the sum of the previous F and the F that precedes that one to total the new F number.


Yes, but what is F? It definitely isn't the current index, which is all your program is working with.

My suggestion: StopCoding (←click), and have another look at the formula. If you're still having problems, come back and we can help you further.

Tip: You might want to think about using an array (or a List) to keep previous values of 'F', but it's not strictly necessary.

Winston
 
Ranch Hand
Posts: 69
IntelliJ IDE Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JB, I'm not able to find that course in iTunes. Is there a link?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston is right. From looking at your code I think you have misunderstood how the Fibonacci Sequence is derived.

The formula is Fn = Fn-1 + Fn-2. With seed values F0=0, F1=1.

Start small. Get your code working for small values of n.
Start at n=0. What is the expected outcome? What about where n=1?
n=0. Sequence [0]
n=1. Sequence [0,1]
n=2. Sequence [0,1,1]
n=3. Sequence [0,1,1,2]

Take a look at how you are calculating the value of Fn. Are you calculating Fn = Fn-1 + Fn-2? Or are you calculating Fn = (n-1) + (n-2)?

I would also recommend that you base your if conditions on the current value of f rather than total, it's not clear what the significance of it being less than 0 and whether it's -3 or not is. Given that the value of total, which should be the current Fib sequence value, is not right then when you fix that then your if condition will fall apart causing you more confusion.
 
Greenhorn
Posts: 8
Eclipse IDE Java ME Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your variable SENTINEL will print up-to F16 though you need F15, make it 15.
 
Jb Anderson
Greenhorn
Posts: 13
Mac Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

Thank you very much for your reply. I was hoping for someone to evaluate my code and then nudge me so I could see what I was actually doing and how it differed from what I needed to do. Your response has greatly helped me re-focus my approach. Not being a "math-person" I was getting a bit flummoxed with the actual Fibonacci equation which then resulted in a flawed translation to Java coding.

Thank you, Tim, for taking the time to examine my code and realize what I needed to be told so I could refine things on my own.

I will go back to the woodshed and re-post with (hopefully) a successful program.

-JB
 
Jb Anderson
Greenhorn
Posts: 13
Mac Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mr. Pandey,

Thanks for pointing out that mistake. It was an oversight on my part. I appreciate your input.

-JB
 
Jb Anderson
Greenhorn
Posts: 13
Mac Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Douglas,

The course can be found on iTunes U. It won't be listed in the "normal" iTunes storefront. Download the iTunesU app to access the available courses.

-JB
 
Douglas Knapp
Ranch Hand
Posts: 69
IntelliJ IDE Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, JB. I haven't yet wrapped my head around iTunes U.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jb, How are you getting on?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic