Lesson Plan

Comparator Interface Implementation Using Anonymous Inner class

Lesson Description

Teaches students about the Comparator Interface and how it is different from Comparable. Shows students how to implement the Comparator Interface using an Anonymous Inner Class structure.

Prerequisite Knowledge

Students must already know about Anonymous Inner Classes.

Length of Completion

Lecture: 30-45 minutes

Lab: 45-60 Minutes

Level of Instruction

This lesson is for High School Juniors and Seniors.

Concepts to be Taught

This lesson teaches how to implement Comparators using AICs and why they are useful.

Resources Needed

Accommodations

Lesson Outcomes (TSWBAT ...)

The students will be able to create Comparators (implemented with Anonymous Inner Classes) to be used to sort objects in an array or ArrayList.

Interconnection with Other Lessons

Students must have already been taught about Anonymous Inner Classes before this lesson.

Assessments

Extension Activities

After this lecture, students must complete the Comparator lab in the on-line textbook. This lab requires them to take their Dog code from AP CSA and use an Anonymous Inner Class to create a Comparator to sort the dogs.

Differentiated Learning Opportunities

Lesson Warm Up

Create a new project using IntelliJ (not BlueJ) called the "Cat Project". Declare two state variables for the Cat class: String name and int lives.

Using IntelliJ's built-in auto-code generation capabilities, demonstrate to the students how to auto-generate the following:

  1. getters

  2. setters

  3. a toString method

  4. a full-featured constructor

  5. a default constructor with no arguments

  6. a constructor that only takes the name as a parameter

For the default and name-only constructors, set the lives variable to 9, indicating the old superstition that a cat is born with 9 lives.

Create some test code for the Cat class in a separate class called CatTester. Create an array of at least 5 cats, some with the same number of lives and some with a different number. Create a static printArray method in the test code. Run the test code and print out the array of cats. Make sure the array is not already sorted in any way.

Now go back and implement the Comparable interface on the Cat class. In the compareTo method, sort the cats by the number of lives. Then add the following to the test code:

printArray(cats);
Arrays.sort(cats);
printArray(cats);

Show the students how the implementation of the Comparable interface allows the cats to be sorted.

Lesson Details

Ask what is to be done if we did not have access to the Cat code and the Comparable Interface was not implemented, or if we wanted the cats sorted in a way different from what was implemented.

After a student suggests that we could write our own Cat class that inherits from Cat and implements Comparable, mention that writing a whole new class just to make the cats sortable would be unnecessary. We could use an Anonymous Inner Class instead.

Now introduce the concept of the Comparator Interface. Describe how it is different from Comparable. Say that Comparable requires the compareTo method while Comparator needs a compare method, instead. Also mention that while compareTo for Comparable has only one parameter (the other Object), the compare method for Comparator has two parameters - the two objects being compared.

Go back and remove the Comparable Interface code from the Cat class that you wrote earlier. In the test code, use an AIC to implement a Comparator object. This time, sort the cats by name.

Pass this Comparator object to the sort routine and demonstrate how the cats can be sorted alphabetically by name:

printArray(cats);
Arrays.sort(cats, c); // c is the Comp
arator object created with an AIC
printArray(cats);

After demonstrating the use of an AIC object, show how an AIC can be created in-line:

printArray(cats);
Arrays.sort(cats,
new Comparator() { ... });
printArray(cats);

Finally, show how this can be done using a functional technique:

printArray(cats);
Arrays.sort(cats,
(c1, c2) -> c1.getName().compareTo(c2.getName());
printArray(cats);

Mention that the functional technique will be studied at the end of the course in June.