Question

Please I need java code for this question with output: 4.19) The Fibonacci sequence is the...

Please I need java code for this question with output:

4.19)

The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, ....
Formally, it can be expressed as:

f ib0 = 0
f ib1 = 1
f ibn = f ibn−1 + f ibn−2
Write a multithreaded program that generates the Fibonacci sequence
using either the Java, Pthreads, or Win32 thread library. This program
should work as follows: The user will enter on the command line
the number of Fibonacci numbers that the program is to generate.
The program will then create a separate thread that will generate the
Fibonacci numbers, placing the sequence in data that can be shared by
the threads (an array is probably the most convenient data structure).
When the thread finishes execution, the parent thread will output the
sequence generated by the child thread. Because the parent thread cannot
begin outputting the Fibonacci sequence until the child thread finishes,
this will require having the parent thread wait for the child thread to
finish,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please refer to screenshots

code

1 import java.util.*; 2 // java code by extending 3 // the Thread class 4- class Multithreading extends Thread{ private int [24 // Main Class 25 - public class Main{ 26 - public static void main(String[] args) { Scanner sc = new Scanner(System.in); S

Output

Enter value of n, number of fibonacci numbers to be generated: 12 0 i 2 3 5 8 13 21 34 55 89 144 ...Program finished with exi

Code to copy

import java.util.*;
// java code by extending
// the Thread class
class Multithreading extends Thread{
  
   private int []array;
   private int n;
  
   // constructor to initialize array
   public Multithreading(int n, int []arr){
        this.array = arr;
        this.n = n;
   }
  
   public void run() {
        array[0] = 0; // initial values
        array[1] = 1;
        for(int i = 2; i <= n; i++){
            array[i] = array[i - 1] + array[i - 2]; // generating fibonacci
        }
   }
}

// Main Class
public class Main{
   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter value of n, number of fibonacci numbers to be generated: ");
       int n = sc.nextInt(); // Number of threads
       int []arr = new int[n + 1];
       Multithreading object = new Multithreading(n, arr); // creating thread
       object.start(); // starting thread
       try {
            object.join(); // waiting for child thread to finish
        }
        catch (Exception e) {
            System.out.println(e);
        }
       // displaying data
       for(int i = 0; i <= n; i++){
            System.out.print(String.valueOf(arr[i]) + ' ');
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Please I need java code for this question with output: 4.19) The Fibonacci sequence is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Check the words in bold. The Fibonacci sequence is the series of numbers 0,1,1, 2, 3,...

    Check the words in bold. The Fibonacci sequence is the series of numbers 0,1,1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded program that generates the Fibonacci sequence using the Win32 thread library(not pthreads because it does not work on windows). This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is...

  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...

  • fibonacci sequence in C

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 i. Write a C program using the fork() system call that that generates the Fibonacci sequence by the child process. ii. If the user input as 5 then the child process has to output up to the 5 that is output must be 0,1,1,2,3,5. iii. Parent has to print the child process id and child has to print the parent process id. iv. Parent has to finish only after the child terminates. v. Is there any process synchronization problem here? Justify your answer. vi. Modify the above program to create a zombie process. How do y ou identify the zombie process? vii. Modify the above program to create an orphan process. viii. Compare and contrast the process and threads.

  • pls give answers for this

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 i. Write a C program using the fork() system call that that generates the Fibonacci sequence by the child process. ii. If the user input as 5 then the child process has to output up to the 5 that is output must be 0,1,1,2,3,5. iii. Parent has to print the child process id and child has to print the parent process id. iv. Parent has to finish only after the child terminates. v. Is there any process synchronization problem here? Justify your answer. vi. Modify the above program to create a zombie process. How do y ou identify the zombie process? vii. Modify the above program to create an orphan process. viii. Compare and contrast the process and threads.

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • A Fibonacci sequence is a series of numbers where the next number in the series is...

    A Fibonacci sequence is a series of numbers where the next number in the series is the sum of the previous two numbers. The sequence starts with the numbers 0 and 1. Here are the first ten numbers of the Fibonacci sequence:             0 1 1 2 3 5 8 13 21 34 Write a Java program to print out the first fifteen numbers of the Fibonacci sequence using a loop. You will need three variables previous, current, and next...

  • c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8,...

    c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21 Note that the first Fibonacci number is 1, F(1) = 1 The second Fibonacci number is 1, i.e. F(2) = 1 Other Fibonacci numbers in the sequence is the sum of two previous Fibonacci numbers. For example F(3) = F(2) + F(1). In general F(n) = F(n-1) + F(n-2) Write a program to do the following tasks. User entries are shown in...

  • Please write a MIPS program to print the first thirty numbers in the Fibonacci sequence in which each number is the sum of the two preceding ones, starting from 0 and 1. Note that each number in the Fibonacci sequence should be calculated using MIPS instr

    Please write a MIPS program to print the first thirty numbers in the Fibonacci sequence in which each number is the sum of the two preceding ones, starting from 0 and 1. Note that each number in the Fibonacci sequence should be calculated using MIPS instructions. After that please run your MIPS program using SPIM software to display the result in the output (console) window. Save your execution result in a log file of SPIM.What to submit:  1. Your MIPS...

  • Please help with Java test questions, I will post the rest of the question in separate...

    Please help with Java test questions, I will post the rest of the question in separate post. Question 1 Consider the following program: public class CountDownApp3 {     public static void main(String[] args)     {         Thread t1 = new CountDown();         Thread t2 = new CountDown();         t1.start();         t2.start();     } } Assuming that the CountDown class extends the Thread class, how many threads are used to run this program? a. 3 b. 2 c. 4 d. 1...

  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT