posted 3 years ago

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!

posted 3 years ago

Those two statements sound contradictory.

Yes, but what is F? It definitely

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

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Douglas Knapp

Greenhorn

Posts: 26

posted 3 years ago

- 2

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.

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.

Tim Driven Development

posted 3 years ago

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

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

Douglas Knapp

Greenhorn

Posts: 26