Purpose To become familiar with throwing and catching exceptions. Directions: The Fibonacci series is a famous sequence of numbers that occurs often in nature, architecture, painting, etc. (you can more read about it on Wikipedia if you are interested). The sequence is defined as follows: • The first Fibonacci number is 1 • The second Fibonacci number is also 1 • The nth Fibonacci number is the sum of the previous two numbers. The first few numbers in the sequence are therefore: 1, 1, 2, 3, 5, 8, 13, 21, 34, … Your task is to write a method called fibonacci that accepts an integer argument called n and returns the nth Fibonacci number. Because this method only makes sense for positive values of n, your method should throw an exception (which you will need to create) of type InvalidArgumentException. Your method should have the following signature: public static int fibonacci(int n) throws InvalidArgumentException Hint: One way to implement the fibonacci method is to have two variables – one to keep track of the previous fibonacci number and another to keep track of the one before the previous one. Then you can update these values inside a loop. You also need to create a class called Driver.java with a main method. In main: Prompt the user with the statement “Enter the desired Fibonacci number n: “ Read in the user’s response. If the user enters something that is not a number, catch the associated exception and display the message “Error: you must enter a number” to standard error. If the user enters an integer, call the fibonacci method and display the result. If an InvalidArgumentException occurs, catch it from main and display the associated message to standard error. Note: you must use exceptions and try-catch blocks for this assignment. You will not get any credit if you only use if statements. Example output: Enter the desired Fibonacci number n: 9 34 Enter the desired Fibonacci number n: bob Error: you must enter a number Enter the desired Fibonacci number n: -2 Error: -2 is not a positive number
Purpose To become familiar with throwing and catching exceptions. Directions: The Fibonacci series is a famous sequence of numbers that occurs often in nature, architecture, painting, etc. (you can more read about it on Wikipedia if you are interested). The sequence is defined as follows: • The first Fibonacci number is 1 • The second Fibonacci number is also 1 • The nth Fibonacci number is the sum of the previous two numbers. The first few numbers in the sequence are therefore: 1, 1, 2, 3, 5, 8, 13, 21, 34, … Your task is to write a method called fibonacci that accepts an integer argument called n and returns the nth Fibonacci number. Because this method only makes sense for positive values of n, your method should throw an exception (which you will need to create) of type InvalidArgumentException. Your method should have the following signature: public static int fibonacci(int n) throws InvalidArgumentException Hint: One way to implement the fibonacci method is to have two variables – one to keep track of the previous fibonacci number and another to keep track of the one before the previous one. Then you can update these values inside a loop. You also need to create a class called Driver.java with a main method. In main: Prompt the user with the statement “Enter the desired Fibonacci number n: “ Read in the user’s response. If the user enters something that is not a number, catch the associated exception and display the message “Error: you must enter a number” to standard error. If the user enters an integer, call the fibonacci method and display the result. If an InvalidArgumentException occurs, catch it from main and display the associated message to standard error. Note: you must use exceptions and try-catch blocks for this assignment. You will not get any credit if you only use if statements. Example output: Enter the desired Fibonacci number n: 9 34 Enter the desired Fibonacci number n: bob Error: you must enter a number Enter the desired Fibonacci number n: -2 Error: -2 is not a positive number
- Create a driver class.
- Create a main method inside the driver class.
- Read number as a user input inside the try block.
- If the entered number is not a number, throw InputMismatchException and catch it in a catch block and print the error message.
- Else if the entered number is not a positive number, throw IllegalArgumentExcepion and catch it in a catch block inside main method and print the error message.
- Call fibonacci mehod and pass n as a parameter.
- Implement fibonacci method and return the nth fibonacci number which is then printed inside the main method.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images