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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Expert Solution
ALGORITHM :-
  • 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

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education