Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will start by making two simple classes, Employee.java and Department.java. Just copy them in and save them: Employee.java package lab10; public class Employee {

We will start by making two simple classes, Employee.java and Department.java. Just copy them in and save them:

Employee.java

package lab10; public class Employee { private String name; private int age; public Employee(String n, int a){ name = n; age = a; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString(){ return name + "(" + age + ")"; } }

Department.java

package lab10; import java.util.ArrayList; import java.util.List; public class Department { private String departmentName; private List employees; public Department(String n){ departmentName = n; employees = new ArrayList<>(); } public void addEmployee(Employee e){ employees.add(e); } public String getDepartmentName(){ return departmentName; } public List getEmployees(){ return employees; } public String toString(){ return employees.toString(); } }

Add the following block of code to the top of a new file Bonus.java

private static List departments = new ArrayList<>(); static{ Employee john = new Employee("John Doe", 30); Employee jane = new Employee("Jane Deer", 25); Employee jack = new Employee("Jack Hill", 40); Employee snow = new Employee("Snow White", 22); Employee jared = new Employee("Jared Miller", 60); Employee jeff = new Employee("Jeff Jackson", 27); Employee gerald = new Employee("Gerald House", 15); Employee mary = new Employee("Mary Lou", 23); Department hr = new Department("Human Resources"); hr.addEmployee(jane); hr.addEmployee(jack); hr.addEmployee(snow); Department accounting = new Department("Accounting"); accounting.addEmployee(john); accounting.addEmployee(jared); accounting.addEmployee(jeff); accounting.addEmployee(gerald); accounting.addEmployee(mary); departments.add(hr); departments.add(accounting); }

In Bonus.java, implement the method public static void printYoungestEmployee() which prints out the youngest Employee in departments.

Hints: You will need flatMap() to flatten the Stream of Departments into a Stream of Employees, and you will then need to use a clever use of reduce() and a ternary operator (look it up if you are unfamilar with it) to get it to return the youngest employee.

After you have called reduce on the stream, call ifPresent() on the reduce call which will allow you to use a lambda expression to print the youngest Employee you found. In this case, reduce() returns an Optional of the found Employee, which is why we are adding the ifPresent() check to get a hold of the value and print it. In order to match the output below, you will also need the peek() method to print out each department. Figure out where the peek needs to be added to the stream operation chaining.

Calling printYoungestEmployee() in a main method should print out the following output:

Human Resources: [Jane Deer(25), Jack Hill(40), Snow White(22)] Accounting: [John Doe(30), Jared Miller(60), Jeff Jackson(27), Gerald House(15), Mary Lou(23)] youngest: Gerald House(15)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago