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.3
Figs. 18.4

Figs. 18.5
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
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
Get step-by-step solutions from verified subject matter experts
