A Level Computer Science Topic 6: Introducing OOP

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Python Objects and Classes
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
Lilian Blot TOWARDS MORE ADVANCED CONCEPTS & OBJECT ORIENTED PROGRAMMING Building Data Structure Autumn 2014 TPOP 1.
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
Object-Oriented PHP (1)
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 :)Advanced:) Visual Basic laura leventhal and julie barnes.
ASP.NET Programming with C# and SQL Server First Edition
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
Module 2: User Data types #1 2000/01Scientific Computing in OOCourse code 3C59 Module 2: User defined Data Types & Operations upon them In this module.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
Data Abstraction UW CSE 160 Spring What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
Python programming - Defining Classes
Chapter 7 Object-Oriented Programming
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Concepts of Object Oriented Programming
COMPSCI 107 Computer Science Fundamentals
Chapter 8: More About OOP and GUIs
Classes and OOP.
Objects as a programming concept
Department of Computer Science,
Copyright (c) 2017 by Dr. E. Horvath
CSC 222: Object-Oriented Programming
Lecture VI Objects The OOP Concept Defining Classes Methods
Object Oriented Programming
To Get Started Paper sheet
Data Abstraction UW CSE 160 Winter 2017.
Teach A-Level Computer Science: Object-Oriented Programming in Python
Introduction to Python
Learning Objectives Classes Constructors Principles of OOP
Data Abstraction UW CSE 160 Spring 2018.
Object Oriented Programming in Python
COP 3330 Object-oriented Programming in C++
Inheritance and Polymorphism
Object Oriented Programming in A Level
Object-Oriented PHP (1)
Lecture 18 Python OOP.
Object-Oriented Design AND CLASS PROPERTIES
Classes and Objects Systems Programming.
Data Abstraction UW CSE 160.
Introduction to Classes and Objects
Presentation transcript:

A Level Computer Science Topic 6: Introducing OOP Teaching London Computing A Level Computer Science Topic 6: Introducing OOP William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

Aims What and why OOP? OOP concepts Using classes in Python The problem of software development OOP concepts Data hiding Class and instances Using classes in Python Using classes – example of files ‘Methods’ versus functions Create a new class in Python

What is OOP and Why?

What is OOP? Object-oriented programming IS An idea for organising programs Object-oriented programming IS NOT A completely different type of programming Builds on if, while, functions etc Necessary: remember it’s all assembly code eventually At first, OOP is more complex

Why Organise S/W? Hard to organise large problem Work must be shared across a team Imagine building a house with no plan? Advantages claimed for OOP organisation Better reuse of code in libraries Software easier to change OOP very popular for Graphical User Interface (GUI) libraries

Software Organisation So Far Break overall program into functions Discussion: is it obvious what functions to choose? Aside: more complex organisation possible Function def Function def Function def Main program Initialise variable Call functions

Exercise 1.1 (and 1.2) Use google to find an example of a failed software project in the UK How late? How much money wasted?

OO Concepts

Data Hiding – Abstraction Different ways to represent complex data Example: shopping list List of pairs: (item string, amount integer) Dictionary: map from item to amount required Data hiding principle: the user should not know the representation It may change Instead, provide functions (or ‘operations’)

What's a CLASS – I A box with buttons (functions or operations or methods) A class is just a template class Parameters in def f1 Return value def f2

Words Method: this word is used in OOP theory Function: Python has these, as do other programming languages Operation: this word is used on OO analysis

What's a CLASS – II A box containing data (variables) A class is just a template class Parameters in def f1 Return value data def f2

Picture of Classes Data hiding: A class has Hide the attributes Use the operations A class has A name Attributes (i.e. variables) Operations (i.e. functions) Friend Class name name phone number setNumber sendText Attributes Operations

Object: An Instance of a Class A class is a template An object is a particular instance of a class Different data (attribute values) Same code alice: Friend bob: Friend name = "Alice" phone number = 123 name = "Bob" phone number = 456

Exercise 2.1 – Shopping Functions Recall the shopping list representations: List of pairs: (item string, amount integer) Dictionary: map from item to amount required Suggest the functions that would be useful What do you do with a shopping list?

Using Objects in Python You already do this

Example: Files file object import io f = open("hello.txt", 'w') f.write("This is a line\n") f.close() function or method method There is not a ‘file’ class; the object is of one of several classes

f and g are different objects Example: Two Files f and g are different objects import io f = open("hello.txt", 'w') g = open("bye.txt", 'w') f.write("Hello to you\n") g.write("Good bye. Go away.\n") f.write("You are welcome\n") g.close() f.close()

What Data is in the File Object? We are not told: details probably depend on the OS File name Location of file on disk Buffer of text Each file object must have different data

Function and Method Syntax object strng1 = "hello william" n = len(string1) strng2 = strng1.upper() dot function name str.upper(strng1) function name class name ‘str’ is a class str(99) – returns a string object Equivalent syntax

Functions and Methods strng1 = "hello william" n = len(string1) strng2 = strng1.upper() object function name The strng1 object has a class Take the upper() function from this class Call it with the object as the first parameter … add any further parameters

Lists are Objects >>> lst = [1,2,3,4] >>> lst.append(99) >>> lst [1, 2, 3, 4, 99] >>> lst = [1,2,3,4] >>> type(lst) <class 'list'> >>> list.append(lst, 99) >>> lst [1, 2, 3, 4, 99] The list is changed Append 99 to the list lst Nothing is returned

Exercise 3.1 and 3.2 Look at String and List method in the Python documentation Try some out.

Define New Classes in Python This bit is new

Declaring A Class A person class with two functions setAge() function sets an attribute age Remember: in Python variables are initialised, not declared class Person: def setAge(self, a): self.age = a def getAge(self): return self.age

Use class name to construct new objects Using The Person Class Create instance of the Person class i.e. people! Use class name to construct new objects p1 = Person() p2 = Person() p1.setAge(21) print(p1.getAge()) p2.setAge(101) print(p2.getAge())

What is ‘self’? The name self is used by convention Not a key word Always use it Explanation (not essential) In the ‘dot’ syntax, object is first parameter … so function called with method syntax needs at least one parameter

Exercise 4.1 – Person Class Enter the Person class The class declaration and the ‘using code’ go in the same file Add another attribute: What else can you know about a person?

Problem – Initialising Attributes What happens if we get the age before it is set? Need to initialise the attributes p1 = Person() p2 = Person() print(p1.getAge()) p1.setAge(21)

Constructor Constructor is a special function Called using class name class Person: def __init__(self, n): self.name = n self.age = 0 def setAge(self, a): self.age = a def getAge(self): return self.age

Using a Constructor Constructor called using class name If you do not define a __init__ the default constructor creates an empty object p1 = Person("Alice") p2 = Person("Bob") print(p1.getAge()) p1.setAge(21)

Exercise 4.2 – Add a Person Constructor Add a constructor to the person class Initialise all the attributes Either to default values Or to values given as parameters Write code to use the class

Working With Many Source Files Module – file containing Python definitions Contains function and class definitions Guideline Write each class in a separate file Filename same as class name Import: from Person import Person

Summary Object-oriented programming is a way to organise more complex programs Learn the syntax and behaviour Learn how to use OO to organise a program A class is a template for an object. An object has Attributes: what is unique about this object? Operation: what can you do to it? Data and code are organised together Supports data (information) hiding – abstraction