OPS435 Python Lab 7

From CDOT Wiki
Revision as of 11:12, 3 December 2017 by Andrew (talk | contribs) (INVESTIGATION 1: Classes and objects)
Jump to: navigation, search

LAB OBJECTIVES

Object-oriented programming is one level of complexity higher than simply structured programming as you've experienced in languages such as Bash or C. In this second lab on objects we're going to look at slightly more complex issues that come up when using them.

PYTHON REFERENCE

In previous labs, you have been advised to make notes and use online references. This also relates to learning about objected oriented programming to help becoming "overwhelmed" with the volume of information in this lab.
Below is a table with links to useful online Python reference sites (by category). You may find these references useful when performing assignments and labs.
Category Resource Link
Using Classes
Classes

INVESTIGATION 1: Classes and objects

In the last lab we created a class named Student and a couple of objects of type Student which were named student1 and student2. Let's spend some more time on that distinction.

A class is a type, a description of a thing, the definition of what it should look like. An object is an instance of a class, an individual described by a class, a specific thing with properties defined by a class. Here are some examples of what might be types and what might be objects of those types:

Class Objects of that class
IPAddress 192.168.1.3, 192.168.1.10, 10.0.0.50, 142.204.1.2
Person Andrew Oatley-Willis, Murray Saul, Andrew Smith
BMWModel BMW2Series, BMW3Series, BMW4Series, BMWXSeries, BMWMSeries, BMWiSeries
Car Andrew's silver Civic with VIN 1234567890, John's blue Subaru with VIN 0987654321, Evan's BWMMSeries with VIN 4567981230

The exact definition of the type and what you would expect to store in objects of that type is up to you - the programmer. You want to design your classes so that you can manage data in your application as easily as possible.

Notes about the mechanics of implementing classes:

  • Inside a program typically a class name starts with a capital letter and object names, as typical variable names, start with a lowercase letter.
  • Just as you can define a function in a separate file from where you use it - you can define a class in a separate file. In fact it's even more common with classes since you're more likely to need to use them in multiple places.

For this investigation create the classes and objects from the table above. No test script is provided for this section because the design of the classes is left up to you. Store the data that you think might be relevant for that type. Implement at least two methods in each class (the constructor plus one other).

As you try to design the classes you will quickly realise that there is a potentially infinite number of properties (components) and methods that any class can have. What you choose to include in your class definition should be guided by what you intend to do with it. For example if the tire tread depth in the cars is important in your application - you should include it in your Car class. But if you don't care about when the last oil change was or you don't have the means of obtaining that information - there's no point in adding that to the description of the Car class.

LAB 7 SIGN-OFF (SHOW INSTRUCTOR)

Have Ready to Show Your Instructor:
Output of: ./CheckLab7.py -f -v
Output of: cat lab7a.py

LAB REVIEW