< Back

Lecture 10- Inheritance, Day 2


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab10 
$ cd ~/cs170/labs/lab10

When to use Inheritance

On Friday, we saw how to perform inheritance. We focused on the syntax of creating classes that inherit from one another. Today we are going to focus on the when of inheritance. Namely, why on earth would you choose to use such a strange concept in a language such as python.


Lab Activity
College Directory

As you are probably aware, the college keeps a directory of everyone affiliated with the college accessible for anyone to search. It is located on the main page of roanoke.edu. Everyone has certain information, but there is more information stored for certain classifications of people, such as Faculty or Staff. One mechanism to make storage of this information is to create separate classes for each of the classifications of people, but inheritance will eliminate a lot of duplicated code.

Details

create 3 different files for our 3 different classifications of people:  People , Student, and Faculty. We aren't going to worry too much about actions that these classes can take, but you will want a __str__ method for each one of them. Both Student and Faculty will inherit the People class. You simply need to work out what information is common between Students and Faculty; Any shared information should go into their shared parent class, People.

For the sake of this program, you should store the following information (but only for the appropriate people):

Once you have finished with the class definitions, test them by creating several Students and Faculty. Make sure you can access the appropriate attributes of each object, and that you are not storing any unnecessary attributes for each class.

Example

scotty = Faculty("Scotty Smith", "1/1/1900", "chssmith@roanoke.edu", "Trexler 365-B", "MCSP Department\n221 College Lane\nSalem, Va 24153")
scotty_student = Student("Scotty Smith", "1/1/1900", "cssmith@mail.roanoke.edu", "Crawford 218", "Box 1952")
print(scotty)
#Prints:
#Scotty Smith
#DOB - 1/1/1900
#Email - chssmith@roanoke.edu
#Office - Trexler 365-B
#Mail - MCSP Department
#221 College Lane
#Salem, Va 24153

print(scotty_student)
#Prints:
#Scotty Smith
#DOB - 1/1/1900
#Email - cssmith@mail.roanoke.edu
#Dorm - Crawford 218
#Mail - Box 1952

 

Challenge

For gradebook, we used a Dictionary to store the list of students. However, we aren't always going to be able to use Dictionaries to solve our problems. In fact, using a Dictionary for something like the directory search would be a bit of a waste of memory.

Instead, we could simply store all of the people you have created into a list. Then, whenever someone is looking for information for a particular person we just have to find they entry in that list.

Modify your program so you can interactively search and print information about people. You should use a list this time, instead of a dictionary.


Lab Assignment 10
Monopoly

I'm sure most of you have played at least one game of Monopoly in your life. The game consists of a set of 40 squares containing 28 properties, 6 Action squares, 2 Tax Squares, "Go", "Jail", "Free Parking", and "Go To Jail". You play the game by moving through the set of squares based off of the roll of the dice. You perform a different action based on which square you land on. For example, if you land on a property, you either have to pay "rent", or you can purchase the property. However, if you land on an "Action" square, you have to draw a card from a certain pile. "Tax" squares require the player to pay some fee to the Game's banker.

Relax, I wouldn't be evil enough to force you to create the whole game of Monopoly in lab. However, this is a great example of Inheritance, since ALL of the squares need some information, but you need to change the behavior of certain squares. We will work on a relaxed scenario for this.

Details

I am not going to force you to start on paper for today. However, I would strongly suggest you at least think about designing on paper first, then start your coding.

You are required to create 4 classes for the Monopoly program: MonopolySquare, PropertySquare, ActionSquare, and TaxSquare. Your other three squares will inherit the MonopolySquare class, which should just have a single attribute: The name of the square. It should also have a method land_on(), which does nothing by default. The derived classes should print the action the player should take.

Once you have written all four classes, you should create a simple program that creates a list of at least 12 squares (Make sure there is at least one of each type), "rolls the dice" by selecting a random square from the list, and prints a message indicating which square the player landed on and the action they should take.

Hint

  • You can actually get away (for now) without any additional attributes of the child classes. We are simply changing a behavior, not the description. So you don't need to define a constructor, just inherit one from the parent class.

  • Because all of the child classes inherit the Monopoly square, you are guaranteed that each one will have a land_on method. This method should be overridden in each of the child classes, printing a message indicating the action the player should take..

  • Make sure you wrap your head around what is going on here. You now have 4 new data types, but they are now all related in a family tree like structure.

 

Challenge

You now can create Monopoly squares. Why not make a class that represents an actual Monopoly game? This class has a different relationship to the Monopoly squares. You won't inherit them, but you will create 40 of them to represent a game board.

You should also modify the other classes that you wrote previously to interact with your game board appropriately. For example, instead of instructing the user on what to do, actually perform said action. Your "Action" squares can immediately draw an action card, "Tax" squares can immediately deduct money from the appropriate player, etc.

You may need to write some additional classes for this as well. Think about the relationship between your classes, to determine when and where inheritance should be used, as opposed to storing information in attributes..


Submission

When you have finished, create a tar file of your lab10 directory. To create a tar file, execute the following commands:

cd ~/cs170/labs
tar czvf lab10.tgz lab10/

To submit your activity, go to inquire.roanoke.edu. You should see an available assignment called Lab Assignment 10. Make sure you include a header listing the authors of the file.


In-Class Notes