Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programmig: Needed the most are: Recursion, Anonymous classes, and Lambda expressions. Part I: Recursion 1. If you know that, in mathematics, the Fibonacci numbers,

Java Programmig: Needed the most are: Recursion, Anonymous classes, and Lambda expressions. Part I: Recursion 1. If you know that, in mathematics, the Fibonacci numbers, commonly denoted F, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F0 =0, F1=1 and Fn = Fn-1 + Fn-2

The beginning of the sequence is thus: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... a. Write a recursive function (method) to find the nth number in Fibonacci sequence. b. Write an iterative function (method) to find the nth number in Fibonacci sequence. c. Report the time elapsed by both functions in milliseconds*. The time elapsed by a function is the difference of the time reported after calling the function to the time reported before calling the function.

image text in transcribed

*To see the significant difference, try big numbers such as: 40.

Part II: Lambda Expressions 2. Suppose that a functional interface ArrayProcessor is defined as:

image text in transcribed

In the main method of a class that you create later, we defined a function that returns the length of an array of type double and used it as:

image text in transcribed Now, using the same concept, lambda, you write and use the following functions: a. A function to find the maximum value in an array. b. A function to find the minimum value in an array. c. A function to find the sum of the values in an array. d. A function to find the average of the values in an array.

long start = System.currentTimeMillis(); // your function call long finish = System.currentTimeMillis(); long timeElapsed = finish - start; System.out.println(time Elapsed + " ms"); Fibonacci #40 using recursion function: 102334155 256 ms Fibonacci #40 using Iterative function: 102334155 ms public interface ArrayProcessor { double apply( double[] array ); double[] firstList = { 6, 2.6, 3, 5, 7, 12, 7, 2.5, 10.3, 1.7 }; Array Processor numberOfElements = (array) -> { return array.length; }; System.out.println(numberOfElements.apply(firstList)); //prints 10

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions