I am working on a program that I am having trouble with it. I have been working with this program for a day and did not work. The output came only letters, @ symbol and numbers. Here is the description of the program.
Generate a Fibonacci sequence. Each number in the Fibonacci series is the sum of the two preceding numbers in the sequence. The first two numbers in the sequence are both 1. The third number is 2, the fourth number is 3, the fifth number is 5, and the sixth number is 8. The program should be able to return a specified amount in the fibo sequence. If a number is specified that is out of range, a -1 should be returned.
I have done the code and the runner code. My output will be similar to this.
1
1
2
3
5
8
89
987
10946
1346269
165580141
1836311903
1
1
1
-1
Here is my code
public class Fibonacci
{
int[] fibArray;
public Fibonacci()
{
fibArray = new int[50];
this.setFibo();
}
public void setFibo()
{
fibArray[0]=1;
fibArray[1]=1;
for(int idx = 2; idx<fibArray.length; idx++){
fibArray[idx] = fibArray[idx-1] + fibArray[idx-2];
}
}
public int[] getFibo()
{
return fibArray;
}
public String toString()
{
return this.getFibo() + "\n";
}
}
Here is my runner code.
public class FibonacciRunner
{
public static void main(String[] args) {
int[] fibArray = {1,2,3,4,5,6,11,16,21,31,41,46,1,1,2,1,2,11};
Fibonacci fibo = new Fibonacci();
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
System.out.println(fibo.getFibo());
}
}