Reference no: EM133137652 , Length: word count:2000
COIT20256 Data Structures and Algorithms - Central Queensland University
Question 1. Assuming that random is a SecureRandom object, explain in detail the stream pipeline:
random.ints(1000000, 1, 3)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.forEach((side, frequency) ->
System.out.printf("%-6d%d%n", side, frequency));
Question 2. Reimplement the following statement using a lambda as the event handler:
1. slider.valueProperty().addListener(
2. new ChangeListener<Number>() {
3. @Override
4. public void changed(ObservableValue<? extends Number> ov,
5. Number oldValue, Number newValue) {
6. System.out.printf("The slider's new value is %s%n", newValue);
7. }
8. }
9. );
Activity One
Implement the program designed as part of Activity One in Lecture. The question is repeated here.
Question 3. Write a java program to generate twenty five random integers between 0 and 100 (inclusive), display the values, display the sorted values, sum and average of the values. Display the list of values greater than 50. Use Lambdas.
Activity Two
Question 4. Write a stream operation to calculate and display the average employee salary by department. (Use the source code in Fig.17.20 from the Example source files used in lecture, available at the Unit Website, Week 8 Folder.
Question 5. What is wrong with the following stream pipeline?
1 list.stream()
2 filter(value -> value % 2 != 0)
3 sum()
Write the correct stream pipeline.
Question 6. Write a java program to:
a) Display the original string values in the array
b) Display the string values converted to uppercase
c) Display the string values in ascending order after filtering the values starting with g or less
d) Display the string values in descending order after filtering the values starting with g or less.
Data: String [] names = {"bob", "Mary" "Andrew", "Tomy", "Michael", "Kathy", "lily", "Frances"}
Question 7. Use the class Invoice provided at the unit website in Week 8 folder. Use the sample data shown below. Perform the following queries on the array of Invoice Objects and display the results:
a) Use lambdas and streams to sort the Invoice objects by Price, then display the results.
b) Use lambdas and streams to map each Invoice to its PartDescription and Qunatity, sort the results by Quantity, and display the results.
c) Use lambdas and streams to map each Invoice to its PartDescription and the value of the Invoice (i.e., Quantity * Price). Order the results by Invoice value.
d) Modify Part (d) to select the Invoice values in the range $200 to $500. (Reference: Textbook Chapter 17,Exercise 17.11)
Attachment:- Data Structures and Algorithms.rar