Question

Solve the following three problems using "Java with Akka Actors". Read the submission instructions carefully. Problem...

Solve the following three problems using "Java with Akka Actors". Read the submission instructions carefully.

Problem 1 (25 Points): Fibonacci Numbers 1. Develop an actor program for computing Fibonacci numbers concurrently. Particularly, create an actor which receives a request for a particular Fibonacci number from a client actor. If it is one of the base cases, the actor replies with a message containing the answer; otherwise, it creates the two sub-problems you would normally create in a recursive implementation. It then creates two new (Fibonacci) actors, and sends one subproblem to each. Once it has received replies from these actors, it adds them to produce its result, and sends it back to the computation's client.

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

Actor:

public class Actor extends Thread
{
//number will be the input whose fibonacci we need to create
private int number;
//Result will store the final answer
public int result;

public Actor(int number)
{
this.number = number;
}

public int getResult()
{
return this.result;
}
public void run()
{
//Base case is if number is 2 or lesst than 2 it sets result to 1
//fibonacci series is 0 1 1 2 3
//so second fibonaci is 1
if (number <= 2)
result = 1;
else
{
//Since we are going to use threads we need to use exceptions
try
{
//Create the first actor and assign him one of the number to calculate
Actor first = new Actor(number - 1);
//Create the first second and assign him one of the number to calculate
Actor second = new Actor(number - 2);
//calls the instance of first actor
first.start();
//call the instance of second actor
second.start();

first.join();

second.join();
//result would be the sum of answers given by both acotrs
result = first.result + second.result;
}
catch (InterruptedException ex)
{
System.out.println("Exception occured");
}
}
}
}

Client class:

import java.util.*;

public class Main {
public static void main(String[] args) throws Exception
{
try
{
//Scanner for taking the input
Scanner sc = new Scanner(System.in);
//Taking input from the user
int number = sc.nextInt();
//CAlling the actor class with the input
Actor fibonaci = new Actor(number);
//Starting the main thread
fibonaci.start();
//join all the threads to get the answer
fibonaci.join();
//printing the answer
System.out.println(fibonaci.getResult());
}
catch(Exception e)
{
System.out.println("Exception occured");
}
}
}

Input/Output

input - 8

output - 21

fibonacci series - 1 1 2 3 5 8 13 21

Add a comment
Know the answer?
Add Answer to:
Solve the following three problems using "Java with Akka Actors". Read the submission instructions carefully. Problem...
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
  • Using scala concurrency programming with akka to solve the fallowing: Develop an actor program for computing...

    Using scala concurrency programming with akka to solve the fallowing: Develop an actor program for computing Fibonacci numbers concurrently. Particularly, create an actor which receives a request for a particular Fibonacci number from a client actor. If it is one of the base cases, the actor replies with a message containing the answer; otherwise, it creates the two sub-problems you would normally create in a recursive implementation. It then creates two new (Fibonacci) actors, and sends one subproblem to each....

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

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