1 / 162

COP 3503 FALL 2012 Shayan Javed Lecture 7

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.

barb
Télécharger la présentation

COP 3503 FALL 2012 Shayan Javed Lecture 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 FALL 2012ShayanJavedLecture 7 Programming Fundamentals using Java

  2. More With Interfaces

  3. Interfaces • Language construct for specifying functionality without implementation • Method specifications but no implementations.

  4. Interfaces in Software Engineering • In large projects you have a lot of classes interacting with each other

  5. 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.

  6. 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.

  7. 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.

  8. Interfaces • Some interfaces already defined in Java • Widely used • Comparable • Comparator • Cloneable

  9. The Comparable interface • Defined in the java.langPackage

  10. The Comparable interface • Defined in the java.langPackage publicinterface Comparable { publicintcompareTo(Object o); }

  11. The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); }

  12. The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type

  13. The Comparable interface publicinterface Comparable { publicintcompareTo(Object o); } • Comparisons between objects of the same type • Object1.compareTo(Object2)

  14. 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)

  15. 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

  16. 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)

  17. The Comparable interface • Used commonly.

  18. The Comparable interface • Used commonly. • For ex. the String and Date classes.

  19. The Comparable interface public String implements Comparable,… { publicintcompareTo(Object o) { // compares the two Strings lexicographically } }

  20. The Comparable interface public String implements Comparable,… { publicintcompareTo(Object o) { // compares the two Strings lexicographically } } Ex.: "computer".compareTo ("comparison")

  21. The Comparable interface public String implements Comparable,… { publicintcompareTo(Object o) { // compares the two Strings lexicographically } } Ex.: "computer".compareTo ("comparison") Returns 20

  22. The Comparable interface "computer".compareTo ("comparison") Returns 20 • Provides the first non-zero difference in ASCII values.

  23. The Comparable interface "computer".compareTo ("comparison") Returns 20 • Provides the first non-zero difference in ASCII values. • “c”, “o”, “m”, “p” are all equal

  24. 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’

  25. 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”

  26. The Comparable interface • Implement it for the Rectangle class

  27. The Comparable interface • Implement it for the Rectangle class • Comparisons based on area

  28. 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; } }

  29. The Comparable interface • What if a non-Rectangle object is passed in?

  30. The Comparable interface • What if a non-Rectangle object is passed in? • A ClassCastException is thrown

  31. The Comparable interface • What if a non-Rectangle object is passed in? • A ClassCastException is thrown • Will look at it later

  32. The Comparable interface • Can also use Comparable as a data type

  33. The Comparable interface • Can also use Comparable as a data type String s = “aString”; (s instanceofComparable) // returns true!

  34. The Comparable interface • Can also use Comparable as a data type String s = “aString”; (s instanceofComparable) // returns true! Comparable[] compObjects = new Comparable[5];

  35. The Comparable interface • So now we can compare objects

  36. The Comparable interface • So now we can compare objects • Would be nice if we could sort them using this info

  37. Sorting • Always need data sorted by certain requirements

  38. Sorting • Always need data sorted by certain requirements • How do you sort arrays?

  39. Sorting • Always need data sorted by certain requirements • How do you sort arrays? • Will look at specific sorting algorithms later on.

  40. Sorting • Sorting on Amazon’s website

  41. Sorting • By different categories

  42. Comparing Books public Book implements Comparable{ String title; int popularity; double price, avgCustomerReview; DatepublicationDate; }

  43. 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; } }

  44. Arrays.sort() • An easy way to sort an array

  45. Arrays.sort() • An easy way to sort an array • Works directly with most of the primitive types – int, char, double, long, etc.

  46. 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);

  47. 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]

  48. Arrays.sort() • Can also sort arrays of Objects

  49. Arrays.sort() • Can also sort arrays of Objects • Arrays.sort(Object[] o)

  50. Arrays.sort() • Can also sort arrays of Objects • Arrays.sort(Object[] o) • Make sure that the objects in the array: • Implement Comparable

More Related