CS 1713 Section 1 Spring 2011, Homework 2

Draw Fibonacci Numbers

Fibonacci numbers come up a lot in mathematics and in nature. The first Fibonacci number, F0, is 0, and the second Fibonacci number, F1, is 1. Each subsequent Fibonacci number is the sum of the previous two, i.e. Fn = Fn-1 + Fn-2.

For this assignment, you will develop several classes:

  1. Fibs, due by 11:59pm, Monday, January 24th - This class will work just like the Primes class discussed in Lecture 2 except that instead of having a nextPrime method that returns the next prime number, it has a nextFib method that returns the next Fibonacci number.

    You can test your Fibs class with the PrintFibs.java program that prints the first 49 Fibonacci numbers, F0 through F48. When you run PrintFibs with your class correctly, the output should look like this:

    0 0
    1 1
    2 1
    3 2
    4 3
    5 5
    6 8
    7 13
    8 21
    9 34
    (a lot more lines deleted for the sake of brevity)
    40 102334155
    41 165580141
    42 267914296
    43 433494437
    44 701408733
    45 1134903170
    46 1836311903
    47 2971215073
    48 4807526976
    
    Notice that the last few numbers listed exceed the maximum value of a Java int, so you will need to use the 64-bit long type instead of int. Do not "hard-code" values of Fibonacci numbers into your Fibs class; your class must work for arbitrarily large Fibonacci numbers that fit into the Java long type.

    What To Turn In

    Turn in your Fibs.java file by emailing it as an attachment to our teaching assistant by 11:59pm, Monday, January 24th.

  2. DrawFibs, due by 11:59pm, Wednesday, January 26th. This class will draw the first 49 Fibonacci numbers, F0 through F48, on a graphics window. The result should look something like this:

    The class DrawFibs should have the following properties:

    1. It should draw a 7×7 grid.
    2. Each Fibonacci number should be drawn in the middle of one of the grid squares.
    3. The numbers should be filled in columns from top to bottom, left to right, just as in the picture.
    4. The numerals should be horizontally centered in the grid squares.
    5. Everything should be drawn correctly even when the window is resized.
    6. Your DrawFibs class must use your Fibs class to get Fibonacci numbers.
    7. Do not "hard-code" values of Fibonacci numbers into your program.
    8. Put informative comments in the code. You do not need to document each statement, but give a comment for each method, each loop and each short related sequence of statements.

    What To Turn In

    Turn in your Fibs.java and DrawFibs.java files by emailing them as two separate attachments in the same email message to our teaching assistant by 11:59pm, Wednesday, January 26th. Make sure to include your Fibs.java file even if you have not changed it. Note that you are encouraged to change it, especially if you didn't do a great job on it before and want to improve it.

  3. DrawPrimeFibs and Primes, due by 11:59pm, Friday, January 28th - The DrawPrimeFibs class will work just like the DrawFibs, with the additional requirement that boxes containing prime Fibonacci numbers will be drawn in reverse video, i.e., white text on a black background instead of black text on a white background. Just like this:

    The Primes class should have the same functionality as the Primes class we discussed in class, but it should work for longs. In particular, it should have a public static boolean method isPrime that returns true if its long parameter is prime, false otherwise.

    Primes should have the same functionality, but you should write it to be more efficient than the Primes we saw in class. Testing the last few Fibonacci numbers for primality will take a very long time using trial division between 2 and n-1, so find a better way to test for primality. We have discussed one such way in class.

    The DrawPrimeFibs should have the same properties as listed above for DrawFibs in addition to the following:

    1. Boxes containing prime Fibonacci numbers should appear with white text on a black background.
    2. When the window is first drawn or resized, everything should be draw quickly, i.e., with a delay that the user barely notices.
    3. Your program must use the Primes class to test for primality. The DrawPrimeFibs class itself should not be doing the computations that test for primality; it should just call Primes.isPrime().

    What To Turn In

    Turn in your Fibs.java, DrawPrimeFibs.java, and Primes.java files by emailing them as three attachments in the same email message to our teaching assistant by 11:59pm, Friday, January 28th.

Hints

Late assignments will not be accepted. If you're not done with some part before it's due, turn in what you have for partial credit. Don't cheat.