Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using interfaces 3.0. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.

Similar presentations


Presentation on theme: "Using interfaces 3.0. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum."— Presentation transcript:

1 Using interfaces 3.0

2 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum element in a collection? Could this code be generalised? How would you sort a collection? Could this code be generalised? What type would such a method have?

3 3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using the Collections Class The collections class provides methods that can be used on any collection: Lists and Sets Can be used for –finding max –sorting –searching –lots of other things as well

4 4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using max The signature of the max method is public static > T max(Collection coll) In other words, T must implement the Comparable interface

5 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Interfaces An interface is a list of method signatures It can be thought of as a “completely abstract type”: no implementation detail Can use interface names as types Good for separate compilation

6 6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The Comparable Interface public interface Comparable { int compareTo(T o); } Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

7 7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Writing compareTo Write compareTo methods for the following classes Player with an int field goalScored Club with an int field points and an int field goalDifference

8 8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using max etc Having defined the compareTo method adapt the class declaration public class Player implements Comparable{ }

9 9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Club{ ArrayList squad; //… public Player topScorer(){ return Collections.max (squad); }

10 10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Different sorting criteria You may need to sort things in different ways E.g. different people would have a favourite present Can use an alternative max (and sort etc) with the Comparator interface

11 11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public interface Comparator { int compare(T o1,T o2); boolean equals(Object obj); } Could always use equals from Object: nothing to do

12 12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Pass the comparator object into a Collections method to use it e.g. public static void sort (List list, Comparator c) N.B. List is also an interface, implemented by ArrayList


Download ppt "Using interfaces 3.0. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum."

Similar presentations


Ads by Google