I\'m in java one and I need help with this program.Please follow the instruction
ID: 3676164 • Letter: I
Question
I'm in java one and I need help with this program.Please follow the instructions and answer if you know about programming. thank you.
The Fibonacci number series looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
Write a program that allows the user to request a particular Fibonacci number, and then your program will calculate what it is. For instance, if the user requested the 0th Fibonacci number, your program should print 0. If they request the 7th Fibonacci number, your program should print 13. It's strange, but there is a 0th Fibonacci number (0), so make sure your program handles this correctly. The 1st and 2nd Fibonacci numbers are 1, the 3rd Fibonacci number is 2, the 4th Fibonacci number is 3, and so on.
The first two numbers of the Fibonacci number series are always 0 and 1. Each successive number in the series is generated by adding the previous two.
Write a static method fibcalc() that takes one int argument (the requested Fibonacci number) and returns a double value (the actual Fibonacci number itself). Use data type double throughout your program so you can process large Fibonacci numbers. You should be able to ask for Fibonacci number 70 and get 190392490709135 as your result.
Your program needs to validate that it is possible to calculate the Fibonacci number they are requesting. In other words, make sure that they enter a non-negative (>=0) number less than or equal to 70 before proceeding. If an invalid value is input, display the following error message:
Invalid Value – Try Again
Goals
Use your knowledge of methods to solve this program.
Use loops to solve the Fibonacci number series.
Use the double data type to process large Fibonacci numbers.
Points to Think About
- If you call the method with 0, your method can return 0. If you call the method with 1, you can return 1. If you call the program with something larger than 1, you will need to perform the appropriate calculations to determine what the Fibonacci number is.
- Try to have only one return statement in your method.
- You will want to make sure your return data type is double, since the result calculated will be very large.
- Use the appropriate printf(), print() or println() statement to display your Fibonacci number without any decimals or the decimal point itself. In other words, if you request Fibonacci #70, you should output 190392490709135 and not 190392490709135.000000.
-Make sure your program can calculate and display the 71st Fibonacci number (which is Fibonacci #70) as well as the 0th Fibonacci number
(which is Fibonacci #0).
- Do not use material past lecture 14 (book sections 5.1 – 5.4, 5.11 and 5.12) to solve this program.
- You may not use any recursive solutions to solve this program.
- Method fibcalc() will not do any output; it should simply do the math and return the requested Fibonacci number.
- There should be no public or private fields in your clas
Sample Program Run (user input is underlined) s.
Which Fibonacci number would you like? -1
Invalid Input – Try Again
Which Fibonacci number would you like? 93
Invalid Input – Try Again
Which Fibonacci number would you like? 8
Fibonacci #8 is 21
Explanation / Answer
Please find the required solution:
import java.util.Scanner;
//Program to print the nth Fibonacci number
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Which Fibonacci number would you like?");
int input = scanner.nextInt();
scanner.close();
if (input < 0 || input > 71) {
System.out.println("Invalid Input – Try Again");
return;
}
// type cast to long and print the result
System.out.println("number is"+(long) fibcalc(input));
}// end of main method
// compute the nth Fibonacci number
public static double fibcalc(int num) {
double f[] = new double[num + 1];
int i;
f[0] = 0;
if (f.length > 1)
f[1] = 1;
for (i = 2; i <= num; i++) {
f[i] = f[i - 1] + f[i - 2];
}
return f[num];
}// end of method
}// end of class
Sample output:
Which Fibonacci number would you like?
10
number=55
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.