to to get started
play

To To Get Started Paper sheet Online: - PowerPoint PPT Presentation

To To Get Started Paper sheet Online: http://www.eecs.qmul.ac.uk/~william/CAS-London-2020.html Download sample programs - Create directory - Unzip - Recommend copy sample files before editing Object Oriented Programming in A Level


  1. To To Get Started • Paper sheet • Online: http://www.eecs.qmul.ac.uk/~william/CAS-London-2020.html • Download sample programs - Create directory - Unzip - Recommend copy sample files before editing

  2. Object Oriented Programming in A Level William Marsh [Original version co-authored with Melissa Bustamante]

  3. Session Aim and Outline Se Outline Aims • Using classes: the Face • Be able to motivate the use of classes and objects • Attributes and the constructor • Be able to explain OOP in • Reflection : Decomposition and relations Abstraction and design Decomposition • Practical break • ...progression in OOP • Reflection: How versus Why • Be aware of issues for teaching • Progression OOP • Misconceptions • Python versus java

  4. A Face Class: Becoming a User of Objects There are many examples of classes and object that are familiar

  5. Us Using the Face Class • File class is This is a variable. What is a familiar from turtle import * the type of its value? example from Face import Face • Are we f1 = Face(0, 0) aware we f1.draw() Draw methods: use which face is objects? f2 = Face(-200, 0) drawn? f2.setSize(80) f2.draw()

  6. Obj Object ects and and Metho hods ds Name Type Description f1, f2 Variables; A drawing of a face Objects of ‘Face’ class Face Class name; constructor Create a Face object setSize Method of ‘Face’ class Set size of the face draw Method of ‘Face’ class Draw the face ‘Method’ is an OO word for function

  7. Su Summary: Us Using Objects • Face representation is hidden • Method act (read or update) on objects f2.setSize(80) The object to be A second parameter: acted on the size to set The method: implements the action

  8. Te Teaching Functional Decomposition • You have already learnt about functions - How they work - How to use them • Is it easy or hard to learn about functions? - What aspects are easier? - What aspects are harder?

  9. Reflection on: Abstraction on and Decom ompos osition on Motivation: What are we trying to achieve with classes?

  10. Li Liskov and and Gu Guttag 1986 1986 – De Decom ompos osition on • A very small program consisting of no more than a few hundred lines can be implemented as a single monolithic unit. • However, as the size of the program increases such a ... structure is no longer reasonable ... • Instead the program must be decomposed into ... modules that together provide the desired function. • … how to decompose large programming problems into small ones … what kinds of modules are useful … [how] modules can be combined to solve the original problem

  11. Tw Two Different Aims for Learning OOP How Why • How to use classes • Decomposing a problem using classes - Create a new object - Use objects as variables (e.g. in a - Which classes to use? list) - What makes a good class? • How to create (declare) new • How to do good abstractions classes - Analysis of the problem - Add method and attributes - … and constructors • How classes can interact • How to create sub-classes - Software design (inheritance)

  12. Su Summary • Emphasis continuity between OOP and previous programming - Use of objects and methods explained - Abstractions implemented using functions • Program decomposition; problem abstraction - Distinguish between learning syntax and - … practicing abstraction and program design • OOP is a new solution to the goal of decomposition using abstraction - Comparison with use of functions

  13. Practical Wor ork Drawing Faces: Exercises 1 and 2

  14. Declaring You our Own Classes Key concepts

  15. The The Faces es Ex Exampl ple • Using Python turtle graphics • Good points - Visual and ?? Engaging (creative) - Class versus object distinction - Incremental • Limitations - Not typical of OO design - Complexity of drawing a distraction

  16. Cl Class De Declaration on Constructor from turtle import class Face: Attributes def __init__(self, xpos, ypos): self.size = 50 self.coord = (xpos, ypos) self.noseSize = 'normal' Method def setSize(self, radius): self.size = radius def draw(self): ... self.drawOutline() ...

  17. Class De Cl Declaration on def goHome(self): penup() from turtle import goto(self.coord) setheading(0) class Face: def drawOutline(self): def __init__(self, xpos, ypos): penup() self.size = 50 forward(self.size) self.coord = (xpos, ypos) left(90) self.noseSize = 'normal' pendown() circle(self.size) def setSize(self, radius): self.goHome() self.size = radius def draw(self): ... self.drawOutline() ...

  18. Defining a Con De Constructor or class Face: Constructor • Has a special name def __init__(self, xpos, ypos): • May have self.size = 50 parameters self.coord = (xpos, ypos) self.noseSize = 'normal' Constructor name Constructor • Don’t forget ‘self’ Always ‘self’ parameter Initialise attributes

  19. Attributes – Good At Good Practice • Attributes are not declared class Face: - In Python, nothing is! def __init__(self, xpos, ypos): self.size = 50 self.coord = (xpos, ypos) • Good practice to initialise all self.noseSize = 'normal' attributes in the constructor - Getters do not fail def setSize(self, radius): - Clear what the attributes are self.size = radius - Do not add more

  20. Practical Wor ork Drawing Faces: Exercise 3 onwards

  21. Teaching OOP in Python on

  22. Pr Program Structure and Complexity If & loops x = • Program grows more complex while x > : y = in structure print • Simpler elements remain Functions - If & loop à part of function Function def - Method à part of class Function def Main program • Initialise vars • Call functions Classes & objects class Friend: def __init__() class Town: def m1(a, b): def __init__() Main program def m1(a, b): • Create obj • Call methods

  23. OOP OOP Concept Details Basic • Calling a method of an object Con Concepts Prerequisite mechanics • Class as a template for data knowledge: • Class as a collection of methods functions & parameters Constructors • Definition and use Interaction • Object as a value (variable, list item, …) • Object as an attribute value (has-a Prerequisite relationship) knowledge: • Object passed as a parameter basic Abstraction • Class as a domain concept mechanisms and modelling • Methods (and constructor) have parameters Inheritance • Superclass and subclasses • Constructor called using super() • Method inherited or overridden

  24. Misconception Possible Evidence Also lack of Attributes in the wrong scope • Omission of self (assignment or use) prerequisite Confusion between class and • No objects created knowledge: object • Only one instance functions & • Inheritance rather than instance parameters Confusion between class and • Many classes – all simple attribute Objects only contain data • No encapsulation • Only get and set methods Objects do not interact • All code in single class • Classes defined but not imported • Objects not used as attributes • Objects never passed as parameters Believing objects are copied • Unnecessary copying not shared • Unexpected sharing

  25. Py Python Issues for Teaching OOP Usual OOP Python • The attributes are declared • Nothing is declared • A class has a fixed set of • Attributes appear when assigned attributes to • Attributes can be hidden: access • Hiding is not enforced only by methods Use Python to teach OOP • Avoid some Python tricks • Use only a subset • … explain later

  26. Python Py ve versus Ja Java • No declarations • Declarations • Values are typed • Static typing of variables - Variable types are dynamic • Run time type checking • Compile time type checking • Syntax with indentation • Syntax with braces { } • Permissive philosophy • Rigid philosophy

  27. Su Summary • Object-oriented programming - Builds on more basic programming - A approach to program decomposition (decomposition take practice) - Previous experience learning decomposition • Progression: concepts not syntax - Proficiency with functions essential - Class versus object - Classes have attributes and methods - Constructor - Relationships between classes; objects as values - Inheritance • Python – some disadvantages

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend