Exercise 3-17 (Application Party, Celebrate)

Chapter_3     Exercise_3-16 AppParty







Exercise 3-17     TCS, p. 86


Exercise 3-17. Choose one of the classes developed in Exercise_3-16 that requires some complex behavior (perhaps an item that needs baking or the purchase of exotic ingredients). List the classes that would be required to collaborate to accomplish the complex behavior. For instance, if the behavior was lighting candles on a cake, the classes might include Candle, Cake, and Match.




We add the method Celebrate() to Cake.cs. We want to light a candle using a match, so we add the appropriate packages and classes. We add subpackages ornaments and tools to package party.house.rooms. In package ornaments we add the classes Candle.cs, derived from Ornament.cs, while in package tools we add the classes Match.cs, derived from Tool.cs. We override the method ToString() in both Match and Candle, to print appropriate messages: "Match is practical", "Candle is romantic". We add the method Light() to both Match and Candle, and call these methods from method Celebrate() in Cake.cs.

We could also add a method Toast() to Soda.cs or to a new class Wine that we could add to package party.foods (Wine would be derived from Drink).




Note:  For a partial implementation of the application, see Section AppParty.









Chapter_3     Exercise_3-16 BACK_TO_TOP AppParty



Comments

Popular posts from this blog

Contents