First, we'll begin with your code from the previous lab that printed a pattern of stars. Specifically, we want to break down the code into a number of methods:
getPositiveInt: This method should require the user to input a positive integer. Once it has obtained that integer, it should return it.
printRow: This method should print the number of asterisks (*) provided by the caller on a single row.
printTriangle: This method should print an entire triangle, where the size of the triangle (that is, the number of rows) is provided by the caller.
printPattern: This method should print the full pattern of triangles, where each triangle is one size smaller than the previous one, down to a triangle that is just one asterisk. The number of triangles to print (and thus the size of the first, largest triangle) is provided by the caller.
After writing these methods, the main method should be modified to call first the getPositiveInt method, and then to pass the result of that method call to printPattern in order to get the pattern to appear.
To get started on this project, you should create a lab-4 directory and then make a copy of your lab-3 code. From your home directory, the commands should be:
mkdir lab-4
cp lab-3/Triangles.java lab-4
cd lab-4
You will now be in your lab-4 directory with a copy of your Triangles.java code to modify.
The Fibonacci numbers are the following infinite sequence of integers:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 ...
More specifically, if the function F(n) provides the nth Fibonacci number, then:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2)
Your assignment is to write a wholly new program named Fibonacci that does the following:
Calls a method getNonNegativeInt to obtain a non-negative integer k from the user. (Hint: Copy and modify your getPositiveInt method from the Triangles portion of this lab.)
Calculate the kth Fibonacci number with a recursive method fib that you must write.
Print the computed kth Fibonacci number for the user.
Hint: The hard part here is the fib method. Remember, don't think mechanically, think functionally -- that is, assume that when you call a method (even the one that you are writing) that it will do what it is supposed to do. Later, if your method has bugs, then you should think mechanically about how the program is running.
When your work is complete, you should submit it using the cs11-submit command, like so:
cs11-submit lab-4 Triangles.java Fibonacci.java