正在加载图片...
PAS PUBLIC ANSWERS TO EXERCISES E2. 11 p. 47 Describing how polymorphic implementations of certain shape operations would work. a)*translate: In SimplePolygon (as inherited by Rectangle and Regularpolygon), and EllipticalShape(as inherited by circle), the method translate would move the center c)getArea: In Rectangle, the method getArea would return height width In RegularPolygon, it would compute the area by dividing the polygon into numPoints individual iangles(see the answer to E2. 30 for the detailed algorithm). Once this area is calculated, the total area of the RegularPolygon would be calculated by multiplying the area of each triangle by numPoints In Circle, the method getArea would return pi*radius E2. 12 No public answer E2. 13 P. 47 Incorporating new classes into an existing class hierarchy that contains considerable polymorphism a)*IsoscelesTriangle: One might think of making this a subclass of ArbitraryPolygon, however that would be inappropriate since you dont want it to inherit methods such as addPoint and removePoint. a better solution is to make it a subclass of SimplePolygon. As attributes you would have to store the baseLength and height, or else you could store one of the two values for angles and the length of one of the sides, letting the other angle value and side be computed when needed. As methods, you would need changeScale, setBaseLength, setHeight, getArea getPerimeterLength, getvertices, getBoundingRect, getBaseAngle, getTopAngle and perhaps setBaseAngle and setTopAngle E2. 14 No public answer E2. 15 No public answer E2. 16 No public answer E2.17 P. 49 Determining when dynamic binding is needed in a set of polymorphic methods This exercise has turned out to be particularly useful to ensure students really understand the implications of polymorphism. Before assigning this exercise, it has proved to be necessary to explain the material on pages 48 and 49 particularly carefully, with several examples. Note that the exercise has the assumption, that the compiler knows that no new classes or methods can be added to the hierarchy", it is worth reminding students that this is not generally true in Java(you can add a subclass unless the class is declared final, and you cannot declare a non-leaf class to be final) a) *Invoking getPerimeterLength on a Rectangle variable: No dynamic binding is needed since ectangle is a leaf class and so the variable could only ever contain an instance of that class. The local method in Rectangle would always be called c)*Invoking getBoundingRect on a Polygon variable: Dynamic binding would be needed, since either the method in Polygon or the one in Rectangle might have to be executed depending on which class of object is placed in the variable at run time E2.18 P. 51 Researching products that claim to be object-oriented to determine if they really are. The answer to this question will vary over time, depending on what products are availabl E2. 19 p. 53 Using documentation to look up library classes and thus better understand a prog This is a purely practical exercise. Its purpose is to encourage students to get in the habit of using documentation E2.20 No public answer E2.21 No public answer E2. 22 No public answer E2.23 No public answer E2. 24 No publi c answe C 2002 TIMOTHY C. LETHBRIDGE AND ROBERT LAGANIEREPA5 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 E 2.11 p. 47 De sc ribing how poly m orphic im ple me ntations of c ertain shapeoperations would work. a )*translate: I n SimplePolygon (a s inher ite d by Rectangle and RegularPolygon) , and EllipticalShape (a s inher ite d by Circle) , themethod translate would movethe center. c )*getArea: I n Rectangle, the me thod getArea would r eturn height * width. I n RegularPolygon, it w ould compute the ar e a by dividing the polygon into numPoints individua l triangle s ( se e thea nsw er to E 2.30 f orthe de taile d a lgorithm). O ncethis a r eais c alc ulate d, the total a r eaof the RegularPolygon would be c alculated by multiplying the a re a of ea c h tr iangle by numPoints. I n Circle, theme thod getArea would r eturn pi * radius2 . E 2.12 No public answe r E 2.13 p. 47 Incorporating new c lasse s into an e xisting c lass hie rarchy that c ontains c onside rablepoly morphism. a )*IsoscelesTriangle: O ne might think of ma king this a subcla ss of ArbitraryPolygon, how e ve r tha t w ould be inappr opr ia te since you don’t w a nt it to inher it me thods such as addPoint and removePoint. A be tter solution is to ma ke it a subc lass of SimplePolygon. A s a ttributes you w ould have to storethe baseLength and height, or e lse you c ould store one of thetwo va lue s fora ngles a nd the le ngth of one of the sides, letting the othe r a ngle va lue a nd side be c omputed w hen ne eded. A s methods, you would ne ed changeScale, setBaseLength, setHeight, getArea, getPerimeterLength, getVertices, getBoundingRect, getBaseAngle, getTopAngle, a nd pe rhaps setBaseAngle and setTopAngle. E 2.14 No public answe r E 2.15 No public answe r E 2.16 No public answe r E 2.17 p. 49 De te rmining whe n dy nam icbinding is nee ded in a se t of poly m orphic me thods. T his e xe rc ise ha s tur ne d out to be par tic ular ly use ful to ensur e stude nts r e ally unde r stand the implic ations of polymorphism. Be fore assigning this exer c ise, it ha s pr ove d to be ne ce ssa ry to e xplain the ma te r ia l on pa ges 48 and 49 par tic ular ly ca r ef ully, w ith se ve ra l e xa mples. N ote tha t the exer c ise has the assumption, “ that the c ompilerknow s tha t no ne wc la sse s or me thods ca n be added to thehier a rc hy” ; it is w orth r eminding stude nts tha t this is not gene ra lly true in Java ( you c an a dd a subcla ss unle ss the c la ss is de clar e d final, a nd you c annot dec lar e anon- le afclass to be final) . a )*I nvoking getPerimeterLength on a Rectangle va ria ble: No dyna mic binding is ne e de d since Rectangle is a le af class a nd so the va r ia ble c ould only eve r conta in an instanc e of that class. Theloc a l method in Rectangle would a lw a ys bec alled. c )*I nvoking getBoundingRect on a Polygon va ria ble: Dyna mic binding w ould be ne ede d, sinc e e ithe r the me thod in Polygon or the one in Rectangle might have to be exe cute d depe nding on w hich class of objec t is pla ce d in thevar iableat r un time. E 2.18 p. 51 Re se arc hing products that c laim to beobje ct- orie nte d to de termine if theyre ally are . T hea nsw erto this question will va r y ove r time, de pe nding on w ha t produc ts ar e a va ila ble. E 2.19 p. 53 Using doc ume ntation to lookup library c lasse s and thus better unde rstand a program. T his is a pur ely pra c tica l e xe r cise . I ts purpose is to e nc our age stude nts to get in the ha bit of using doc ume ntation. E 2.20 No public answe r E 2.21 No public answe r E 2.22 No public answe r E 2.23 No public answe r E 2.24 No public answe r
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有