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:
- 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:
(a lot more lines deleted for the sake of brevity)
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.
- 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
The class DrawFibs should have the following properties:
- It should draw a 7×7 grid.
- Each Fibonacci number should be drawn in the middle of one of the grid squares.
- The numbers should be filled in columns from top to bottom, left to right, just as in the picture.
- The numerals should be horizontally centered in the grid squares.
- Everything should be drawn correctly even when the window is resized.
- Your DrawFibs class must use your Fibs class to get Fibonacci numbers.
- Do not "hard-code" values of Fibonacci numbers into your program.
- 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.
- 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:
- Boxes containing prime Fibonacci numbers should appear with white text on a black background.
- When the window is first drawn or resized, everything should be draw
quickly, i.e., with a delay that the user barely notices.
- 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.
- Get started early. Have you started yet? No? Get started now.
- The first part of the assignment is due on Monday. That doesn't mean
you should wait until Tuesday to start the second part; make a non-functional
"stub" version of Fibs.java and develop both parts in parallel.
By Monday you should have Fibs.java ready to turn in and be well
on your way to finishing DrawFibs.java
- You cannot complete this program by copying-and-pasting
from things we've seen in class. Programming doesn't work
that way. This program will require thought and original
programming by you. However, looking at some of the code in Lecture 2
will give you an idea of the methods in the Graphics class you
will need to use.
- Your instructor and teaching assistant are happy to help you if you
come to office hours or seek help through email. You would be surprised
how much help that can be. If you don't ask us for help and can't figure
out this assignment, don't blame us.
- How do you make a number from 0 through 48 into a column from 0
through 6 and a row from 0 through 6? Divide by 7; the quotient is the
row number and the remainder is the column number.
- When you write your applet's HTML file for testing, what should the
width and height be? You'll have to test it yourself, but the graphics
in this web page might help you. Right click on them and see what you
can find out.
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.