CMP 6221
Computing for AI
Lecture – 8
(Based on Gaddis, T., Starting out with Python, Pearson Education)
CMP6221, School of CDT, Birmingham City University.
Object-Oriented Programming
CMP6221, School of CDT, Birmingham City University.
Procedural Programming
Procedural programming: writing programs made of functions
that perform specific tasks
– Procedures typically operate on data items that are separate from the
procedures
– Data items commonly passed from one procedure to another
– Focus: to create procedures that operate on the program’s data
CMP6221, School of CDT, Birmingham City University.
Object-Oriented Programming
Object-oriented programming: focused on creating objects
Object:
– In reality: a tangible thing that we may touch, feel and manipulate
– In software development: a model of a tangible thing that contains
- data: to represent different characteristics of the tangible thing
- associate behaviors (methods): to manipulate the data of the tangible thing.
CMP6221, School of CDT, Birmingham City University.
Object-Oriented Programming? Why?
Data Types in Python :
– Float, String, Integer
Good when used for procedural operation e.g.
– Value = 3.2 * 5
But how can a variable hold all data needed to represent a Customer,
Student or Bank Account.
– Using variables ?? E.g.
Customer = “email address = [email protected], address, city, Gender,
Credit card number, bank details, shopping basket, purchased items, ……etc.”
Student = [first name, last name, modules, degree, address, bank details, etc.]
CMP6221, School of CDT, Birmingham City University.
Procedural vs Object-Oriented Programming
Find the area and perimeter of a thin rectangular plate
Passing data when
procedures are called
No need to pass data when
procedures are called on objects
CMP6221, School of CDT, Birmingham City University.
Object Oriented Programming – No restriction
In OOP, everything is object.
Objects are anything that you can give a noun too.
– Eg. TV, Lamp and glass of water.
In reality objects are designed with the clear purpose, but
they are not limited in the way they can interact with one
another.
For example,
– The lamp is used to provide sufficient light for reading a
book
– Use the glass of water to drink
– Dump the water on the book
Interactions between the world’s objects are limited with
our imagination rather than the object limitations.
CMP6221, School of CDT, Birmingham City University.
Object Oriented Programming
Object-oriented programming is
centered on creating objects rather Object
than procedures.
Attributes (data)
Objects are a melding of data and
functions that manipulate that
data.
Data in an object are known as
attributes.
Functions in an object are known
as methods. Methods
(behaviors / procedures)
CMP6221, School of CDT, Birmingham City University.
Object Oriented Programming
Object-oriented programming
combines data and behavior
via encapsulation. Object
Data hiding is the ability of an Programming Attributes (data)
Interface typically private to this object
object to hide data from other
objects in the program.
Only an objects methods
should be able to directly
manipulate its attributes.
Other objects are allowed
manipulate an object’s Other
attributes via the object’s objects
methods.
This indirect access is known
as a programming interface.
Methods
(behaviors / procedures)
CMP6221, School of CDT, Birmingham City University.
Object-Oriented Programming
Data attributes: define the state of an object
– Example: clock object would have second, minute, and hour data
attributes
– Attributes are normally designed as private.
- In Python, prefix the attribute name with " " when implementing
the code: second, minute, hours
Public methods: allow external code to manipulate the object
– Example: set_time, set_alarm_time
Private methods: used for object’s inner operations
– Example: clock_internal_operation
– Same as private attribute, private function name starts with " "
CMP6221, School of CDT, Birmingham City University.
Classes
Class: code that specifies the data attributes and methods of a
particular type of object
– Similar to a blueprint/template of a house or a cookie cutter
Instance: an object created according to a class definition
– Similar to a specific house built according to the blueprint or a specific
cookie
– There can be many instances of one class
CMP6221, School of CDT, Birmingham City University.
OOP: Class Design
CMP6221, School of CDT, Birmingham City University.
Object Oriented Design and Programming
Object Oriented Design
– Is a systematic process to think about how software systems are
composed of interacting objects
- identify the objects
- Identify attributes and methods of each object
- How the objects are connected to each other
– Output of object oriented design is an implementation specification
Object Oriented Programming
– Is the process of converting the implementation specification into a working
program
CMP6221, School of CDT, Birmingham City University.
Object Oriented Design Specification
Unified Modelling Language (UML) is a very popular language that is used
to specify object oriented design.
– It offers different types of diagrams to specify OOD.
– Classes (i.e. objects) of a software system and the relationships among the classes can
be described using UML class diagram
Q: What are the two diagrams have we learned in week 1 &2?
In UML classes are represented as a box with three sections
– Top section displays the name of the class
– Middle section lists the data attributes
– Bottom section lists the methods of the class
– A minus (-) sign in front of an attribute (or method) signifies private attribute (or method)
– A plus (+) sign in front of an attribute (or method) signifies a public attribute (or method)
– Note. In UML diagram, no " " prefix should be used for private members.
CMP6221, School of CDT, Birmingham City University.
Object Oriented Design Specification
When developing object oriented program, first goal is to identify
classes
– Typically involves identifying the real-world objects that are in the
problem
– Technique for identifying classes:
- Get written description of the problem domain
- Identify all nouns in the description, each of which is a potential class
- Refine the list to include only classes that are relevant to the problem
To find out a class’s responsibilities look at the problem domain. A
classes responsibilities are:
– The things the class is responsible for knowing
- Identifying these helps identify the class’s data attributes
– The actions the class is responsible for doing
- Identifying these helps identify the class’s methods
CMP6221, School of CDT, Birmingham City University.
Object Oriented Design Specification
Acustomer uses a bank ATM to check balance
of his/her bank account,
deposit funds, withdraw cash and/or transfer funds
CMP6221, School of CDT, Birmingham City University.
Object Oriented Design Specification (Example)
Consider a Banking System.
– The most obvious objects would be i) Bank Account and ii) Customer
– Each customer should be connected to a bank account
– Identify attributes and methods for each object
– Use "void" to indicate the method has no return type.
CMP6221, School of CDT, Birmingham City University.
Class Definitions in Python
Class definition: set of statements that define a class’s methods and data
attributes
– Format: begin with class ClassName:
- Convention: Start each word with a capital letter.
– Method definition like any other python function definition
- self parameter: required in every method in the class
- references the specific object that the method is working on
- Convention: Use a lowercase and separate words with underscores
CMP6221, School of CDT, Birmingham City University.
Class Definitions in Python
Initialise method: automatically executed when an instance of the class is
created
– Format: def init (self):
- self parameter is required for every method
– Attributes are declared and initialised within the init method
– Note. Private attributes are prefixed with " "
Class methods can have multiple parameters in addition to self
– Since both attributes are essential to a bank account, a common practice is to pass in
their values via the initialise method:
CMP6221, School of CDT, Birmingham City University.
Class definition in Python
Check the implementation of the
below class diagram:
– The initialise method has been
added into the UML.
– It's known as constructor and
represented by the class name.
– In Python, it's implemented via
method init .
Let's save this class into L7_library.py
CMP6221, School of CDT, Birmingham City University.
Class Instance
Classes can be stored in modules
– Filename for module must end in .py
– Module can be imported to programs that use the class
To create a new instance of a class call the initialiser method
– Format: My_instance = Class_Name()
To call any of the class methods using the created instance,
use dot notation
– Format: My_instance.method()
- Reference to self is passed automatically – different to a normal
function call
CMP6221, School of CDT, Birmingham City University.
Class Instance (Example)
CMP6221, School of CDT, Birmingham City University.
Class Instance (Example 2)
We can also import the class like this:
CMP6221, School of CDT, Birmingham City University.
8.1 Exercise: Re-implement the calculator in a class
(15 minutes)
In-class
The original task in Lab 4: Exercise
CMP6221, School of CDT, Birmingham City University.
Exercise 8.1: Identify the class, its attributes and
methods
In-class
Our original solution: Exercise
– call: run_calculator() to start the application
Attributes
• None
Methods
• public
• add, subtract, multiply,
divide
• run_calculator
• display_menu
• get_input
CMP6221, School of CDT, Birmingham City University.
Exercise 8.2: Create a customer class
for the bank system In-class
Exercise
A customer uses a bank ATM to check balance
of his/her bank account,
deposit funds, withdraw cash and/or transfer funds.
What attributes and methods?
– For init (), it should ask for both name and address and initialise the
private parameters.
– Let's save this class into L8_library.py
CMP6221, School of CDT, Birmingham City University.
Essential information
OOP design
– Object, attributes, methods
Class diagram
– Annotation of attributes, methods, and datatypes
– Annotation of public and private
How to create a class
– init method
– self parameter
How to create an instance
– Import class from another module
CMP6221, School of CDT, Birmingham City University.
Thank you!
CMP6221, School of CDT, Birmingham City University.