Given:
You want to calculate the average of the Player’s score.
Which statement inserted on line 1 will accomplish this?
Given:
You want to calculate the average of the Player’s score.
Which statement inserted on line 1 will accomplish this?
To calculate the average score of the players, you need to extract the scores from each Player object, convert them into an IntStream, and then compute the average. Option B uses the mapToInt method to achieve this, mapping the Player objects to their integer scores. The average method calculates the average of these integer values, and orElse(0.0) provides a default value of 0.0 if no values are present. Therefore, using players.stream().mapToInt(a -> a.score).average().orElse(0.0); is the correct approach.
The correct statement to insert on line 1 to calculate the average of the Player's score is option B: `players.stream().mapToInt(a -> a.score).average().orElse(0.0);`. This statement uses the `mapToInt` method to convert the stream of `Player` objects into an `IntStream` of their scores, then calculates the average using the `average` method, and finally returns the result or 0.0 if no result is present using the `orElse` method.
answer: B A. Failed there is no such method as average() in Stream B. correct IntStream OptionalDouble average() public double orElse(double other) If a value is present, returns the value, otherwise returns other. so it returns double C. Failed Failed this expression returns OptionalDouble so double average = players.stream().mapToDouble(a -> a.score).average(); returns MyClass.java:17: error: incompatible types: OptionalDouble cannot be converted to double D. Failed there is no such method as average() in Stream
B => to contact orElse return a double. Lonely average return a optionalDouble OTHHERS stream not contain average, InsStream or InDouble yes but with orElse clause return a primitive value
tested.
double average = players.stream().mapToInt(a -> a.score).average().orElse(0.0);