Inheritance and Interfaces l l Inheritance models an

  • Slides: 16
Download presentation
Inheritance and Interfaces l l Inheritance models an "is-a" relationship Ø A dog is

Inheritance and Interfaces l l Inheritance models an "is-a" relationship Ø A dog is a mammal, an Array. List is a List, a square is a shape, … Write general programs to understand the abstraction, advantages? void execute(Pixmap target) { // do something } l But a dog is also a quadruped, how can we deal with this? CPS 100 e 5. 1

Single inheritance in Java l A class can extend only one class in Java

Single inheritance in Java l A class can extend only one class in Java Ø All classes extend Object --- it's the root of the inheritance hierarchy tree Ø Can extend something else (which extends Object), why? l Why do we use inheritance in designing programs/systems? Ø Facilitate code-reuse (what does that mean? ) Ø Ability to specialize and change behavior • If I could change how method foo() works, bar() is ok Ø Design methods to call ours, even before we implement • Hollywood principle: don't call us, … CPS 100 e 5. 2

Comparable and Comparator l l Both are interfaces, there is no default implementation Ø

Comparable and Comparator l l Both are interfaces, there is no default implementation Ø Contrast with. equals(), default implementation? Ø Contrast with. to. String(), default? Where do we define a Comparator? Ø In its own. java file, nothing wrong with that Ø Private, used for implementation and not public behavior • Use a nested class, then decide on static or non-static • Non-static is part of an object, access inner fields l l How do we use the Comparator? Ø Sort, Sets, Maps (in the future) Does hashing (future topic) have similar problems? CPS 100 e 5. 3

Sets l Set is an unordered list of items Ø Items are unique! Only

Sets l Set is an unordered list of items Ø Items are unique! Only one copy of each item in set! We will use two different implementations of sets Tree. Set l A Tree. Set is backed up by a tree structure (future topic) Ø Keeps items sorted (+) Ø Slower than Hash. Sets ? ? (-) Hash. Set l l Ø Ø A Hash. Set is backed up by a hashing scheme (future topic) Items not sorted – should seem to be in random order (-) Faster than Tree. Sets ? ? (+) CPS 100 e 5. 4

Using Both Array. List and Sets l l l You may want to use

Using Both Array. List and Sets l l l You may want to use a set to get rid of duplicates, then put the items in an Array. List and sort them! Problem: Ø Often data comes in the form of an array Ø How do we go from array to Array. List or Tree. Set? Problem: Ø Often we are required to return an array Ø How do we go from a Collection such as an Array. List or Tree. Set to an array? Can do it the “hard” way with loops or iterators: Ø one item at a time OR: CPS 100 e 5. 5

Data processing example l Scan a large (~ 107 bytes) file Print the 20

Data processing example l Scan a large (~ 107 bytes) file Print the 20 most frequently used words together with counts of how often they occur Need more specification? l How do you do it? l l CPS 100 e 5. 6

Possible solutions 1. 2. • Use heavy duty data structures (Knuth) Ø Hash tries

Possible solutions 1. 2. • Use heavy duty data structures (Knuth) Ø Hash tries implementation Ø Randomized placement Ø Lots o’ pointers Ø Several pages UNIX shell script (Doug Mclroy) tr -cs “[: alpha: ]” “[n*]” < FILE | sort | uniq -c | sort -n -r -k 1, 1 | head -20 Which is better? Ø K. I. S. ? CPS 100 e 5. 7

Dropping Glass Balls l l l Tower with N Floors Given 2 glass balls

Dropping Glass Balls l l l Tower with N Floors Given 2 glass balls Want to determine the lowest floor from which a ball can be dropped and will break How? What is the most efficient algorithm? How many drops will it take for such an algorithm (as a function of N)? CPS 100 e 5. 8

Glass balls revisited (more balls) l l 1. 2. 3. 4. 5. 6. 7.

Glass balls revisited (more balls) l l 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Assume the number of floors is 100 In the best case how many balls will I have to drop to determine the lowest floor where a ball will break? 1 2 10 16 17 18 20 21 51 100 l In the worst case, how many balls will I have to drop? 1. 1 2 10 16 17 18 20 21 51 100 2. 3. 4. 5. 6. 7. 8. 9. 10. If there are n floors, how many balls will you have to drop? (roughly) CPS 100 e 5. 9

What is big-Oh about? (preview) l Intuition: avoid details when they don’t matter, and

What is big-Oh about? (preview) l Intuition: avoid details when they don’t matter, and they don’t matter when input size (N) is big enough Ø For polynomials, use only leading term, ignore coefficients y = 3 x y = x 2 l y = 6 x-2 y = x 2 -6 x+9 y = 15 x + 44 y = 3 x 2+4 x The first family is O(n), the second is O(n 2) Ø Intuition: family of curves, generally the same shape Ø More formally: O(f(n)) is an upper-bound, when n is large enough the expression cf(n) is larger Ø Intuition: linear function: double input, double time, quadratic function: double input, quadruple the time CPS 100 e 5. 10

More on O-notation, big-Oh l Big-Oh hides/obscures some empirical analysis, but is good for

More on O-notation, big-Oh l Big-Oh hides/obscures some empirical analysis, but is good for general description of algorithm Ø Allows us to compare algorithms in the limit • 20 N hours vs N 2 microseconds: which is better? l O-notation is an upper-bound, this means that N is O(N), but it is also O(N 2); we try to provide tight bounds. Formally: Ø A function g(N) is O(f(N)) if there exist constants c and n such that g(N) < cf(N) for all N > n cf(N) g(N) CPS 100 e x = n 5. 11

Which graph is “best” performance? CPS 100 e 5. 12

Which graph is “best” performance? CPS 100 e 5. 12

Big-Oh calculations from code l Search for element in an array: Ø What is

Big-Oh calculations from code l Search for element in an array: Ø What is complexity of code (using O-notation)? Ø What if array doubles, what happens to time? for(int k=0; k < a. length; k++) { if (a[k]. equals(target)) return true; }; return false; l Complexity if we call N times on M-element vector? Ø What about best case? Average case? Worst case? CPS 100 e 5. 13

Amortization: Expanding Array. Lists l l Expand capacity of list when add() called Calling

Amortization: Expanding Array. Lists l l Expand capacity of list when add() called Calling add N times, doubling capacity as needed Item # Resizing cost Cumulative Resizing Cost Capacity After add cost per item 1 2 3 -4 0 2 6 0 1 1. 5 1 2 4 5 -8 8 14 1. 75 8 2 m+1 - 2 m+1 2 m+2 -2 around 2 2 m+1 l What if we grow size by one each time? CPS 100 e 5. 14

Some helpful mathematics l 1+2+3+4+…+N Ø N(N+1)/2, exactly = N 2/2 + N/2 which

Some helpful mathematics l 1+2+3+4+…+N Ø N(N+1)/2, exactly = N 2/2 + N/2 which is O(N 2) why? l N + N + …. + N (total of N times) Ø N*N = N 2 which is O(N 2) l N + N + … + N (total of 3 N times) Ø 3 N*N = 3 N 2 which is O(N 2) l 1 + 2 + 4 + … + 2 N Ø 2 N+1 – 1 = 2 x 2 N – 1 which is O(2 N ) l Impact of last statement on adding 2 N+1 elements to a vector Ø 1 + 2 + … + 2 N+1 = 2 N+2 -1 = 4 x 2 N-1 which is O(2 N) resizing + copy = total (let x = 2 N) CPS 100 e 5. 15

Running times @ 106 instructions/sec N O(log N) O(N 2) 10 0. 000003 0.

Running times @ 106 instructions/sec N O(log N) O(N 2) 10 0. 000003 0. 00001 0. 000033 0. 0001 100 0. 000007 0. 00010 0. 000664 0. 1000 1, 000 0. 000010 0. 00100 0. 010000 1. 0 10, 000 0. 000013 0. 01000 0. 132900 1. 7 min 100, 000 0. 000017 0. 10000 1. 661000 2. 78 hr 19. 9 11. 6 day 18. 3 hr 318 centuries 1, 000 0. 000020 1. 0 1, 000, 0 0. 000030 16. 7 min 00 CPS 100 e O(N log N) 5. 16