SlideShare a Scribd company logo
1 of 6
Download to read offline
1Write a Java program that calculates and displays the Fibonacciseries, defined by the recursive
formula
F(n) = F(n-1) + F(n-2).
F(0) and F(1) are given on the command line.Define and use a class Fib with the following
structure:
public class Fib {
// constructorpublic Fib(int f0, int f1){.....}
// computes F(n) using an ***iterative*** algorithm, where F(n) = F(n-1) +
F(n-2) is the recursive definition.
// use instance variables that store F(0) and F(1).
// check parameter and throw exception if n < 0. Don't worry about
arithmetic overflow.
public int f(int n) {....}
// computes F(n) using the ***recursive*** algorithm, where F(n) = F(n-1)
+ F(n-2) is the recursive definition.
// use instance variables that store F(0) and F(1).// check parameter and throw exception if n < 0.
Don't worry about
arithmetic overflow.
public int fRec(int n) {....}
public static void main(String[] args){
// get numbers F(0) and F(1) from args[0] and args[1].// use either the Scanner class or
Integer.parseInt(args[...])// you must handle possible exceptions !....
// get n from args[2]:....
// create a Fib object with params F(0) and F(1)....
// calculate F(0), ..., F(n) and display them with
System.out.println(...) using
// the iterative methode f(i)....
// calculate F(0), ..., F(n) and display them with
System.out.println(...) using
// the recursive methode fRec(i)....
}
// instance variables store F(0) and F(1):....
};
Write javadoc comments for the Fib class.
Solution
// Fib.java
/**
* This class provide iterative as well as recursive mode of
* finding nth value of fibonaci series
*
* To run this program please run like java Fib 1 1 10
*
* This means f(0) = 1, f(1) = 1 and you want to compute series for 10 numbers
*
* @author Anonymous
*
*/
public class Fib {
public Fib(int first, int second)
{
f0 = first;
f1 = second;
}
static int f0;
static int f1;
public static int f(int n) throws Exception
{
if (n < 0)
{
throw new Exception ("invalid value: n should be greater than equal to 0");
}
if (n == 0) return f0;
if (n == 1) return f1;
int prev = f1;
int prevprev = f0;
int result = 0;
for (int i = 2 ; i <= n; i++)
{
result = prev + prevprev;
prevprev = prev;
prev = result;
}
return result;
}
public static int fRec(int n) throws Exception
{
if (n < 0)
{
throw new Exception ("invalid value: n should be greater than equal to 0");
}
if ( n == 0)
{
return f0;
}
if (n == 1)
{
return f1;
}
return fRec(n-1) + fRec(n-2);
}
public static void main(String[] args) throws Exception{
int first = 0;
int second = 0;
int n = 0;
try
{
first = Integer.parseInt(args[0]);
second = Integer.parseInt(args[1]);
n = Integer.parseInt(args[2]);
}
catch (Exception e)
{
System.out.println("Please enter integer number only: " + e);
throw e;
}
Fib fib = new Fib(first, second);`
System.out.println("Printing using iterative method");
for (int i = 0; i <= n; i++)
{
System.out.println(f(i));
}
System.out.println("Printing using recursive method");
for (int i = 0; i <= n; i++)
{
System.out.println(fRec(i));
}
}
};
/*
Sample run
java 1 1 30
Printing using iterative method
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
Printing using recursive method
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
*/

More Related Content

More from shahidqamar17

At one time, the country of Aquilonia had no banks, but had curre.pdf
At one time, the country of Aquilonia had no banks, but had curre.pdfAt one time, the country of Aquilonia had no banks, but had curre.pdf
At one time, the country of Aquilonia had no banks, but had curre.pdfshahidqamar17
 
A certain element has a half life of 4.5 billion years. a. You find .pdf
A certain element has a half life of 4.5 billion years. a. You find .pdfA certain element has a half life of 4.5 billion years. a. You find .pdf
A certain element has a half life of 4.5 billion years. a. You find .pdfshahidqamar17
 
A linear system is always time-invariant.SolutionNo its false. .pdf
A linear system is always time-invariant.SolutionNo its false. .pdfA linear system is always time-invariant.SolutionNo its false. .pdf
A linear system is always time-invariant.SolutionNo its false. .pdfshahidqamar17
 
Workforce Management also called Human Resource Management manages o.pdf
Workforce Management also called Human Resource Management manages o.pdfWorkforce Management also called Human Resource Management manages o.pdf
Workforce Management also called Human Resource Management manages o.pdfshahidqamar17
 
Where has healthcare information management been historically How h.pdf
Where has healthcare information management been historically How h.pdfWhere has healthcare information management been historically How h.pdf
Where has healthcare information management been historically How h.pdfshahidqamar17
 
What were the successes and failures of the Progressive Movement, an.pdf
What were the successes and failures of the Progressive Movement, an.pdfWhat were the successes and failures of the Progressive Movement, an.pdf
What were the successes and failures of the Progressive Movement, an.pdfshahidqamar17
 
What is the difference between private sector, nonprofit sector, and.pdf
What is the difference between private sector, nonprofit sector, and.pdfWhat is the difference between private sector, nonprofit sector, and.pdf
What is the difference between private sector, nonprofit sector, and.pdfshahidqamar17
 
What are Cubas entrepreneurial opportunities and the status of .pdf
What are Cubas entrepreneurial opportunities and the status of .pdfWhat are Cubas entrepreneurial opportunities and the status of .pdf
What are Cubas entrepreneurial opportunities and the status of .pdfshahidqamar17
 
The LabVIEW G programming is considered to be a data flow programmin.pdf
The LabVIEW G programming is considered to be a data flow programmin.pdfThe LabVIEW G programming is considered to be a data flow programmin.pdf
The LabVIEW G programming is considered to be a data flow programmin.pdfshahidqamar17
 
what is a bottom heap up construction with an example pleaseSol.pdf
what is a bottom heap up construction with an example pleaseSol.pdfwhat is a bottom heap up construction with an example pleaseSol.pdf
what is a bottom heap up construction with an example pleaseSol.pdfshahidqamar17
 
What are the types of differences that exist between IFRS and U.S. G.pdf
What are the types of differences that exist between IFRS and U.S. G.pdfWhat are the types of differences that exist between IFRS and U.S. G.pdf
What are the types of differences that exist between IFRS and U.S. G.pdfshahidqamar17
 
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdfROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdfshahidqamar17
 
question.(player, entity ,field and base.java codes are given)Stop.pdf
question.(player, entity ,field and base.java codes are given)Stop.pdfquestion.(player, entity ,field and base.java codes are given)Stop.pdf
question.(player, entity ,field and base.java codes are given)Stop.pdfshahidqamar17
 
Question 25 A Multilateral agreement in which countries signed to ban.pdf
Question 25 A Multilateral agreement in which countries signed to ban.pdfQuestion 25 A Multilateral agreement in which countries signed to ban.pdf
Question 25 A Multilateral agreement in which countries signed to ban.pdfshahidqamar17
 
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdfProve. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdfshahidqamar17
 
need an example of a System Anaysis and design project must have 3 .pdf
need an example of a System Anaysis and design project must have 3 .pdfneed an example of a System Anaysis and design project must have 3 .pdf
need an example of a System Anaysis and design project must have 3 .pdfshahidqamar17
 
Money laundering has become a mechanism for terrorist financing acti.pdf
Money laundering has become a mechanism for terrorist financing acti.pdfMoney laundering has become a mechanism for terrorist financing acti.pdf
Money laundering has become a mechanism for terrorist financing acti.pdfshahidqamar17
 
Let x be any set and let lambda SolutionProof. Obviously .pdf
Let x be any set and let lambda SolutionProof. Obviously .pdfLet x be any set and let lambda SolutionProof. Obviously .pdf
Let x be any set and let lambda SolutionProof. Obviously .pdfshahidqamar17
 
Java public cts Node t next ni next SolutionYou hav.pdf
Java public cts Node t next ni next SolutionYou hav.pdfJava public cts Node t next ni next SolutionYou hav.pdf
Java public cts Node t next ni next SolutionYou hav.pdfshahidqamar17
 
1. What is the purpose of level numbers in COCOL records2.What ar.pdf
1. What is the purpose of level numbers in COCOL records2.What ar.pdf1. What is the purpose of level numbers in COCOL records2.What ar.pdf
1. What is the purpose of level numbers in COCOL records2.What ar.pdfshahidqamar17
 

More from shahidqamar17 (20)

At one time, the country of Aquilonia had no banks, but had curre.pdf
At one time, the country of Aquilonia had no banks, but had curre.pdfAt one time, the country of Aquilonia had no banks, but had curre.pdf
At one time, the country of Aquilonia had no banks, but had curre.pdf
 
A certain element has a half life of 4.5 billion years. a. You find .pdf
A certain element has a half life of 4.5 billion years. a. You find .pdfA certain element has a half life of 4.5 billion years. a. You find .pdf
A certain element has a half life of 4.5 billion years. a. You find .pdf
 
A linear system is always time-invariant.SolutionNo its false. .pdf
A linear system is always time-invariant.SolutionNo its false. .pdfA linear system is always time-invariant.SolutionNo its false. .pdf
A linear system is always time-invariant.SolutionNo its false. .pdf
 
Workforce Management also called Human Resource Management manages o.pdf
Workforce Management also called Human Resource Management manages o.pdfWorkforce Management also called Human Resource Management manages o.pdf
Workforce Management also called Human Resource Management manages o.pdf
 
Where has healthcare information management been historically How h.pdf
Where has healthcare information management been historically How h.pdfWhere has healthcare information management been historically How h.pdf
Where has healthcare information management been historically How h.pdf
 
What were the successes and failures of the Progressive Movement, an.pdf
What were the successes and failures of the Progressive Movement, an.pdfWhat were the successes and failures of the Progressive Movement, an.pdf
What were the successes and failures of the Progressive Movement, an.pdf
 
What is the difference between private sector, nonprofit sector, and.pdf
What is the difference between private sector, nonprofit sector, and.pdfWhat is the difference between private sector, nonprofit sector, and.pdf
What is the difference between private sector, nonprofit sector, and.pdf
 
What are Cubas entrepreneurial opportunities and the status of .pdf
What are Cubas entrepreneurial opportunities and the status of .pdfWhat are Cubas entrepreneurial opportunities and the status of .pdf
What are Cubas entrepreneurial opportunities and the status of .pdf
 
The LabVIEW G programming is considered to be a data flow programmin.pdf
The LabVIEW G programming is considered to be a data flow programmin.pdfThe LabVIEW G programming is considered to be a data flow programmin.pdf
The LabVIEW G programming is considered to be a data flow programmin.pdf
 
what is a bottom heap up construction with an example pleaseSol.pdf
what is a bottom heap up construction with an example pleaseSol.pdfwhat is a bottom heap up construction with an example pleaseSol.pdf
what is a bottom heap up construction with an example pleaseSol.pdf
 
What are the types of differences that exist between IFRS and U.S. G.pdf
What are the types of differences that exist between IFRS and U.S. G.pdfWhat are the types of differences that exist between IFRS and U.S. G.pdf
What are the types of differences that exist between IFRS and U.S. G.pdf
 
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdfROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
ROT13 (rotate by 13 places) is a simple letter substitution cipher t.pdf
 
question.(player, entity ,field and base.java codes are given)Stop.pdf
question.(player, entity ,field and base.java codes are given)Stop.pdfquestion.(player, entity ,field and base.java codes are given)Stop.pdf
question.(player, entity ,field and base.java codes are given)Stop.pdf
 
Question 25 A Multilateral agreement in which countries signed to ban.pdf
Question 25 A Multilateral agreement in which countries signed to ban.pdfQuestion 25 A Multilateral agreement in which countries signed to ban.pdf
Question 25 A Multilateral agreement in which countries signed to ban.pdf
 
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdfProve. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
Prove. Let n be a natural number. Any set of n integers {a1, a2, . ..pdf
 
need an example of a System Anaysis and design project must have 3 .pdf
need an example of a System Anaysis and design project must have 3 .pdfneed an example of a System Anaysis and design project must have 3 .pdf
need an example of a System Anaysis and design project must have 3 .pdf
 
Money laundering has become a mechanism for terrorist financing acti.pdf
Money laundering has become a mechanism for terrorist financing acti.pdfMoney laundering has become a mechanism for terrorist financing acti.pdf
Money laundering has become a mechanism for terrorist financing acti.pdf
 
Let x be any set and let lambda SolutionProof. Obviously .pdf
Let x be any set and let lambda SolutionProof. Obviously .pdfLet x be any set and let lambda SolutionProof. Obviously .pdf
Let x be any set and let lambda SolutionProof. Obviously .pdf
 
Java public cts Node t next ni next SolutionYou hav.pdf
Java public cts Node t next ni next SolutionYou hav.pdfJava public cts Node t next ni next SolutionYou hav.pdf
Java public cts Node t next ni next SolutionYou hav.pdf
 
1. What is the purpose of level numbers in COCOL records2.What ar.pdf
1. What is the purpose of level numbers in COCOL records2.What ar.pdf1. What is the purpose of level numbers in COCOL records2.What ar.pdf
1. What is the purpose of level numbers in COCOL records2.What ar.pdf
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

1Write a Java program that calculates and displays the Fibonacciseri.pdf

  • 1. 1Write a Java program that calculates and displays the Fibonacciseries, defined by the recursive formula F(n) = F(n-1) + F(n-2). F(0) and F(1) are given on the command line.Define and use a class Fib with the following structure: public class Fib { // constructorpublic Fib(int f0, int f1){.....} // computes F(n) using an ***iterative*** algorithm, where F(n) = F(n-1) + F(n-2) is the recursive definition. // use instance variables that store F(0) and F(1). // check parameter and throw exception if n < 0. Don't worry about arithmetic overflow. public int f(int n) {....} // computes F(n) using the ***recursive*** algorithm, where F(n) = F(n-1) + F(n-2) is the recursive definition. // use instance variables that store F(0) and F(1).// check parameter and throw exception if n < 0. Don't worry about arithmetic overflow. public int fRec(int n) {....} public static void main(String[] args){ // get numbers F(0) and F(1) from args[0] and args[1].// use either the Scanner class or Integer.parseInt(args[...])// you must handle possible exceptions !.... // get n from args[2]:.... // create a Fib object with params F(0) and F(1).... // calculate F(0), ..., F(n) and display them with System.out.println(...) using // the iterative methode f(i).... // calculate F(0), ..., F(n) and display them with System.out.println(...) using // the recursive methode fRec(i).... } // instance variables store F(0) and F(1):.... }; Write javadoc comments for the Fib class.
  • 2. Solution // Fib.java /** * This class provide iterative as well as recursive mode of * finding nth value of fibonaci series * * To run this program please run like java Fib 1 1 10 * * This means f(0) = 1, f(1) = 1 and you want to compute series for 10 numbers * * @author Anonymous * */ public class Fib { public Fib(int first, int second) { f0 = first; f1 = second; } static int f0; static int f1; public static int f(int n) throws Exception { if (n < 0) { throw new Exception ("invalid value: n should be greater than equal to 0"); } if (n == 0) return f0; if (n == 1) return f1; int prev = f1; int prevprev = f0;
  • 3. int result = 0; for (int i = 2 ; i <= n; i++) { result = prev + prevprev; prevprev = prev; prev = result; } return result; } public static int fRec(int n) throws Exception { if (n < 0) { throw new Exception ("invalid value: n should be greater than equal to 0"); } if ( n == 0) { return f0; } if (n == 1) { return f1; } return fRec(n-1) + fRec(n-2); } public static void main(String[] args) throws Exception{ int first = 0; int second = 0; int n = 0; try { first = Integer.parseInt(args[0]); second = Integer.parseInt(args[1]); n = Integer.parseInt(args[2]);
  • 4. } catch (Exception e) { System.out.println("Please enter integer number only: " + e); throw e; } Fib fib = new Fib(first, second);` System.out.println("Printing using iterative method"); for (int i = 0; i <= n; i++) { System.out.println(f(i)); } System.out.println("Printing using recursive method"); for (int i = 0; i <= n; i++) { System.out.println(fRec(i)); } } }; /* Sample run java 1 1 30 Printing using iterative method 1 1 2 3 5 8 13 21 34 55