Exercise 3-15. You have been approached by an android manufacturer to develop the control system for a robotic servant. Describe a party in object-oriented terms. Use abstractions such as Food so that you can encompass the entire range of data and behavior between drawing up the invitation list to cleaning up the house afterward.
The house is on 228 Maple Rd.
We make a class House in a file House.cs, which we include in package party.house. We add the field address (of type string) to class House.
The house holds robot Robbie, 2 hosts: John, Patricia, and 3 rooms: Kitchen, Dining room, and Bedroom.
We add class Robot in a file Robot.cs to package party.house.occupants. We also add Host.cs and Guest.cs to the same package. We add the field robot (of type Robot) to class House, as well as a list of hosts: List<Host> hosts;
For this, we have to include the classes from package party.house.occupants in House.cs:
using party.house.occupants; // for Robot, Host
We add the classes for rooms BedRoom.cs, Diner.cs, and Kitchen.cs in the package party.house.rooms. These classes are derived from the base class Room.cs, which we include in the same package.
We add a list of rooms to the house in House.cs: List<Room> rooms;
For this, we have to include the classes from package party.house.rooms in House.cs:
using party.house.rooms; // for Room
We consider adding types of food to package party.foods. We add the classes Cake.cs (derived from the base class in Food.cs) and Soda.cs (derived from Drink.cs) to the package.
The file with the Main() method is Party.cs, in package party:
using party.house; // for House
using party.house.rooms; // for Room, Kitchen, Diner, BedRoom
using party.house.occupants; // for Robot, Host, Guest
using party.foods; // for Food, Drink, Cake, Soda
namespace party
{
public class Party
{
public static void Main() {}
}
}
The main program logic will be in class Party, inside its Main() method.
First, we initialize the robot and the house:
Robot r = new Robot("Robbie");
House house = new House("228 Maple Rd.", r);
Then we initialize the rooms and guests and add them to the house:
.........................................................................................................
Diner diner = new Diner("Dining room");
house.AddRoom(diner);
Host h1 = new Host("John");
Host h2 = new Host("Patricia");
house.AddHost(h1);
house.AddHost(h2);
The robot will be in charge of inviting the guests, catering, setting the table, and eventually cleaning up.
We add a list of guests to the Robot class:
List<Guest> guests;
Then, in class Party, in Main(), we add the invitations (r is the robot):
Guest g1 = new Guest("Martin");
Guest g2 = new Guest("Linda");
r.AddGuest(g1);
r.AddGuest(g2);
The robot Robbie makes the bed, cleans the rooms, caters for the party (not implemented), sets the table, invites the guests (Martin and Linda), and starts the party.
We add the package furniture to party.house.rooms. Package party.house.rooms.furniture will contain Bed.cs, tables (DiningTable.cs, KitchenTable.cs, derived from the base class in Table.cs), and chairs (ArmChair.cs, DiningChair.cs, KitchenChair.cs, derived from Chair.cs).
The hosts and guests enjoy the party. They drink, eat, chat, dance, listen to music (not implemented). Then the guests depart to their homes. Robbie says goodbye to the guests and cleans the table and the kitchen.
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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.
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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).