Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2 and Java’s Comparator Interface McCloskey, Beidler, & Bi Computing Sciences University of Scranton.

Similar presentations


Presentation on theme: "CS 2 and Java’s Comparator Interface McCloskey, Beidler, & Bi Computing Sciences University of Scranton."— Presentation transcript:

1 CS 2 and Java’s Comparator Interface McCloskey, Beidler, & Bi Computing Sciences University of Scranton

2 CS 2 and Java’s Comparator Interface Using Interfaces Comparable versus Comparator Why aren’t you using the Comparator Interface? How to apply the Comparator Interface Observations

3 Using Interfaces Java Interfaces –Specifications: Let’s you say what is expected to be accomplished without committing to how it will be carried out –Standards: Let’s you describe a standard that may be applied over a collection of classes A great software engineering tool

4 Comparable versus Comparator Comparable Interface –Standard that is applied when a class has a natural order. –Implemented within the class with a compareTo method. –The compareTo method should apply a total ordering on the class

5 Comparable versus Comparator Comparator Interface –Standard that is applied to describe a problem dependent ordering of a class. –Implemented outside the class with a class that contains a compare method that maps a total ordering onto the objects in the target class. –Frequently the compare method makes use of the compareTo methods of a class’s instance variables

6 Why aren’t you using the Comparator Interface? Didn’t really know about it The Comparable interface is good enough. I can do everything with it. I don’t need the Comparator interface. Too be honest, I couldn’t quite figure out how to implement it.

7 Why aren’t you using the Comparator Interface? If you are overusing the Comparable interface and not using the Comparator interface (like most text books) you are doing a disservice to your students. Why?

8 Why aren’t you using the Comparator Interface? Every problem dependent ordering should be carried out with a Comparator.

9 Why aren’t you using the Comparator Interface? Every class that expects to order objects from another class should be constructed to accept the ordering from a comparator. –Array Ordering, logical ordering –Ordered List, skip list –BST, AVL, … Writing an ordering class to use comparators makes it industrial strength; it can order anything.

10 How to apply the Comparator Interface Constructing a class that implements the comparator interface public class MyComparator implements Comparator{ public int compare(Object L, Object R){ // returns a negative int if LR }

11 How to apply the Comparator Interface Illustration: Assume that objects in a class Able have an instance variable accessed by getData() and it is a Comparable object. public class MyCtor implements Comparator{ public int compare(ObjectL, Object R){ return ((Able)L).getData().compareTo(((Able)R).getData()); }

12 How to apply the Comparator Interface Comparator may include a constructor public class MyCtor implements Comparator{ MyCtor (){... } public int compare(ObjectL, Object R){... } }

13 How to apply the Comparator Interface Comparator Class Application

14 How to apply the Comparator Interface Student Class: Possible orderings –Alpha by last name –By class (no. of credits) –By ZIP –By major –By various combinations of instance variables

15 How to apply the Comparator Interface Comparator 1 Application 1 Class Comparator 2 Application 2 Application n Comparator n

16 How to apply the Comparator Interface Comparator 1 Class Comparator 2 Application Comparator n

17 How to apply the Comparator Interface Comparator c = new SomeComparator(); … BinSearchTree T = new BinSearchTree(c); … Using a comparator to implement an ordering class, like a BST, expands reusability (genericity) of the ordering class.

18 Observations See examples in the paper –Student class Basic comparators – simple orderings Compound comparators –By class, by GPA, by … –By any logical combination of the above Logical (indirect) ordering

19 Conclusions Are you teaching Java. Or are you teaching how to develop software in Java using, and building upon, the API Applying the Comparator interface for all problem dependent orderings enhances software reuse.

20 Conclusions Our resources http://www.cs.uofs.edu/~beidler/cmps240/text/ Username: cmps240 Password: bebimc


Download ppt "CS 2 and Java’s Comparator Interface McCloskey, Beidler, & Bi Computing Sciences University of Scranton."

Similar presentations


Ads by Google