Question

In: Computer Science

Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a...

Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array.

Solutions

Expert Solution

// formula for fibonacci Fn = F(n-1) + F(n-2) n>=2 F[0]=0.0 F[1]=1.0
class Fib{
   //function for calculating 15 fibonacci numbers
   public double[] getFib(){
       int num1=0,num2=1,num3,i,size=15;
       double[] fibArray = new double[15];   //double type array for stroing fibonacci values
       fibArray[0]=num1;   //first fibonacci number
       fibArray[1]=num2;   //second fibonacci number
       //loop for calculating third fibonacci number to 15th fibonacci numbers
       for(i=2;i<size;i++){
           num3=num1+num2;   //next fibonacci number calculated by sum of previous two fibonacci values
           fibArray[i]=num3;   //storing next fibonacci value in to array
           num1=num2;
           num2=num3;
       }
       return fibArray;   //returning array
   }
   public static void main(String args[]){
       Fib obj =new Fib();   //object creation
       double[] res = obj.getFib();   //stroing function return array
       //displaying first 15 fibonacci numbers
       for(int i=0;i<15;i++){
           System.out.println((i+1)+" fibnocci number fib["+i+"] = "+res[i]);
       }
   }
}


Related Solutions

Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
Describe how Java creates a single dimension array and a double dimension array. Describe and explain...
Describe how Java creates a single dimension array and a double dimension array. Describe and explain how Java processes the contents of a single dimension array. Describe and explain how Java saves values to a single dimension array, and to a double dimension array.
Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Create a complete java program called Week_Report. The program must include two array structures, a string...
Create a complete java program called Week_Report. The program must include two array structures, a string array called DaysOfWeek and a double array called Temp_Values. Store in the DaysOfWeek array the following values (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday). Store in the Temp_Values array the following (23.5, 34.0, 20.9, 45.7, 29.3, 34.5, 32.5). Using a for loop structure output the values for the two arrays. Day of the Week Temperature Values Monday 23.5 Tuesday 34.0 Wednesday 20.9 Thursday 45.7...
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
The following program creates a linked list which contains 5 links. Add a method called findMax()...
The following program creates a linked list which contains 5 links. Add a method called findMax() to the LinkedList class. The findMax() method must be of type integer, it must search the list and return the maximum value of the number data field. Add the required code to the main() method to call the findMax() method. public class Link { private int number; private Link next; public Link(int x) { number = x; } public void displayLink() { System.out.println("The number...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT