Question: Each of the counter-controlled for loops we used in this chapters examples can be implemented using IntStreams rangeClosed method to produce a range of values,

Each of the counter-controlled for loops we used in this chapter’s examples can be implemented using IntStream’s rangeClosed method to produce a range of values, then using IntStream’s forEach method to specify a lambda to execute for each value. The lambda argument to forEach could, for example, call method factorial (Figs. 18.3 –18.4) or method fibonacci (Fig. 18.5 ) and display the result.

Figs. 18.31 // Fig. 18.3: Factorial Calculator.java // Recursive factorial method. 2 3 4 public class Factorial

Figs. 18.4

I // Fig. 18.4: Factorial Calculator.java 2 // Recursive factorial method. 3 import java.math.BigInteger; 4 5

Figs. 18.5I // Fig. 18.5: FibonacciCalculator.java // Recursive fibonacci method. import java.math.BigInteger; 23456780

1 // Fig. 18.3: Factorial Calculator.java // Recursive factorial method. 2 3 4 public class Factorial Calculator { 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 } 0! = 1 1!= 1 2!= 2 3! = 6 // recursive method factorial (assumes its parameter is >= 0) public static long factorial (long number) { } 20! 21! = } 4! = 24 5! = 120 if (number

Step by Step Solution

3.50 Rating (150 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Based on the description and the images you provided you are interested in how to use Javas IntStream with the rangeClosed and forEach methods to repl... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!