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

CS 2 and Java’s Comparator Interface

Embed Size (px)

DESCRIPTION

CS 2 and Java’s Comparator Interface. McCloskey, Beidler, & Bi Computing Sciences University of Scranton. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 2 and Java’s  Comparator Interface

CS 2 and Java’s Comparator Interface

McCloskey, Beidler, & Bi

Computing Sciences

University of Scranton

Page 2: CS 2 and Java’s  Comparator Interface

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

Page 3: CS 2 and Java’s  Comparator Interface

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

Page 4: CS 2 and Java’s  Comparator Interface

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

Page 5: CS 2 and Java’s  Comparator Interface

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

Page 6: CS 2 and Java’s  Comparator Interface

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.

Page 7: CS 2 and Java’s  Comparator Interface

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?

Page 8: CS 2 and Java’s  Comparator Interface

Why aren’t you using the Comparator Interface?

• Every problem dependent ordering should be carried out with a Comparator.

Page 9: CS 2 and Java’s  Comparator Interface

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.

Page 10: CS 2 and Java’s  Comparator Interface

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 L<R

// 0 if L==P

// a positive int if L>R

}

}

Page 11: CS 2 and Java’s  Comparator Interface

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());

}

}

Page 12: CS 2 and Java’s  Comparator Interface

How to apply the Comparator Interface

• Comparator may include a constructor

public class MyCtor implements Comparator{

MyCtor (){ . . . }

public int compare(ObjectL, Object R){ . . . }

}

Page 13: CS 2 and Java’s  Comparator Interface

How to apply the Comparator Interface

Comparator

Class

Application

Page 14: CS 2 and Java’s  Comparator Interface

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

Page 15: CS 2 and Java’s  Comparator Interface

How to apply the Comparator Interface

Comparator1

Application1

Class Comparator2

Application2

Applicationn

Comparatorn

Page 16: CS 2 and Java’s  Comparator Interface

How to apply the Comparator Interface

Comparator1

Class Comparator2

Application

Comparatorn

Page 17: CS 2 and Java’s  Comparator Interface

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.

Page 18: CS 2 and Java’s  Comparator Interface

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

Page 19: CS 2 and Java’s  Comparator Interface

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.

Page 20: CS 2 and Java’s  Comparator Interface

Conclusions

• Our resourceshttp://www.cs.uofs.edu/~beidler/cmps240/text/

Username: cmps240

Password: bebimc