Question: Modify the StockAverage2 program in Figure 12.13 by inserting into the streams method-call chain a .peek method call that prints the previously entered values. Use

  1. Modify the StockAverage2 program in Figure 12.13 by inserting into the stream’s method-call chain a .peek method call that prints the previously entered values. Use a lambda expression for peek’s argument.

  2. Figure 12.13

/***** StockAverage2.java *Dean & Dean * * This program stores user-entered stock values in an ArrayList. *

/***** * StockAverage2.java *Dean & Dean * * This program stores user-entered stock values in an ArrayList. * Then converts to stream to compute average value and avoid error. import java.util.Scanner; import java.util.ArrayList; public class StockAverage2 ( public static void main(String[] args) ( Scanner stdIn = new Scanner (System.in); ArrayList stocks = new ArrayList (); double stock; System.out.print("Enter a stock value (-1 to quit):"); stock = stdin.nextDouble(); while (stock >= 0) { stocks.add(stock); System.out.print("Enter a stock value (-1 to quit): "); stock = stdIn.nextDouble(); } // end while System.out.printf(" Average stock value // a stock value. $%.2f ", stocks.stream() // sourcing op. .mapToDouble (Double::double Value) // intermediate op. .average ( ) // terminal op. .or Else (0.0)); // avoids error } // end main ] // end class StockAverage 2 = 1

Step by Step Solution

3.44 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The peek method in Java streams is an intermediate operation that can be used to perform an action o... 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 Introduction To Programming With Java A Problem Solving Approach Questions!