Download presentation
Presentation is loading. Please wait.
Published byMildred Singleton Modified over 9 years ago
1
CSC 205 – Java Programming II Lecture 22 March 1, 2002
2
User’s v.s. Developer’s Views User’s view –A better name could be client or external view –A user or client of an object o shouldn’t be bothered with the implementation details of o How does o store its state? How does o perform an action? Developer’s view –A better name could be internal view
3
Client of Server Roles Program that uses collection add remove size contains Client object (e.g. a MyMusic object) Server object (e.g. the LinkedCollection object) Entities
4
Why Do We Need Abstraction
5
Using Collection Objects cd1 cd2null cassette1null 0 1 musics: Linked Collection MyMusics aTitle 1 cassette2 musics[0]: LinkedCollection musics[1]: LinkedCollection m: Music Add a Music object m musics[m.getType()].add(m); Remove a Music object m musics[m.getType()].remove(m);
6
Internal View YESNO MAYBE head newEntry null Add a new entry newEntry newEntry.next = head; head = newEntry;
7
The equals Method prublic boolean equals (Object o) { if ( !(o instance of Date) ) return false; else Date d = (Date) o; return (year==d.getYear() && month==d.getMonth() && day==d.getDay()); }
8
Big O Notations A rough estimation of how fast a function f(n) increase with the growth of n The hierarchy of orders O(1) O(log 2 n) O(n) O(n log 2 n) O(n 2 ) … O(2 n ) When n = 1,000,000 – log 2 n ~ 20 – n = 1,000,000 –n log 2 n ~ 20,000,000 – n 2 = 1,000,000,000,000
9
The Smallest Upper Bound Question 4(a) –Expand the expression f(n) = (2+n)(3+log 2 n) = 6+2log 2 n+3n+n log 2 n –Find the highest order term n log 2 n O(n log 2 n) –Use definition f(n) = K In this case K = 8, C = 4.
10
Show Equivalency Question 4(b) –Use definition f(n) is O(n+7) means f(n) = K –Substitute C(n +7) = Cn +7C = C –Proved f(n) = K ’ f(n) is O(n) where C ’ = C+7, K ’ = C
11
Recursion private static int puzzle (int n) { if ( (n % 3) == 2 ) return 1; //base case else if ( (n % 3) == 1 ) return ( puzzle (n + 1) + 2 ); else return ( puzzle (n / 3) + 1 ); }
12
Tracing Recursive Processes puzzle(9) LV: n=9 RA: return puzzle(3)+1 puzzle(3) LV: n=3 RA: return puzzle(2)+1 puzzle(2) LV: n=2 RA: return puzzle(1)+1 puzzle(1) return 1 Push execution state in stack: Local Variables Return Address Pop execution state from stack: Local Variables Return Address
13
Towers of Hanoi
14
Algorithm move (n, orig, dest, temp) { if (n == 1) Move disk from orig to dest; else { move (n - 1, orig, temp, dest); move (1, orig, dest, temp); move (n - 1, temp, dest, orig) ; } // else }
15
Tracing Recursive Processes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.