Fibonacci Numbers - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Fibonacci Numbers

Description:

The whole point of this presentation is to give you some idea of how to put ... The example we use is writing a program to ... oops--a bug. 12. Fixing the bug ... – PowerPoint PPT presentation

Number of Views:217
Avg rating:3.0/5.0
Slides: 19
Provided by: davidleem
Category:

less

Transcript and Presenter's Notes

Title: Fibonacci Numbers


1
Fibonacci Numbers
  • A simple example of program design

2
Purpose of this presentation
  • The whole point of this presentation is to give
    you some idea of how to put together the
    components we have so far (declarations,
    assignments, if and while statements) into a
    working program
  • The example we use is writing a program to
    compute and display a Fibonacci sequence

3
Fibonacci sequences
  • A Fibonacci sequence is an infinite list of
    integers
  • The first two numbers are given
  • Usually (but not necessarily) these are 1 and 1
  • Each subsequent number is the sum of the two
    preceding numbers
  • 1 1 2 3 5 8 13 21 34 55 89 144 ...
  • Lets write a program to compute these

4
Starting the Fibonacci sequence
  • We need to supply the first two integers
  • int first 1int second 1
  • We need to print these out
  • System.out.print(first " ")System.out.print(se
    cond " ")
  • We need to compute and print the next number
  • int next first secondSystem.out.print(next
    " ")

5
Taking the next step
  • We need to compute and print the next number
  • int next first secondSystem.out.print(next
    " ")
  • Now what?
  • We don't want to make up a lot more names
  • If we use a loop, we must reuse names

6
Preparing for another step
  • This computation gave us our third number
  • int next first secondSystem.out.print(next
    " ")
  • The sequence so far is first second next
  • To get another number, we need second next
  • Variable first is no longer useful for anything
  • Lets move values around so that first second
    does the job we need

7
Preparing to make many steps
  • We need to make these moves
  • We can do it like this
  • first secondsecond next
  • We can put these statements in a loop and do them
    as many times as we please

8
The program so far
int first 1int second 1System.out.print(fi
rst " ")System.out.print(second " ")while
( ? ? ? ) // when do we stop? int next
first second System.out.print(next "
") first second second next
9
Deciding when to stop
  • Suppose we stop when we get to a number thats
    1000 or bigger
  • So we continue as long as the number is less than
    1000
  • while (next
  • Question is the final number printed greater
    than 1000 or less than 1000?
  • while (next second System.out.print(next " ")
    first second second next

10
One other minor detail
  • We have been printing the numbers all on one line
  • Well get to 1000 quickly enough, so we wont
    have a terribly long line
  • For neatness sake, we really ought to end the
    line (rather than hoping Java does it for us)
  • System.out.println( )

11
The program so far
int first 1int second 1System.out.print(fi
rst " ")System.out.print(second " ")while
(next second System.out.print(next " ")
first second second nextSystem.out.pr
intln( )
// oops--a bug
12
Fixing the bug
  • The first time we see the variable next, its in
    the test of the while loop
  • while (next
  • next hasnt been given a value yet
  • next hasnt even been declared!
  • Solution declare next up with the other
    variables, and give it some reasonable initial
    value

13
The (fixed) program so far
int first 1int second 1int next
2System.out.print(first " ")System.out.print
(second " ")while (next first second // "int" was removed
System.out.print(next " ") first
second second nextSystem.out.println(
)
14
Finishing up
  • We have the commands we need, but we do not have
    a complete application
  • We need to put the commands into a method
  • We need to put the method into a class
  • The next slide shows the extra stuff we need, but
    doesnt explain it

15
The box our program goes in
public class Fibonacci public static void
main(String args) (code goes here)

If we were to use multiple methods, we would want
a more complex box to avoid issues with static
16
The complete final program
public class Fibonacci public static void
main(String args) int first 1
int second 1 int next 2
System.out.print(first " ")
System.out.print(second " ") while
(next second System.out.print(next "
") first second
second next
System.out.println( )
17
Where is it?
  • When we run Eclipse, we declare some folder to be
    a workspace
  • All our projects go into this (or some other)
    workspace
  • To create a new program in Eclipse, we must
    first create a project to hold all our classes
  • Eclipse will create a directory with the same
    name as the project
  • You should not rename this directory
  • When you create a class in Eclipse, such as
    Fibonacci, it will be put in a file with the same
    name but with the .java extensionfor example,
    Fibonacci.java
  • You must not change the name of this file!
  • Java uses the file names to find the classes it
    needs

18
The End
Always code as if the guy who ends up
maintaining your code will be a violent
psychopath who knows where you live.

--Martin Golding
Write a Comment
User Comments (0)
About PowerShow.com