Anon Classes, Lambda Expressions, Method Reference Exercise

Learning Outcomes

  • Able to pass an object to a method using: inner class, anon class, lambda expression, method reference.
  • Understand when each approach can be used.
  • Form an opinion on which approach is best.
  • Bonus: Introduced to the Interface Segregation principle.

Coding with Anon Class, Lambda Expression, Method Reference

  1. Start with this starter code.
    • You'll need to make a project and then copy this code in as the src folder.
    • Do not use any auto-complete or AI code assistance for this exercise.
    • The purpose is to have you learn to be able to write the same code in numerous ways and evaluate these approaches.
    • AI can totally write all this code correctly; can you?
  2. Complete the code in Main.java in each of the starter code's three packages:
    • Each package has an additional class that each Main is trying to use.
    • Each Main has some empty methods which you must implement.
    • Each of these methods must instantiate an object and pass it as an argument to a method call. See the comments in each method.
    • For example, in package a_sortclowns you'll need to implement the methods in the Main class. Most of these require you to instantiate an object of type ShoeSizeComparator and passing it to the clowns.sort() method.
    • Pay attention to the comments; they will tell you how you should achieve this.

Discussion Question

You should now be able to answer the following questions (not marked):

  1. From the a_sortclownspackage: what is the difference between Comparable and Comparator?
  2. Which approach did you like best {inner class, anon class, lambda function, method reference}? Why?
  3. Which of the packages did not work with lambdas or method references? Why?
  4. Look up Interface Segregation; could that have helped let us use lambdas and method references more?


Submission

This exercise is not for marks: nothing need be submitted.