The book name is Java An introduction to problem sloving 7ed Plz give the code i
ID: 3853752 • Letter: T
Question
The book name is Java An introduction to problem sloving 7ed
Plz give the code in java and it match with the prof output below. Plz provide your output too.
The Assignment:
Chapter 11:
Programming Projects 3: This project is found starting on page 861.
Assignment Guidelines:
The Fibonacci sequence occurs frequently in nature as the growth rate for certain idealized animal populations. The sequence begins with 0 and 1, and each successive Fibonacci number is the sum of the two previous Fibonacci numbers. Hence, the first ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The third number in the series is 0 + 1, which is 1; the fourth number is 1 + 1, which is 2; the fifth number is 1 + 2, which is 3; and so on.
Besides describing population growth, the sequence can be used to define the form of a spiral. In addition, the ratios of successive Fibonacci numbers in the sequence approach a constant, approximately 1.618, called the “golden mean.” Humans find this ratio so aesthetically pleasing that it is often used to select the length and width ratios of rooms and postcards.
Use a recursive formula to define a static method to compute the nth Fibonacci number, given n as an argument. Your method should not use a loop to compute all the Fibonacci numbers up to the desired one, but should be a simple recursive method. Place this static recursive method in a program that demonstrates how the ratio of Fibonacci numbers converges. Your program will ask the user to specify how many Fibonacci numbers it should calculate. It will then display the Fibonacci numbers, one per line. After the first two lines, it will also display the ratio of the current and previous Fibonacci numbers on each line. (The initial ratios do not make sense.) The output should look something like the following if the user enters
5:
Fibonacci #1 = 0
Fibonacci #2 = 1
Fibonacci #3 = 1; 1/1 = 1
Fibonacci #4 = 2; 2/1 = 2
Fibonacci #5 = 3; 3/2 = 1.5
Notes:
The recursive algorithm for Fibonacci numbers is a little more involved than the series calculations in the previous Projects. Base cases for 0, 1 or two numbers simply return a value, and all other numbers make two recursive calls to get the previous two Fibonacci numbers to add together to obtain the current number. The method to calculate a Fibonacci number is recursive, but the code to print the output is not; it uses a for-loop to cycle through the Fibonacci numbers and ratios.
Note:
The assignment must have the following classes:
1. fibonacci, in file fibonacci.java
2.fibonacciDemo, in file fibonacciDemo.java
Sample Run:
----jGRASP exec: java Fibonacci
How many Fibonacci numbers do you want to use?
Enter a positive integer:
25
Here is the Fibonacci series and ratio of current
number to previous number for 25 Fibonacci numbers:
Fibonacci #1 = 0
Fibonacci #2 = 1
Fibonacci #3 = 1 and ratio = 1/1 = 1.0
Fibonacci #4 = 2 and ratio = 2/1 = 2.0
Fibonacci #5 = 3 and ratio = 3/2 = 1.5
Fibonacci #6 = 5 and ratio = 5/3 = 1.6666666
Fibonacci #7 = 8 and ratio = 8/5 = 1.6
Fibonacci #8 = 13 and ratio = 13/8 = 1.625
Fibonacci #9 = 21 and ratio = 21/13 = 1.6153846
Fibonacci #10 = 34 and ratio = 34/21 = 1.6190476
Fibonacci #11 = 55 and ratio = 55/34 = 1.617647
Fibonacci #12 = 89 and ratio = 89/55 = 1.6181818
Fibonacci #13 = 144 and ratio = 144/89 = 1.6179775
Fibonacci #14 = 233 and ratio = 233/144 = 1.6180556
Fibonacci #15 = 377 and ratio = 377/233 = 1.6180258
Fibonacci #16 = 610 and ratio = 610/377 = 1.6180371
Fibonacci #17 = 987 and ratio = 987/610 = 1.6180328
Fibonacci #18 = 1597 and ratio = 1597/987 = 1.6180345
Fibonacci #19 = 2584 and ratio = 2584/1597 = 1.6180338
Fibonacci #20 = 4181 and ratio = 4181/2584 = 1.618034
Fibonacci #21 = 6765 and ratio = 6765/4181 = 1.618034
Fibonacci #22 = 10946 and ratio = 10946/6765 = 1.618034
Fibonacci #23 = 17711 and ratio = 17711/10946 = 1.618034
Fibonacci #24 = 28657 and ratio = 28657/17711 = 1.618034
Fibonacci #25 = 46368 and ratio = 46368/28657 = 1.618034
Do again? Enter 'y' for yes.
n
----jGRASP: operation complete.
Explanation / Answer
//fibonacci.java
//Class fibonacci definition
public class fibonacci
{
//Method to return fibonacci number using recursion
public static int fibonacciSeries(int number)
{
//If the number is zero return zero
if(number == 0)
{
return 0;
}//End of if
//If the number is one return one
if(number == 1)
{
return 1;
}//End of if
//If the number is two return one
if (number == 2)
{
return 1;
}//End of if
//Recursively call the method to generate fibonacci number and returns it
return fibonacciSeries(number - 1) + fibonacciSeries(number -2); //Tail Recursion
}//End of method
//Method to call displayFibonacci till the number passed as parameter to display fibonacci numbers
public static void displayFibonacci(int number)
{
//To store the current recursion value
int currentResult = 1;
//To store the previous recursion value
int previousResult = 1;
//Loops till the number passed as parameter minus one
for (int c = 0; c < number; c++)
{
//Stores the current recursion value by calling the method fibonacciSeries(c)
currentResult = fibonacciSeries(c);
//If counter value is one
if(c == 1)
//Previous and current result is same
previousResult = currentResult;
//If the counter value is more than one
if(c > 1)
{
//Divides the current recursion value by previous recursion value and stores the quotient
float result = (float)currentResult / previousResult;
//Displays the result
System.out.printf(" Fibonacci # %d = %d; %d / %d = %f ", c+1, currentResult, currentResult, previousResult, result);
//Stores the current recursion value in previous recursion variable
previousResult = currentResult;
}//End of if
//If the counter is less than one
else
System.out.printf(" Fibonacci # %d = %d ", c+1, currentResult);
}//End of for loop
}//End of method
}//End of class
//fibonacciDemo.java
import java.util.*;
//Driver class fibonacciDemo definition
public class fibonacciDemo
{
//Main method definition
public static void main(String pyari[])
{
//To store how many Fibonacci number user wants
int number;
//To store user choice
char ch;
//Scanner class object created to accept user data
Scanner sc = new Scanner(System.in);
//Loops till user enters n or N
do
{
//Accepts how many Fibonacci numbers user wants
System.out.println("How many Fibonacci numbers do you want to use? Enter a positive integer: ");
number = sc.nextInt();
//Calls the method to generate fiboncacci series
fibonacci.displayFibonacci(number);
//Accepts user choice
System.out.println(" Do again? Enter 'y' for yes. 'n' for no");
ch = sc.next().charAt(0);
//If choice is N or n exit
if(ch == 'N' || ch == 'n')
break;
}while(true);
System.out.println("----jGRASP: operation complete.");
}//End of main method
}//End of class
Sample Run:
How many Fibonacci numbers do you want to use?
Enter a positive integer:
7
Fibonacci # 1 = 0
Fibonacci # 2 = 1
Fibonacci # 3 = 1; 1 / 1 = 1.000000
Fibonacci # 4 = 2; 2 / 1 = 2.000000
Fibonacci # 5 = 3; 3 / 2 = 1.500000
Fibonacci # 6 = 5; 5 / 3 = 1.666667
Fibonacci # 7 = 8; 8 / 5 = 1.600000
Do again? Enter 'y' for yes. 'n' for no
y
How many Fibonacci numbers do you want to use?
Enter a positive integer:
11
Fibonacci # 1 = 0
Fibonacci # 2 = 1
Fibonacci # 3 = 1; 1 / 1 = 1.000000
Fibonacci # 4 = 2; 2 / 1 = 2.000000
Fibonacci # 5 = 3; 3 / 2 = 1.500000
Fibonacci # 6 = 5; 5 / 3 = 1.666667
Fibonacci # 7 = 8; 8 / 5 = 1.600000
Fibonacci # 8 = 13; 13 / 8 = 1.625000
Fibonacci # 9 = 21; 21 / 13 = 1.615385
Fibonacci # 10 = 34; 34 / 21 = 1.619048
Fibonacci # 11 = 55; 55 / 34 = 1.617647
Do again? Enter 'y' for yes. 'n' for no
n
----jGRASP: operation complete.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.