This chapter introduced you to a different way of programming by using the software object. You learned that software objects can combine functions and data (
Improve the Critter Caretaker program by allowing the
Write a program that simulates a television by creating it as an object. The user should be able to enter a channel number and raise or lower the volume. Make sure that the channel number and volume level stay within valid ranges.
Create a "back door" in the Critter Caretaker program that shows the exact values of the object's attributes. Accomplish this by printing the object when a secret selection, not listed in the menu, is entered as the user's choice. (Hint: add the special method __str__() to the Critter class.)
Create a Critter Farm program by instantiating several Critter objects and keeping track of them through a list. Mimic the Critter Caretaker program, but instead of requiring the user to care for a single critter, require them to care for an entire farm. Each menu choice should allow the user to perform some action for all of the critters (feed all of the critters, play with all of the critters, or listen to all of the critters). To make the program interesting, give each critter random starting hunger and boredom levels.
Download CD Content
In the last chapter, you learned about the software object. Almost every program you saw involved a single object. That's a great way to begin to understand how objects work, but the true power of OOP can only be appreciated by seeing a
Create objects of different classes in the same program
Allow objects to communicate with each other
Create more complex objects by combining simpler ones
Derive new classes from existing ones
Extend the definition of existing classes
Override method definitions of existing classes
The final project for this chapter is a simplified version of the card game, black-jack. The game works like this: Players are dealt cards with point values. Each player
The computer is the
Next, each player gets a chance to take additional cards. Each player can take one card at a time for as long as the player likes. But if the player's total goes over 21 (known as "busting"), the player loses. If all players bust, the computer reveals its first card and the round is over. Otherwise, play continues. The computer must take additional cards as long as its total is less than 17. If the computer busts, all players who have not
Figure 9.1: One player wins, the other is not so lucky.