Fibonacci Sequence Program with User-Specified Golden Ratio Tolerance, Assignments of Computer Science

A java programming assignment where students are required to write a program that calculates fibonacci numbers using a while or do-while loop, and continues to calculate new numbers until the ratio of the last two numbers is within a user-specified tolerance of the golden ratio. Instructions for setting up the program, as well as grading criteria and sample sessions.

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-ze4-1
koofers-user-ze4-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Program 6
Learning Objectives
Learn to implement a while or do-while loop
Increment a counter within a loop
Use a value obtained from the user to signal the end of a loop
Program 6 Assignment
The Fibonacci numbers are a sequence of integers named after the Italian mathematician
Leonardo Fibonacci. The first number, (f0), is defined as 0. The second, (f1), is defined as 1. The
third is the sum of the first two. Each subsequent number in the sequence is the sum of the
previous two. The ratio of two consecutive Fibonacci numbers converges to the golden ratio.
You may refer to the following page in Wikipedia for an explanation of the Fibonacci sequence
and its relation to the golden ratio:
http://en.wikipedia.org/wiki/Fibonacci_number
1. Write a program with three integer variables to represent three sequential Fibonacci
numbers. The first should be initialized to 0, the second initialized to 1 and the third is
the sum of the first two.
2. Declare a counter to keep track of how many Fibonacci numbers have been calculated;
this should be initialized to 3 since the first three are calculated when they are declared.
3. The actual golden ratio is set as a constant at the top of the main method. Note that for
this program only one file is required. It is the file named Fibonacci.java; this class has
all the code including the main method.
4. Prompt the user for a double value which is the maximum allowed difference between the
ratio of the last two Fibonacci numbers calculated and the actual golden ratio.The
program should continue to calculate additional numbers in the sequence until the ratio is
as close to the golden ratio as the user has specified.
5. Write a while OR do-while loop which correctly re-assigns the current Fibonacci
numbers and calculates and displays the next number in the sequence.
o Calculate the ratio of the last two numbers
oDetermine the absolute value of the difference between this ratio and the actual
golden ratio.
oUpdate a counter to keep track of how many Fibonacci numbers have been
calculated.
oThe loop should continue to calculate additional numbers in the sequence until the
absolute value of the (ratio – GOLDEN_RATIO) is less than the difference the
user has chosen. The Math.abs( ) method, with ( ratio – GOLDEN_RATIO) as
the argument of the method, can be used to determine the absolute value.
6. After the loop has terminated, display information similar to the sample output at the
bottom of this assignment.
7. The skeleton code for Fibonacci.java is at the very end of this assignment.
Note: This assignment only requires one class file that contains the main method. You do not
need to create two files for Program 6. You need only create Fibonacci.java, which contains the
main method.
pf3
pf4
pf5

Partial preview of the text

Download Fibonacci Sequence Program with User-Specified Golden Ratio Tolerance and more Assignments Computer Science in PDF only on Docsity!

Program 6

Learning Objectives

 Learn to implement a while or do-while loop  Increment a counter within a loop  Use a value obtained from the user to signal the end of a loop

Program 6 Assignment

The Fibonacci numbers are a sequence of integers named after the Italian mathematician Leonardo Fibonacci. The first number, (f 0 ), is defined as 0. The second, (f 1 ), is defined as 1. The third is the sum of the first two. Each subsequent number in the sequence is the sum of the previous two. The ratio of two consecutive Fibonacci numbers converges to the golden ratio. You may refer to the following page in Wikipedia for an explanation of the Fibonacci sequence and its relation to the golden ratio: http://en.wikipedia.org/wiki/Fibonacci_number

  1. Write a program with three integer variables to represent three sequential Fibonacci numbers. The first should be initialized to 0, the second initialized to 1 and the third is the sum of the first two.
  2. Declare a counter to keep track of how many Fibonacci numbers have been calculated; this should be initialized to 3 since the first three are calculated when they are declared.
  3. The actual golden ratio is set as a constant at the top of the main method. Note that for this program only one file is required. It is the file named Fibonacci.java; this class has all the code including the main method.
  4. Prompt the user for a double value which is the maximum allowed difference between the ratio of the last two Fibonacci numbers calculated and the actual golden ratio.The program should continue to calculate additional numbers in the sequence until the ratio is as close to the golden ratio as the user has specified.
  5. Write a while OR do-while loop which correctly re-assigns the current Fibonacci numbers and calculates and displays the next number in the sequence. o Calculate the ratio of the last two numbers o Determine the absolute value of the difference between this ratio and the actual golden ratio. o Update a counter to keep track of how many Fibonacci numbers have been calculated. o The loop should continue to calculate additional numbers in the sequence until the absolute value of the (ratio – GOLDEN_RATIO) is less than the difference the user has chosen. The Math.abs( ) method, with ( ratio – GOLDEN_RATIO) as the argument of the method, can be used to determine the absolute value.
  6. After the loop has terminated, display information similar to the sample output at the bottom of this assignment.
  7. The skeleton code for Fibonacci.java is at the very end of this assignment. Note: This assignment only requires one class file that contains the main method. You do not need to create two files for Program 6. You need only create Fibonacci.java, which contains the main method.
  • Sample session
  • Sample session

What to turn in

  1. Fibonacci.java program printouts
  2. Be sure your program file is saved in your U:..\Program6\ folder on the computer science network. The assignment is due at the start of next Thursday’s lab.

Grading Criteria

50 points maximum  35 points - Output Correctness o The program correctly reads in the maximum allowed difference from the user (5) o The program correctly calculates and displays the Fibonacci numbers (5) o The program correctly tracks the number of Fibonacci numbers calculated (5) o The program correctly calculates the ratio of the last two Fibonacci numbers calculated. (5) o The program correctly calculates the absolute value of the difference between the ratio and the golden ratio(5) o The program terminates the loop correctly when the exit condition is met. (5) o Using the DecimalFormat class, the program correctly formats, with commas every third digit, Fibonacci numbers greater than 999 (3) o The program correctly ensures that the user enters a double value (2)  5 points - Comments o Complete header comments are present and complete (2)

o Inline comments summarize chunks of code (2)  Include single-line Java comments to improve program readability  Skip a blank line before every single-line comment o Alignment and indentation are correct (1)  Use the previous programs and the CS 110 Java Programming Style Guide for guidance.  Avoid line wrap  5 points - Identifier Names o Use meaningful identifier names. Avoid the use of abbreviations in forming identifier names. o Class names should begin with an upper case letter, and each subsequent word should start with an upper-case letter. o All variable identifier names should begin with a lower case letter, and each subsequent word should start with an upper-case letter.  5 points - Creation of Assignment Folder, File, and Printouts o Java program file saved as U:...\Program6\Fibonacci.java (1) o Program printout submitted correctly (4)

Beginning for Fibonacci.java file:

import java.util.Scanner; import java.text.DecimalFormat; public class Fibonacci { public static void main(String[] args) { final double GOLDEN_RATIO = 1.618033988749895; int fib1 = 0; //first of three sequential Fibonacci numbers int fib2 = 1; //second of three sequential Fibonacci numbers int fib3 = fib1 + fib2; //third of three sequential Fibonacci numbers int numberOfFibs = 3; //number of Fibonacci numbers calculated double ratio = (double) fib3/fib2; //ratio of the last two Fibonacci numbers // …the remainder is for you } }