



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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
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
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)
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 } }