12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The order simulation works as follows:a. The Lunch class’ constructor should make and embed an instance ofCustomer and an instance of Employee, and should export a method calledorder. When called, this order method should ask the Customer to place anorder by calling its placeOrder method. The Customer’s placeOrder methodshould in turn ask the Employee object for a new Food object by callingEmployee’s takeOrder method.b. Food objects should store a food name string (e.g., “burritos”), passed downfrom Lunch.order, to Customer.placeOrder, to Employee.takeOrder, andfinally, to Food’s constructor. The top-level Lunch class should also export amethod called result, which asks the customer to print the name of thefood it received from the Employee via the order (this can be used to test yoursimulation).Note that Lunch needs to pass either the Employee or itself to the Customer toallow the Customer to call Employee methods.Experiment with your classes interactively by importing the Lunch class, callingits order method to run an interaction, and then calling its result method toverify that the Customer got what he or she ordered. If you prefer, you can alsosimply code test cases as self-test code in the file where your classes are defined,using the module _ _name_ _ trick in Chapter 21. In this simulation, the Customeris the active agent; how would your classes change if Employee were the objectthat initiated customer/employee interaction instead?8. Zoo animal hierarchy. Consider the class tree shown in Figure 26-1. Code a setof six class statements to model this taxonomy with <strong>Python</strong> inheritance. Then,add a speak method to each of your classes that prints a unique message, and areply method in your top-level Animal superclass that simply calls self.speak toinvoke the category-specific message printer in a subclass below (this will kickoff an independent inheritance search from self). Finally, remove the speakmethod from your Hacker class so that it picks up the default above it. Whenyou’re finished, your classes should work this way:% python>>> from zoo import Cat, Hacker>>> spot = Cat( )>>> spot.reply( ) # Animal.reply; calls Cat.speakmeow>>> data = Hacker( ) # Animal.reply; calls Primate.speak>>> data.reply( )Hello world!Part VI Exercises | 569

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!