/
07 - Collections - Exercise

07 - Collections - Exercise

Overview

The aim for this exercise is to replace the Array in our Employee application with a Collection. We will also implement multiple ways of sorting the collection.

Task 1 - Replace Array with a Collection

  1. Reopen your EmployeeManagement project we created (06 - OOP Intro - Exercise 1 ) previously.

  2. In the EmployeeApplication class, locate the main method and replace the Array of three employees with a suitable Collection object. (Hint: maybe one of the List implementation?)

  3. Use the enhanced for loop when iterating through your collection.

  4. In the Manager class, amend the addEmployee method to NOT allow a manager to manage more than 5 employees.

  5. Add the jar dependencies to your project as we did before and unit test your Employee and Manager classes. Which methods need special attention?

Task 2 - Sorting a collection of employees

  1. Create a new class called EmployeeNameComparator, which implements java.util.Comparator interface.

  2. Provide the public int compare(Employee e1, Employee e2) method. You can use String's compareTo method on the employee's name.

  3. Sort the list of employees from the previous task. Use the class method defined on Collections class and provide a new instance of your comparator class.

  4. Print out the content of the list and check that the employees are indeed sorted.

  5. Create a unit test for this functionality.

Task 3 - Time different types of collections

  1. Create a large number of Employee objects with random details. The method Math.random() returns a random double between 0.0 and 1.0, which you can use to scale up to random ages for example:

    int age = (int) ((Math.random() * 40) + 18);
  2. In the above expression, do you need all of the brackets?

  3. You must also give the employees random names, which can be done in a similar way:

    char c = (int) ((Math.random() * 26) + 65);
  4. This gives you a random character, which you can assemble into Strings, and use the methods toUpperCase() and toLowerCase() in the String class to make their names have the appropriate case.

  5. If you want to have 'normally' distributed ages, you might try experimenting with the class java.util.Random, which has an instance method nextGaussian(). But remember the ages are not unlimited!

  6. Refactor your code so all this random stuff is encapsulated. Perhaps the Employee class could have a method createRandomEmployee()?

  7. Similarly, create random Managers, and devise some way to build a random organisation of Managers and Employees.

  8. In such a fashion, create a million Employees!

  9. The class method System.currentTimeMillis() tells you the epoch-based time (i.e. the number of milliseconds since the 1st Jan 1970!). You can use this to measure the time your application runs for. Do this and record your execution time.

  10. Experiment with different Collection implementation classes to see which one is faster.

 

 

Related content