============================================================ IT6003 - Advanced Java Programming Homework 2: Lambda Expressions & Stream API State University of Zanzibar (SUZA) ============================================================ Total Marks: 40 Due: As announced by instructor ============================================================ INSTRUCTIONS: - Submit .java files in a ZIP named RegNo_HW2.zip - Use lambda expressions and streams wherever possible - Avoid traditional loops - use functional style ============================================================ Problem 1: Functional Calculator [10 marks] ---------------------------------------------- a) Define a functional interface MathOperation: double apply(double a, double b); [1 mark] b) Create a Map storing operations: "add", "subtract", "multiply", "divide", "power", "modulus", "max", "min" Use lambda expressions for each. [2 marks] c) Create a method: double calculate(String expr) Parse expressions like "add 5 3" or "power 2 10". Look up operation in the map and apply it. [3 marks] d) Create a pipeline method that chains operations: double pipeline(double initial, List> operations) Test: start with 10, multiply by 2, add 5, divide by 5 = 5.0 [2 marks] e) Create a method using BiFunction and andThen(): First compute two numbers' sum, then compute the square of the sum. Use compose() to reverse the order. [2 marks] Problem 2: Employee Data Processing [15 marks] -------------------------------------------------- Create an Employee class with: id, name, department, salary, yearsOfService, city. Create a list of 25+ employees across 5 departments and 4 cities. Using ONLY Stream API (no loops), implement: a) Basic queries: [3 marks] - Find all employees earning more than 50000 - Find the employee with the highest salary - Find all unique departments - Count employees per city b) Grouping and Partitioning: [4 marks] - Group employees by department: Map> - Average salary per department: Map - Partition into senior (>5 years) and junior: Map> - Group by city, then by department (nested grouping) c) Aggregation: [4 marks] - Total salary expenditure using reduce() - Department with the highest average salary - Top 3 highest paid employees per department - Salary statistics (min, max, avg, sum, count) using summarizingDouble d) Transformation: [4 marks] - Create a salary report string: "Name - Dept - $Salary\n" using Collectors.joining - Create Map of name -> "dept (city)" using Collectors.toMap - Create sorted list of unique department names - Create a formatted ranking: "1. Name ($salary)" for top 10 earners Problem 3: Text Analysis with Streams [10 marks] ---------------------------------------------------- Write a TextAnalyzer class that reads a text file and performs analysis using ONLY streams (no traditional loops). a) Read file into a stream of lines using Files.lines(). [1 mark] b) Implement these analysis methods: [9 marks] - int totalWords(): count all words across all lines - int uniqueWords(): count unique words (case-insensitive) - Map wordFrequency(): frequency of each word - List topNWords(int n): n most frequent words - double averageWordLength(): average length of all words - String longestWord(): the longest word in the text - Map letterFrequency(): frequency of each letter (a-z) - List wordsOfLength(int len): all words of exactly len characters - Map> groupByLength(): words grouped by length Create a sample text file with 200+ words for testing. Print a formatted analysis report. Problem 4: Custom Collector [5 marks] ----------------------------------------- a) Create a custom Collector that collects stream elements into a comma-separated String with "and" before the last element. Example: ["Ali", "Fatma", "Hassan"] -> "Ali, Fatma, and Hassan" Use Collector.of() with supplier, accumulator, combiner, finisher. [3 marks] b) Create a custom Collector that collects numbers into a statistics object with: count, sum, min, max, average, median. [2 marks] ============================================================ Viva Questions (be prepared): 1. What is a functional interface? Can it have default methods? 2. Explain the difference between map() and flatMap() in streams. 3. When would you use parallel streams? What are the risks? 4. What is the difference between intermediate and terminal operations? 5. Can you reuse a stream after a terminal operation? ============================================================