正在加载图片...
PA3 PUBLIC ANSWERS TO EXERCISES Chapter 2. Review of object orientation E2.1 p 32 Distinguishing among classes and instances The class names in the following answers could vary slightly. The points in parentheses are of lesser importance, and are for those who have read Chapter 5 a)*Instance of Automobilecompany(or perhaps just Company c)*Class, subclass of Person. (In Chapters 5 and 6, we will see that it might be better to make this a subclass of PersonRole) e)"Instance of Person E2. 2 P 33 Detecting bad class names and improving them a)*Bad. Is this a particular vehicle (which might better be called something like Locomotive, or RollingstockConfiguration)or a scheduled run(RegularlyScheduledRun)that could use any vehicle, or the run on a particular day( specificRun ) We will discuss this kind of problem in more detail in Chapter 6 in the context of the Abstraction Occurrence pattern c)*Bad. The word" is inappropriate in a class name. Call it simply sleepingCar e)*Bad. It should not be plural: Route would be bette E2.3 P 33 Naming different classes derived from words with multiple English meanings. vofritle a)*Title: Describes published books independently of the number of copies Copyofritle: or perhaps LibraryItem: Represents physical books(as well as other things, such as videos )that the library places on its shelves c)*RegularlyScheduledFlight: has a flight number, departure time, origin and destination and is flown every day; SpecificFlight: flies on a particular day E2. 4 pp 34-35 identifying attributes In this question and the next, there are many alternatives, a few possibilities are shown here. In particular ometimes certain attributes could be represented as associations instead -these are shown in parentheses a)*name, description, (producer, distributer) c)*date, startTime, endTime, description, soundAlarmWhenstarting,(room) e)'callerNumber, calledPartyNumber, isConnected, startTime, currentcell signalStrength, totalCost E2.5 p 35 Identifying associations a)boughtFrom ProductionCompany, produced By: Producer, episodes Episode leadActors Actor c)*meetingRoom: MeetingRoom e)*caller: CallParty, called: CallParty E2.6 P. 35 Differentiating between variables and objects E2.7 p. 36 ldentifying operations In the following, operations that would probably be polymorphic are shown in italics, however, exactly which operations are polymorphic would depend on the design. a)*getArea, getPerimeterLength, getPoints, move, resize, rotate flipHorizontally, flipVertically E2.8 p. 40 Identifying poor generalizations a)*Bad: You can't say 'A Canadian dollars is a money'; also CanadianDollars should be an instance of Currency c)*Probably OK e)"Bad: Account12876 would be instance of BankAccount(or some subclass of BankAccount) E2.9 No public C 2002 TIMOTHY C. LETHBRIDGE AND ROBERT LAGANIEREPA3 PUBLI C A NSW ERS T O EX E RCISE S © 2002 T IMO TH Y C. LE T HBRI D GEA N D RO BERT L AG AN I ÈRE Chapt er 2. Revi ew of object or i entat ion E 2.1 p. 32 Distinguishing am ong c lasse s and instanc es T he c lass names in the following answe rs could var y slightly. T he points in pa r enthe se s a re of lesser impor tance , a nd ar e f orthose w ho ha ver e ad Chapte r5. a )*I nsta nceof AutomobileCompany. ( orpe rha ps just Company) c )*Cla ss, subc la ss of Person. ( In Chapter s 5 a nd 6, w ew ill seethat it might be be tte r to ma ke this a subcla ss of PersonRole) e )*I nsta nceof Person. E 2.2 p. 33 De te c ting bad c lass name s and im proving them a )*Bad. I s this a par tic ular ve hic le ( w hich might better be c alled some thing like Locomotive, or RollingStockConfiguration) or a sche dule d r un (RegularlyScheduledRun) tha t c ould use a ny ve hicle , orthe r un on apa rtic ula r day ( SpecificRun) ? Wewill disc uss this kind ofproble m in mor e detail in Cha pte r 6 in thec ontext of theA bstra ction O c curr e nc e patte r n. c )*Bad. T hew ord ‘ D ata’is inappr opr ia te in a cla ss na me . Call it simply SleepingCar. e )*Bad. I t should not beplur al: Route would be bette r . E 2.3 p. 33 Naming diffe re nt classes de riv ed fromwords with m ultiple E nglish m eanings. a )*Title: D esc ribe s publishe d books independently of the number of c opies CopyOfTitle: or per ha ps LibraryItem: Repr esents physica l books (a s w ell a s other things, such a s vide os) tha t thelibr ar y pla c es on its she lve s. c )*RegularlyScheduledFlight: has a flight numbe r , de par tur e time, or igin and destination a nd is f lown ever y day; SpecificFlight: f lie s on a pa r ticularda y. E 2.4 pp. 34-35 I de ntify ing attribute s I n this que stion a nd the next, ther e a re ma ny alte r na tives, a f ew possibilitie s a re show n her e. In pa rtic ula r sometime s c er ta in attributes c ould be re pre se nte d a s assoc ia tions inste ad – thesea reshow n in par enthe se s. a )*name, description, ( producer, distributer) c )*date, startTime, endTime, description, soundAlarmWhenStarting, ( room) e )*callerNumber, calledPartyNumber, isConnected, startTime, currentCell, signalStrength, totalCost E 2.5 p. 35 Identifying assoc iations a )*boughtFrom:ProductionCompany, producedBy:Producer, episodes:Episode, leadActors:Actor c )* meetingRoom: MeetingRoom e )*caller:CallParty, called:CallParty E 2.6 p. 35 Diffe re ntiating betwee n v ariable s and obje cts a )*O bjec t E 2.7 p. 36 Identifying ope rations I n thef ollow ing, ope ra tions tha t w ould pr oba bly be polymor phic a reshow n in ita lics, how e ve r, exac tly w hic h ope ra tions ar e polymorphicw ould de pend on the design. a )*getArea, getPerimeterLength, getPoints, move, resize, rotate, flipHorizontally, flipVertically E 2.8 p. 40 Identifying poor ge neralizations a )*Bad: Y ou c a n’ t say ‘ A Cana dian dolla rs is a mone y’ ; a lso CanadianDollars should be an instanc e of Currency c )*Proba bly O K . e )*Bad: Account12876 would be insta nceof BankAccount (or some subcla ss of BankAccount) E 2.9 No public answe r
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有