Question

In: Computer Science

Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...

Complete java program below.

Complete non-recursive version nthFibonacciWithLoop() method.

Complete recursive version nthFibonacciWithRecursion() method.

public class Fibonacci {

// Fib(N): N N = 0 or N = 1

// Fib(N-1) + Fib(N-2) N > 1

// For example,

// Fib(0) = 0

// Fib(1) = 1

// Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1

// Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1 =

2

// ...

// Fib(10) = (0), 1, 1, 2, 3, , 5, 8, 13, 21, 34, 55, ...

// Return the nth Fibonacci number.

public static int nthFibonacciWithLoop(int nth) {

}

public static int nthFibonacciWithRecursion(int nth) {

}

// Check first 30 Fibonicci numbers.

private static void check() {

int [] check = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,

610, 987, 1597, 2584, 4181, 6765};

boolean correctLoop = true, correctRecursion = true;

for (int i = 0; i < 20; i++) {

if (nthFibonacciWithLoop(i) != check[i])

correctLoop = false;

if (nthFibonacciWithRecursion(i) != check[i])

correctRecursion = false;

}

if (correctLoop && correctRecursion)

System.out.println("\nYou got them right!");

else {

if (!correctLoop)

System.out.println("\nCheck nthFibonacciWithLoop()");

if (!correctRecursion)

System.out.println("\nCheck nthFibonacciWithRecursion()");

}

}

public static void main(String [] args) {

System.out.print("Enter N: ");

int n = new java.util.Scanner(System.in).nextInt();

for (int i = 0; i <= n; i++) {

System.out.print(nthFibonacciWithLoop(i) + " ");

}

System.out.println();

for (int i = 0; i <= n; i++) {

System.out.print(nthFibonacciWithRecursion(i) + " ");

}

check();

}

}

Solutions

Expert Solution

import java.util.*;
public class Main
{
public static int nthFibonacciWithLoop(int nth)
{
int i,a,b,s=0;
a=0;//initializing a to 0
b=1;// and b to 1
if(nth==0)//the first value is 0 so it returns 0 for n=0
return a;
for(i=1;i<nth;i++)//loops for other Numbers
{
s=a+b;//adding previous value to present
a=b;//exchanging values
b=s;
}
return b;//returning the present value of b
}
public static int nthFibonacciWithRecursion(int nth)
{
if(nth<=1)//end condition
{
return nth;//returns n value 0 or 1
}
return nthFibonacciWithRecursion(nth-1)+nthFibonacciWithRecursion(nth-2);//fib(3)=fib(2)+fib(1)
}
private static void check() {
//initializing first 30 fibonacci series values to array
int [] check = {0, 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};
boolean correctLoop = true, correctRecursion = true;//boolean values
for (int i = 0; i < 30; i++) //checking for first 30 values through loop
{
if (nthFibonacciWithLoop(i) != check[i])
correctLoop = false;
if (nthFibonacciWithRecursion(i) != check[i])
correctRecursion = false;
}
if (correctLoop && correctRecursion)//if both of them are true then w got them right
System.out.println("\nYou got them right!");
else//else it will give us which is giving wrong output loop or recursion
{
if (!correctLoop)
System.out.println("\nCheck nthFibonacciWithLoop()");
if (!correctRecursion)
System.out.println("\nCheck nthFibonacciWithRecursion()");
}
}
   public static void main(String[] args) {
   int n,i;
   Scanner s=new Scanner(System.in);
   System.out.print("Enter N: ");
   n=s.nextInt();
   for(i=0;i<n;i++)
   {
   System.out.print(nthFibonacciWithLoop(i) + " ");
   }
   System.out.println();
for ( i = 0; i <= n; i++)
{
System.out.print(nthFibonacciWithRecursion(i) + " ");
}
check();
   }
}


Related Solutions

There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Java Language Add a recursive method to the program shown in the previous section that states...
Java Language Add a recursive method to the program shown in the previous section that states how many nodes does the stack have. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
Java Language Add a recursive method to the program shown in the previous section that allows...
Java Language Add a recursive method to the program shown in the previous section that allows remove the last node from the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
Java Language Add a recursive method to the program shown in the previous section that allows...
Java Language Add a recursive method to the program shown in the previous section that allows insert a value at the end of the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() {...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT