Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Write a Java program that calculates and displays the Fibonacci series, define

ID: 667164 • Letter: 1

Question

 1 Write a Java program that calculates and displays the Fibonacci  series, 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 {          // constructor         public 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 a. Write a new method for the Greeter class,           public void swapNames(Greeter other) {...}  that swaps the names of this greeter and another instance.   b. write a new method for the Greeter class:         public Greeter createQualifiedGreeter(String qualifier) { ..... } that returns a new Greeter object with its name being the qualifier string followed by " " and the executing greeter's name (i.e. this.name). For example:         Greeter g = new Greeter("world");         Greeter g2 = g.createQualifiedGreeter("beautiful");  g2.name will be the string "beautiful world"  c. Write a GreeterTester class that show how the swapNames() and the createQualifiedGreeter()  are used.   Write javadoc comments. Include both java files in your solution document.     

Explanation / Answer

public class Fibonacci {


SuppressWarnings("resource")
public static void main(String args[]) {

//input to print Fibonacci series upto how many numbers
System.out.print("Enter number upto which Fibonacci series to print: ");
int number = new Scanner(System.in).nextInt();

System.out.println(" Fibonacci series upto " + number +" numbers : ");
//printing Fibonacci series upto number
for(int i=1; i<=number; i++){
System.out.print(fibonacciRecusion(i) +" ");
}
}

// Java program for fibonacci number using recursion.
public static int fibonacciRecusion(int number){
if(number == 1 || number == 2){
return 1;
}

return fibonacciRecusion(number-1) + fibonacciRecusion(number -2); // recursion
}

// Java program for Fibonacci number using Loop.
public static int fibonacciLoop(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number
fibo1 = fibo2;
fibo2 = fibonacci;

}
return fibonacci; //fib number
}   
}

a)

In java the greeter is used either Constructor or method

In the Constructor
r
public Greeter(java.lang.String aName)
Constructs a Greeter object that can greet a person or entity.
Parameters:
aName - the name of the person or entity who should be addressed in the greetings.

In the method

sayHello

public java.lang.String sayHello()
Greet with a "hello" message.
Returns:
a message containing "hello" and the name of the greeted person or entity.

class Greeter {
private String name;

public void swap( Greeter other ){
the test is:

Greeter greetsmy = new Greeter( "my self" );
Greeter greetshai = new Greeter( "hai fine" );
System.out.println( greetmy.sayHello() );
System.out.println( greetshai.sayHello() );
greetsJohn.swapNames( greetshai );
System.out.println( greetsmy.sayHello() );
System.out.println( greetshai.sayHello() );

public class Greeter {

public Greeter(String bName){
name1 = bName;
}


public String sayHello(){
return "Hello, " + name1 + "!";
}

private static String name1;

public static void swapNames(Greeter other){

String kumar = name1;
name1 = other.getName();
other.setName(kumar);
}

private void setName(String aux) {
Greeter.name1 = kumar;
}

private String getName() {
return name1;
}

}

here we can objeserve two methods we are taken.

Greeter greetshai = new Greeter("hai fine");

Greeter greetshello = new Greeter("hello hai");

greetshai.swapNames(greetshello);

assertEquals("Name in greetshai is replaced with hello","hello hai",greetshai.sayHello());