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
- Start with this starter code.
- You'll need to make a project and then copy this code in as the
srcfolder. - 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?
- You'll need to make a project and then copy this code in as the
- Complete the code in
Main.javain each of the starter code's three packages:- Each package has an additional class that each
Mainis trying to use. - Each
Mainhas 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_sortclownsyou'll need to implement the methods in theMainclass. Most of these require you to instantiate an object of typeShoeSizeComparatorand passing it to theclowns.sort()method. - Pay attention to the comments; they will tell you how you should achieve this.
- Each package has an additional class that each
Discussion Question
You should now be able to answer the following questions (not marked):
- From the
a_sortclownspackage: what is the difference betweenComparableandComparator? - Which approach did you like best {inner class, anon class, lambda function, method reference}? Why?
- Which of the packages did not work with lambdas or method references? Why?
- 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.