Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Year 1 Computer Programming - Lecture 6

The document discusses object-oriented programming and design. It introduces classes, objects, and the Unified Modeling Language (UML) for specifying class diagrams. It also discusses defining classes in Python and provides an example of defining classes for a banking system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Year 1 Computer Programming - Lecture 6

The document discusses object-oriented programming and design. It introduces classes, objects, and the Unified Modeling Language (UML) for specifying class diagrams. It also discusses defining classes in Python and provides an example of defining classes for a banking system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CMP CMP4266

Computer Programming UG1


Lecture – 6

(Based on Gaddis, T., Starting out with Python 3e, Pearson Education)

CMP4266, School of CS & DT, Birmingham City University.


Next – The coding challenge

 You will be given 48 hours to complete coding tasks on


Moodle.
 This is based on the Labs 1,2,3,4,5 and 6, so to do well in the
coding challenge you need to complete all previous labs.
 It should be submitted within 48 hours, else, typical penalties
for late submission will be applied.
 After completing the challenge, you need to complete a
verification process with your personal tutor which includes:
– Making sure labs 1, 2, 3, 4, 5 and 6 have been submitted
– Explain your code to your personal tutor
 When the coding challenge is on, no tutor or a demonstrator will give
you any help or support in relation to the challenge or any relevant lab
exercises.

CMP4266, School of CS & DT, Birmingham City University.


Outline

 Procedural and Object-Oriented Programming

 Classes

 Designing Classes in UML

 Class Definition & Class Instance

CMP4266, School of CS & DT, 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

CMP4266, School of CS & DT, 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 (procedures): to manipulate the data of the tangible


thing.

CMP4266, School of CS & DT, 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 = customer@info.uk, address, city, Gender,


Credit card number, bank details, shopping basket, purchased items, ……etc.”
Student = [first name, last name, modules, degree, address, bank details, etc.]

CMP4266, School of CS & DT, 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

CMP4266, School of CS & DT, 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.

CMP4266, School of CS & DT, 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


procedures that manipulate that
data.

 Data in an object are known as


attributes.

 Procedures in an object are known


as methods. Methods
(behaviors / procedures)

CMP4266, School of CS & DT, 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)

CMP4266, School of CS & DT, 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

 Public methods: allow external code to manipulate the object


– Example: set_time, set_alarm_time

 Private methods: used for object’s inner workings

def clock_inner_operation():
for hours in range(24):
for minutes in range(60):
for seconds in range(60):
print(hours, ':', minutes, ':', seconds)

CMP4266, School of CS & DT, Birmingham City University.


Classes

 Class: code that specifies the data attributes and methods of a


particular type of object
– Similar to a blueprint 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

CMP4266, School of CS & DT, 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

CMP4266, School of CS & DT, 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

 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)
– Simple relation (known as association) between two classes is represented by solid lines

CMP4266, School of CS & DT, 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:
A customer uses a bank ATM to check balance
- Get written description of the problem domain
of his/her bank account,
- Identify all nouns in the description, each of which is a potential class
deposit funds,
- Refine the list to withdraw
include only cash
classesand/or
that are transfer
relevant to funds
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 

CMP4266, School of CS & DT, 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

CMP4266, School of CS & DT, 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 Class_name:
- Class names often start with uppercase 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
 Initializer method: automatically executed when an instance of the class is
created
– Initializes object’s data attributes and assigns self parameter to the
object that was just created
– Format: def __init__ (self):
 Class methods can have multiple parameters in addition to self

CMP4266, School of CS & DT, Birmingham City University.


Class Definitions (Example)

CMP4266, School of CS & DT, 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 initializer method


– Format: My_instance = Class_Name()

 To call any of the class methods using the created instance,


use dot notation
– Format: My_instance.method()
– Because the self parameter references the specific instance of the
object, the method will affect this instance
- Reference to self is passed automatically

CMP4266, School of CS & DT, Birmingham City University.


Class Instance (Example)

CMP4266, School of CS & DT, Birmingham City University.


Class Definitions

 Object’s state: the values of the object’s attribute at a given


moment
 __str__ method: displays the object’s state
– Automatically called when the object is passed as an argument to the
print function
– Automatically called when the object is passed as an argument to the
str function

 Instance attribute: belongs to a specific instance of a class


– Created when a method uses the self parameter to create an attribute

 If many instances of a class are created, each would have its


own set of attributes

CMP4266, School of CS & DT, Birmingham City University.


Accessor and Mutator Methods

 An object’s data attributes should be private


– To make sure of this, place two underscores (__) in front of attribute
name
- Example: __current_minute

 Typically, all of a class’s data attributes are private and provide


methods to access and change them

 Accessor methods: return a value from a class’s attribute


without changing it
– Safe way for code outside the class to retrieve the value of attributes

 Mutator methods: store or change the value of a data attribute


CMP4266, School of CS & DT, Birmingham City University.
Class Example

CMP4266, School of CS & DT, Birmingham City University.


Implementing Association (1)

 For a banking system, we have


– BankAccount and Customer objects
– Each customer should be connected to a bank account

 How to implement this?


• Association is generally implemented by making one object as attribute of the
other object.

CMP4266, School of CS & DT, Birmingham City University.


Implementing Association (2)

CMP4266, School of CS & DT, Birmingham City University.


Implementing Association (3)

CMP4266, School of CS & DT, Birmingham City University.


Implementing Association (4)

CMP4266, School of CS & DT, Birmingham City University.


6.1 Exercise: Identify the class, its attributes and
methods from a scenario (15 - 20 minutes)
In-class
Exercise
 Create a software that can calculate energy consumption
and running cost of home appliances.
 To create an object, you need to pass 3 arguments through
its __init__ method, and to be stored as the class attributes,
and these are appliance name, power consumption in
watts, the number of hours the device is being used per
day.
 The class should have the following methods;
 print appliance details
 Calculate energy consumption per month in kWh;
 Calclate monthly running cost.
– Note, you need to provide this method with electricity rate per kWh
to calculate the running cost. E.g. £0.10 per kWh

CMP4266 , School of CS & DT, Birmingham City University.


Exercise 6.1: Identify the class, its attributes and
methods from a scenario (continued)
In-class
Notes: Exercise

• To calculate the energy consumption of the appliance per month


in kWh use the formula ((Watts * hours)/1000)*30

• To calculate the running cost per month ( in kWh) you should use
the formula (electricity rate * energy consumption).
Example:
A refrigerator consumes 300 watts and is used for 24
hours per day, and the energy cost is £0.10 per kWh. To
calculate its running cost per month you need to do the
following:
Monthly consumption (kWh) = ((300*24)/1000) * 30
Monthly cost (£)= Monthly consumption (kWh) * £0.10
From above the refrigerator cost £21.60 per month.
CMP4266 , School of CS & DT, Birmingham City University.

You might also like