Modelling The World With Objects: Code Structure A Review
Modelling The World With Objects: Code Structure A Review
COMMONWEALTH OF AUSTRALIA
MODELLING THE Copyright Regulation 1969
WARNING
WORLD WITH OBJECTS This material has been copied and communicated to
you by or on behalf of Curtin University of
Fundamentals of Programming - COMP1005 Technology pursuant to Part VB of the Copyright
Act 1968 (the Act)
The material in this communication may be subject
Department of Computing to copyright under the Act. Any further copying or
communication of this material by you may be the
Curtin University subject of copyright protection under the Act.
Updated 7/4/2020 Do not remove this notice
Learning Outcomes
• Understand the main concepts in object-
oriented programming and their value
• Read and explain object-oriented code
• Apply and create simple object-oriented CODE STRUCTURE
Python code A REVIEW
Fundamentals of Programming
Lecture 6
3 4
Code Structure
• Across the semester we've learnt many
elements of coding:
• Control structures: if/else/elif, for loops, while loops
• Creating and using functions
• Creating and using modules
• Python style: PEP-8, "readability counts"
• Data types: int, float, string, list, array, set, dictionary
• Files: text, csv
• Key packages: numpy, scipy, matplotlib, pandas, random
• Environments: python scripts, command lines, bash scripts, jupyter
notebooks
5 Fundamentals_Lecture10 6
Fundamentals_Lecture10 7 Fundamentals_Lecture10 8
Fundamentals_Lecture10 9 Fundamentals_Lecture10 10
17 Fundamentals_Lecture10
http://s460.photobucket.com/user/stefer24/media/scan0024.jpg.html 18
the processing steps • The required sub modules are designed (i.e.
Constructors, accessors, etc)
• Some of these steps get refined into sub
• Each Class is thoroughly tested via a test harness
modules and the process repeats until the
design is refined enough to code • Finally, the main algorithm and any required
sub modules is designed (making use of the
• Under Object Orientation this all changes… developed classes in the process)
21 22
BankAccount: bank
27 28
Example: bank account Example: bank account (v2)
class BankAccount ():
class BankAccount (): interest_rate = 0.03
def __init__(self, name, number, balance):
interest_rate = 0.03 self.name = name
def __init__(self, name, number, balance): self.number = number
self.name = name self.balance = balance
self.number = number
self.balance = balance accounts = []
bank = BankAccount('Everyday', '007', 2000)
Class variable accounts.append(bank)
bank = BankAccount('Cheque A/C', '008', 3000)
BankAccount: bank accounts.append(bank)
bank = BankAccount('Term Deposit', '009', 20000)
accounts.append(bank)
interest_rate: 0.03
name: 'Everyday' Instance total = 0
number: '007' variables for i in range(len(accounts)):
balance: 2000 print("Name: ", accounts[i].name, "\tNumber: ", accounts[i].number,
"\tBalance: ", accounts[i].balance)
total = total + accounts[i].balance
print("\t\t\t\t\tTotal: ", total)
29 30
35 36
animals.py - Dog Class (v2) animals.py - Cat Class
class Dog(): class Cat():
def __init__(self, name, dob, colour, breed): def __init__(self, name, dob, colour, breed):
self.name = name self.name = name
self.dob = dob self.dob = dob
self.colour = colour self.colour = colour
self.breed = breed self.breed = breed
37 Fundamentals_Lecture10 38
def __init__(self, name, dob, colour, breed): dude = Dog('Dude', '1/1/2011', 'Brown', 'Jack Russell')
self.name = name oogs = Cat('Oogie', '1/1/2006', 'Grey', 'Fluffy')
self.dob = dob bbird = Bird('Big Bird', '10/11/1969', 'Yellow', 'Canary')
self.colour = colour
self.breed = breed dude.printit()
oogs.printit()
def printit(self): bbird.printit()
print('Name: ', self.name)
print('DOB: ', self.dob)
print('Colour: ', self.colour)
print('Breed: ', self.breed)
print('Class: ', self.myclass)
39 40
Pets.py Name: Dude Summary
DOB: 1/1/2011
from animals import Dog Colour: Brown
from animals import Cat Breed: Jack Russell • Understand the main concepts in object-
Class: Dog
from animals import Bird
oriented programming and their value
dude = Dog('Dude', '1/1/2011', 'Brown', Name:
'Jack Oogie
• Read and explain object-oriented code
Russell')
oogs = Cat('Oogie', '1/1/2006', 'Grey', DOB: 1/1/2006
'Fluffy')
Colour: Grey'Canary')
bbird = Bird('Big Bird', '10/11/1969', 'Yellow',
Breed: Fluffy • Apply and create simple object-oriented
Class: Cat
dude.printit()
oogs.printit() Python code
bbird.printit() Name: Big Bird
DOB: 10/11/1969
Colour: Yellow
Breed: Canary
Class: Bird
If you try to print dude directly:
animals.Dog object at 0x10108d978
41 42
43 44
Mid-semester Test Why did you show the previous slide?
• 60 minutes + 5 minutes reading time • The mid-semester test was important
• Covers weeks 1-6 of lectures, weeks 1-5 of • Motivation to revise the course so far
• Feedback on how well you understand the unit
practicals • Invigilated – so we know it's your work
• i.e. just simple questions from OO
• It was dropped because we were forced to reduce
• Questions based on lectures, practicals, practical
the number of assessments
tests, revision questions and review questions • The practical tests and assignment were
• Examples are available on Blackboard considered more important
• The past papers are an excellent resource for you
• Hopefully will have a ComSSA revision TBA to review your knowledge
• Make use of Piazza and practical sessions to get
feedback
45 46
Next week…
• Lecture 7:
• Relationships in Object-orientation
• Exception handling
47