1.62k likes | 1.76k Views
COP 3503 FALL 2012 Shayan Javed Lecture 7. Programming Fundamentals using Java. More With Interfaces. Interfaces. Language construct for specifying functionality without implementation Method specifications but no implementations. Interfaces in Software Engineering.
E N D
COP 3503 FALL 2012ShayanJavedLecture 7 Programming Fundamentals using Java
Interfaces • Language construct for specifying functionality without implementation • Method specifications but no implementations.
Interfaces in Software Engineering • In large projects you have a lot of classes interacting with each other
Interfaces in Software Engineering • In large projects you have a lot of classes interacting with each other • Need formal descriptions of what a class does, and how it interacts with other classes.
Interfaces in Software Engineering • In large projects you have a lot of classes interacting with each other • Need formal descriptions of what a class does, and how it interacts with other classes. • Interfaces are a way to define expected behavior.
Interfaces in Software Engineering • In large projects you have a lot of classes interacting with each other • Need formal descriptions of what a class does, and how it interacts with other classes. • Interfaces are a way to define expected behavior. • Useful when multiple teams working in tandem.
Interfaces • Some interfaces already defined in Java • Widely used • Comparable • Comparator • Cloneable
The Comparable interface • Defined in the java.langPackage
The Comparable interface • Defined in the java.langPackage publicinterface Comparable { publicintcompareTo(Object o); }
The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); }
The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type
The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type • Object1.compareTo(Object2)
The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type • Object1.compareTo(Object2) • Return values: • < 0 = Object 1 < Object 2 (usually -1)
The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type • Object1.compareTo(Object2) • Return values: • < 0 = Object 1 < Object 2 (usually -1) • == 0 = Object 1 == Object 2
The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type • Object1.compareTo(Object2) • Return values: • < 0 = Object 1 < Object 2 (usually -1) • == 0 = Object 1 == Object 2 • > 0 = Object 1 > Object 2 (usually 1)
The Comparable interface • Used commonly.
The Comparable interface • Used commonly. • For ex. the String and Date classes.
The Comparable interface public String implements Comparable,… { publicintcompareTo(Object o) { // compares the two Strings lexicographically } }
The Comparable interface public String implements Comparable,… { publicintcompareTo(Object o) { // compares the two Strings lexicographically } } Ex.: "computer".compareTo ("comparison")
The Comparable interface public String implements Comparable,… { publicintcompareTo(Object o) { // compares the two Strings lexicographically } } Ex.: "computer".compareTo ("comparison") Returns 20
The Comparable interface "computer".compareTo ("comparison") Returns 20 • Provides the first non-zero difference in ASCII values.
The Comparable interface "computer".compareTo ("comparison") Returns 20 • Provides the first non-zero difference in ASCII values. • “c”, “o”, “m”, “p” are all equal
The Comparable interface "computer".compareTo ("comparison") Returns 20 • Provides the first non-zero difference in ASCII values. • “c”, “o”, “m”, “p” are all equal • Returns: (int)‘u’ - (int)‘a’
The Comparable interface "computer".compareTo ("comparison") Returns 20 • Provides the first non-zero difference in ASCII values. • “c”, “o”, “m”, “p” are all equal • Returns: (int)‘u’ - (int)‘a’ • So “computer” > “comparison”
The Comparable interface • Implement it for the Rectangle class
The Comparable interface • Implement it for the Rectangle class • Comparisons based on area
The Comparable interface • Implement it for the Rectangle class • Comparisons based on area public class Rectangle extendsGeometricObjectimplements Comparable { publicintcompareTo(Object ob) { Rectangle r = (Rectangle)ob; if (this.getArea() > r.getArea()) return 1; else if (r.getArea() > this.getArea()) return -1; else return 0; } }
The Comparable interface • What if a non-Rectangle object is passed in?
The Comparable interface • What if a non-Rectangle object is passed in? • A ClassCastException is thrown
The Comparable interface • What if a non-Rectangle object is passed in? • A ClassCastException is thrown • Will look at it later
The Comparable interface • Can also use Comparable as a data type
The Comparable interface • Can also use Comparable as a data type String s = “aString”; (s instanceofComparable) // returns true!
The Comparable interface • Can also use Comparable as a data type String s = “aString”; (s instanceofComparable) // returns true! Comparable[] compObjects = new Comparable[5];
The Comparable interface • So now we can compare objects
The Comparable interface • So now we can compare objects • Would be nice if we could sort them using this info
Sorting • Always need data sorted by certain requirements
Sorting • Always need data sorted by certain requirements • How do you sort arrays?
Sorting • Always need data sorted by certain requirements • How do you sort arrays? • Will look at specific sorting algorithms later on.
Sorting • Sorting on Amazon’s website
Sorting • By different categories
Comparing Books public Book implements Comparable{ String title; int popularity; double price, avgCustomerReview; DatepublicationDate; }
Comparing Books public Book implements Comparable{ String title; int popularity; double price, avgCustomerReview; DatepublicationDate; // comparison based on price publicintcompareTo(Object o) { Book b = (Book)o; if (price > b.getPrice()) return 1; else if (price <b.getPrice()) return -1; else return 0; } }
Arrays.sort() • An easy way to sort an array
Arrays.sort() • An easy way to sort an array • Works directly with most of the primitive types – int, char, double, long, etc.
Arrays.sort() • An easy way to sort an array • Works directly with most of the primitive types – int, char, double, long, etc. int numbers[] = {4, 1, 19, 8}; Arrays.sort(numbers);
Arrays.sort() • An easy way to sort an array • Works directly with most of the primitive types – int, char, double, long, etc. int numbers[] = {4, 1, 19, 8}; Arrays.sort(numbers); numbers = [1, 4, 8, 19]
Arrays.sort() • Can also sort arrays of Objects
Arrays.sort() • Can also sort arrays of Objects • Arrays.sort(Object[] o)
Arrays.sort() • Can also sort arrays of Objects • Arrays.sort(Object[] o) • Make sure that the objects in the array: • Implement Comparable