CS61B CSM review session PDF

Title CS61B CSM review session
Author Chloe Lee
Course Data Structures
Institution University of California, Berkeley
Pages 16
File Size 302.2 KB
File Type PDF
Total Downloads 22
Total Views 152

Summary

Josh Hug...


Description

CS6 1 BCSM r e v i e wfin a ls e s s i on t r y–c a t c h–fina l l y fin a l l yb l o c kwi l la l wa y sr u n - c a n ’ tc a t c ht h es a mee x c e p t i o nmo r et h a no n c e p a s sb yv a l u e :As s i g n me ntc o p i e st h eb i t sf r o mo nev a r i a b l et oa n o t h e r - Pr i mi t i v e s :Bo o l e a n , b y t e , c h a r , s h or t , i n t , l o n g , flo a t , d o u b l e Pa s sb yr e f e r e n c e :As s i gn me n tc o p i e st h ea d d r e s so fon ev a r i a b l et oa no t h e r : Ge n e r i c s: - Ma k ei te a s yt os t o r ea n yd a t at y p ewi t h i no u rd a t as t r u c t u r e -

Ad d e dFl e x i b i l i t yt op r og r a ma n dc l e a n e rc o de( d o n ’ tn e e dt oo v e r u s eOb j e c tc l a s sYo u c a n ' ti n s t a n t i a t ea ni n s t a n c ewi t hy ou rg e n e r i ck e ywo r d

-

Tt=n e wT( )

I t e r a t o r / i t e r a b l e : I t e r a t o ri sl i ket h ebo o k , i t e r a b l ei st h eb o o kma r k - Cl a s si mp l e me n t si t e r a b l ea ndh a st hef u nc t i o ni t e r a b l et h a tr e t u r nst h ei t e r a t o rde fin e df o r t h ec l a s s - I t e r a t o rc l a s si mpl e me n t si t e r a t o ra ndh a st heh a s Ne x t ( )–Bo o l e a n/ ne x t ( ) -n e x te l e me n tt o r e t ur nf u n c t i o n s ADT: - ADTsa r ed e fin e db yab e h a v i o ra n ds e to fa c t i o n st h a tt h e yc a nd o - Wet h e ni mpl e me n tt h e s eADTsu s i n g1o rmo r ed a t as t r u c t u r e s - I mpl e me n t a t i o n so f t e nd i ffe ri nh o wt h e ys t or ei t e ms , h o wmu c hs p a c et h e yt a k e , a ndt h e r u n t i meo fwo r ki n gwi t ht he m ADT

Li s t

De fin i n g Or de rof Be h a vi o r a c o l l e c t i o n o r s e q u e n c e o f e l e me n t s

Se t

Ma p

St a c k

Qu e u e

Pr i o r i t y Que u e

( un o r d e r e d ) c o l l e c t i o no f uni que e l e me n t s

Co l l e c t i o n o fke y v a l ue ma p pi n g s

Li n e a r c o l l e c t i o n wi t h LI FO r e mo v a l

Li n e a r c ol l e c t i o n wi t hFI FO r e mo v a l

Li n e a r c ol l e c t i o n wi t h p r i o r i t y r e mo v a l

Tr e e Ma p :u n i q uea n di n o r d e r( i fu s ei no r d e rt r a v e r s a l ) - l e f tc h i l d r e nn od ea n dr i g htc h i l d r e n Ob j e c tOr i e n t e dPr o g r a mmi n g :

I n he r i t a n c e : - I n h e r i t a n c ei sd e fin i n gl o g i c a l h i e r a r c hi c a lr e l a t i on s h i p sb e t we e nc l a s s e ss u c ht h a tweh a v e de c r e a s e dc o d er e u s e - I fCl a s sBe x t e n d s /i mp l e me n t sCl a s sA, t h e n : - Ai sas u pe r c l a s s / p a r e n to fB - Bi sas u b c l a s s / c h i l do fA I n t e r f a c eI n h e r i t a n c e :Sp e c i f y i n gt h ec a p a b i l i t i e sofas u b c l a s su s i n gt hei mpl e me nt sk e ywo r di s c a l l e dt h ei n t e r f a c ei nh e r i t a n c e . - Su bc l a s s e smu s to v e r r i d ea l lt h eme t h od si na ni nt e r f a c e . As u bc l a s si n h e r i t sa l lme mbe r sf r omme mbe r so fas u p e r c l a s se x c e p tt h ef ol l o wi n g : 1 .Pr i v a t ev a r i a bl e s /me t ho d s 2 .Co n s t r u c t orme t h o d s Sub c l a s s e sc a ni n he r i ta l lo t h e rme mb e r sl i k es t a t i cme t h o d sa n ds t a t i cfin a lv a r i a b l e s ;ho we v e r , t h e s eme t h o d sc a n n o tb eo v e r r i d d e n . * *Yo uc a no nl ye xt e ndon ec l a s s , b u ty o uc a ni mpl e me n tmu l t i p l ec l a s s e s . Me t h o do v e r r i d i n gv s . me t h o do v e r l o a d i n g I fas u b c l a s sha same t ho dwi t ht hee x a c ts a meme t ho ds i gn a t u r ea si nt hes u p e r c l a s s ,wes a yt h e s u bc l a s so v e r r i de st h eme t h od . Me t h o dwi t ht h es a men a mebu td i ffe r e n ts i g na t ur e sa r eo v e r l o a de d. -

Dy n a mi cme t h o ds e l e c t i o n s : St a t i ct y p e :t y p es p e c i fie da td e c l a r a t i on

1 .Dy n a mi ct y p e :t y pes p e c i fie da ti ns t a n t i a t i o n . Th ec o mpi l e rc he c k st h a tt h es t a t i ct y p eo f t h eme t h o db e i n gc a l l e de x i s t si nt h ec l a s swhi c ht hes t a t i ct y p er e f e r st o . 1 .I fi tD. N. E. , weg e tac o mp i l e re r r o r b .Th ec o mp i l e rr e me mb e r st h es i g n a t u r eo ft h i sme t h o d( n a me , r e t u r nt y p e , a r g u me n t s ) c .Du r i n gr un t i me , wes e a r c hf o rt h er e me mb e r e ds i g na t u r eb a s e do nt h ed y n a mi ct y p ec l a s s . I fi te x i s t s , weu s et hed y na mi ct y p eme t h o d . Ot h e r wi s e , weu s et h es t a t i ct y p eme t ho d . St e p s : 1 .Ch e c kd y n a mi ct y p ei sas u b c l a s so ft hes t a t i ct y pe , ori st h es t a t i ct y p ei t s e l f . 2 .Ch e c kf orme t h o di ns t a t i ct y p ec l a s s . I fme t ho dDNE, i ti saComp i l eTi meEr r o r ! 3 .I fme t h o de x i s t si ns t a t i ct y p ec l a s s , c h e c kd y n a mi ct y p ec l a s st os e ei ft h eme t ho di s o v e r r i d d e n.I ti so n l yo v e r r i d d e ni ft h en a mea n dALLs i gn a t u r e sa r ee x a c t l yt h es a me . 4 .I fme t h o di so v e r r i dd e ni nd y n a mi ct y p ec l a s se x e c u t et ha to t h e r wi s ee x e c u t eme t ho di n s t a t i ct y p e . Ca s t i n g :  T a k i n ga no b j e c to fon es t a t i ct y p ea n dt e l l i n gt hec o mpi l e rt ot hi n ko ft h eo b j e c ta s ha v i n ga n o t h e rs t a t i ct y p e .  Do wnc a s t i n g( Pa r e n t→ Ch i l d ) :



o

T ur n i n ga no b j e c ti n t oa“ mo r es p e c i fic ”t y p eo fo b j e c t

o

Al l o we di fo b j e c tc o u l db et h a tt y p e

Up c a s t i n g( Ch i l d→ Pa r e n t ) : o T ur n i n ga no b j e c to famo r es p e c i fict y pet oal e s ss p e c i fict yp e o Al wa y sa l l o we d

St a t i cv s . i n s t a nc eme t h o d 



I n s t a n c eme t h o da r eme t h o dswh i c hr e q ui r ea no b j e c to fi t sc l a s st ob ec r e a t e db e f o r ei t c a nbec a l l e d . I n s t a n c eme t h od sb e l o n gt ot heo b j e c ta n dn o tt hec l a s sFo re x a mp l e , t h el e n g t h ( )me t h o d r e t ur n st h el e n g t hofas t r i n ga n di ti sd i ffe r e ntf o re v e r ys t r i n g . St a t i cme t ho d sa r et h eme t h o d si nJ a v at h a tc a nbec a l l e dwi t h o utc r e a t i n ga no b j e c to f c l a s s . St a t i cme t h o d sb e l o n gt ot hec l a s sa sawh o l en o tt h eo b j e c t . Fo re xa mp l e , Ma t h . a b s ( ) f u n c t i o ni sas t a t i cme t h o d.

Ru n t i mea n a l y s i s Weu s ed i ffe r e n tn o t a t i o nt od i ffe r e n t i a t ebe t we e nu p p e rbo u n d , l o we rb o un d , t i g htb o u n d o O( N)i sa nuppe rbo und, ! =wo r s tc a s e o Ω( N)i sal o we rbo und, ! =be s tc a s e o Θ( N)i sat i ghtbo und o g ( N)∊ Θ( N)⇔ g ( N)∈ O( N)a n dg ( N)∊ Ω( N) o I nEn g l i s h :gi st i g h t l yb o u n d e db yaf u n c t i o ni fa n do n l yi fi ti su pp e ra n dl o we r b ou n d e db yt h es a mef u n c t i o n i ft h ehe i g h toft h et r e ei sl o g na n de a c hn o d ed o e sc on s t a n two r k ,t h e nt h e t a ( n ) publ i cv o i df o o ( i n tN, i n tm){ i f( m==0){r e t ur n ; } f o r( i n ti=0 ;i...


Similar Free PDFs