Exercise 3-16 (Application Party, Food types, inheritance)

Chapter_3     Exercise_3-15 Exercise_3-17







Exercise 3-16     TCS, p. 86


Exercise 3-16. Take the Food abstraction from Exercise_3-15 and describe it more fully in terms of classes and types. Use inheritance in at least two places. Constrain your model to the data and behaviors appropriate to the robotic butler.




Class Soda is derived from the base class Drink, while Cake is derived from Food. The base classes are abstract as it makes no sense to make generic objects of type Food or Drink. Both base classes have a name field, which is inherited in the derived classes Cake and Soda. Both derived classes override the method ToString(), printing appropriate messages. In Party.cs, we make a Soda object named "Coca Cola" and a Cake object named "Fruit cake". Then we print these objects, which calls their corresponding method ToString():
Drink coke = new Soda("Coca Cola");
Food cake = new Cake("Fruit cake");
System.Console.WriteLine(coke); // Coca Cola is refreshing
System.Console.WriteLine(cake); // Fruit cake is delicious

The robot Robbie can set the table, using knives, forks, glasses, napkins, etc. (not implemented). You can also consider adding more beverages and foods, as well as appropriate actions (behaviors) for both the robot and hosts / guests. See also Exercise_3-17.









Chapter_3     Exercise_3-15 BACK_TO_TOP Exercise_3-17



Comments

Popular posts from this blog

Contents