Exercise 3-15 (Application Party, robotic servant)

Chapter_3     Exercise_3-14 Exercise_3-16







Exercise 3-15     TCS, p. 86


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 to 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 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 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.









Chapter_3     Exercise_3-14 BACK_TO_TOP Exercise_3-16



Comments

Popular posts from this blog

Contents