Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment: Write a set of algorithms and programs to compute and create three tables for the Fibonacci numbers: one recursive, one fast recursive, and one

Assignment: Write a set of algorithms and programs to compute and create three tables for the Fibonacci numbers: one recursive, one fast recursive, and one iterative. Refer to problem E13.20 on page 624, which expands section 13.3 - the RecursiveFib on pages 598-600 - for how to recursively-compute and fast recursively-compute the Fibonacci numbers, and pages 601-602 for an iterative solution (LoopFib). This assignment will build on some of the same materials used in Assignment 5. No graphics permitted.

Provide a FibDemo class that will produce the three tables of n Fibonacci numbers, three classes (FibSequence, FastFibSequence, and LoopFibSequence) that each implements the next() method as provided in a Sequence interface

public interface Sequence { int next( ); }

- same as used in Assignment 5, and exception handling. The number of Fibonacci numbers to be provided in the tables (n) will be input from a file and the three tables themselves will be output to another file - both file names to be found on the command-line upon execution of the FibDemo class.

Output: The output file will contain one table of recursively-computed Fibonacci integers; followed by a second table of expected values - using an iterative algorithm like LoopFib; followed by a third table of fast recursively-computed Fibonacci integers. The output file name will be specified on the command-line.

All tables will be labeled, values right-aligned, square-ish, and obviously have identical values. Additionally, the execution time (in microseconds or nanoseconds) to compute all of the values for each table will be printed/labeled, following each table, for comparison purposes - referring to section 14.2 (page 634) or http://www.tutorialspoint.com/java/java_date_time.htm may prove useful. All output will be handled by the Demo or exception-handling classes.

Note: Output is to be integers. Under some combinations of values from the input file, one or more of the computed Fibonacci values may cause arithmetic overflow. This exception must be handled. Do not output the overflow value; instead, your output should a string of * equal to the length of the overflow value.

Input: The name of the input file and the output file (with or without extensions) will be specified on the command-line. Input validation and exception handling are expected. All input will be handled by the Demo class. Instructions for compilation/execution should include command-line input details. The first integer in the input file is where the Fibonacci sequence will start, beginning with that Fibonacci number. The second integer in the input file will be the number of Fibonacci numbers to be displayed in the tables. The integer values in the input file will be [1-10] & [1-39], respectively, and inclusive. A newline will separate the two integer values. Instructions for compilation/execution should include command-line input details and requirements, and document that the Sequence interface, and the input file, need to be in the same directory with your source code.

Requirements: Use only material covered in the first fourteen chapters. Style requirements as discussed in class expected. Efficiency should always be considered especially in the fast recursive algorithm. Choose the most appropriate loop/decision structures and variable types. No switch or breaks statements allowed. No Magic numbers! Class design guidelines as discussed in class and described in chapters 8/12 expected. Import libraries as required.

You will use the Sequence interface [containing the next() method] as defined in Worked Example 10.1. You must write at least five programs: three will be the FibSequence, FastFibSequence, and LoopFibSequence classes that will each implement the interface; one will be the FibDemo class that will perform the demonstration; and one or more classes to handle user-defined exceptions; and perhaps a table class, or other classes, as required.

FibDemo will access the input (and output) files and read/validate the entries. A trio of separate loops will then be executed, requesting the next Fibonacci number the requisite number of times. System time, before and after each loop, will facilitate determining execution time for each table.

At a minimum, exceptions for File-Not-Found, Empty-File, Non-Integer-Input, and Invalid-Input (negative/zero, greater-than-maximum, invalid number of arguments) will be handled for input/output. Invalid-Input will be a user-defined exception class - section 11.4.5 (page 540-541) may prove useful. Other exceptions, like arithmetic overflow, will be included, as required - section 11.5 (page 545) may prove useful. All files must be opened/closed properly, even if there are exceptions. User-defined exception classes will use the class documentation standards. All exception/error messages should go to the screen and should be instructive: what is invalid, usage example, exact error, etc. Include the PrintStackTrace(), where appropriate. No abnormal terminations allowed be sure to gracefully exit your program where appropriate!

Each FibSequence class will implement its own next() method and any other methods/instance variables required to create the sequence of Fibonacci numbers.

Execution could look like: $ java FibDemo infile outfile

image text in transcribed

image text in transcribed

image text in transcribed

What it should look like.

section_3/LoopFib.java 1 import java.util.canner; 2 This program computes Fibonacci numbers using an iterative method. 4 6 public class LoopFib 8 public static void main(String[] args) 9 10 Scanner in - new Scanner(System.in) System.out.print("Enter n: "): int n = in.nextInt(); 12 14 15 for (int i -1; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

Identify three types of physicians and their roles in health care.

Answered: 1 week ago

Question

Compare the types of managed care organizations (MCOs).

Answered: 1 week ago