Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,

Slides:



Advertisements
Similar presentations
CS104: Chapter 15 CS 104 Students and me. Big Question Q: You say creating a class is how to create a new type. Why would we even want to do this?! A:
Advertisements

Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
Class Inheritance Victor Norman CS104. Reading Quiz, Q1 In the first reading, the author uses the following classes to illustrate Subclassing: A.Shape,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein Classes and Objects Object Oriented Programming.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Lilian Blot TOWARDS MORE ADVANCED CONCEPTS & OBJECT ORIENTED PROGRAMMING Building Data Structure Autumn 2014 TPOP 1.
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
I210 review (for final exam) Fall 2011, IUB. What’s in the Final Exam Multiple Choice (5) Short Answer (5) Program Completion (3) Note: A single-sided.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
Classes 3 COMPSCI 105 S Principles of Computer Science.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Inheritance: Section 9.1, 9.2 Victor Norman CS106.
Python Programming Chapter 14: Classes and Methods Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Object-oriented Programming (review + a few new tidbits) "Python Programming for the Absolute Beginner, 3 rd ed." Chapters 8 & 9 Python 3.2 reference.
Java Classes Using Java Classes Introduction to UML.
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Object Oriented Programing (OOP)
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.
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Computer Science 112 Fundamentals of Programming II Iterators.
Class Inheritance Victor Norman CS104. The Problem What is the basic problem that inheritance tries to solve? Answer: Two types (classes) may have lots.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Adapted from slides by Marty Stepp and Stuart Reges
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Andrew(amwallis) Classes!
COMPSCI 107 Computer Science Fundamentals
Classes and Objects – digging deeper
COMPSCI 107 Computer Science Fundamentals
Advanced Object-Oriented Programming
CS-104 Final Exam Review Victor Norman.
Copyright (c) 2017 by Dr. E. Horvath
Python Classes By Craig Pennell.
Introduction to Object-Oriented Programming (OOP) II
User Defined Classes CS F.
CSC 113: Computer programming II
Teach A-Level Computer Science: Object-Oriented Programming in Python
Week 8 Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Class Examples.
Classes, Objects and lists
Conrad Huang Genentech Hall, N453A x6-0415
7 – Variables, Input and Output
A Level Computer Science Topic 6: Introducing OOP
Introduction to Object-Oriented Programming (OOP) II
Abstraction and Objects
Introduction to Methods and Interfaces
Presentation transcript:

Chapter 17 Q and A Victor Norman, et al. CS104

What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects, which hold data, but also can do stuff to themselves or for the caller. Functional style: set_add(set, newItem) – Pattern: function(object, values) OO style: set.add(newItem) – Pattern: object.method(values)

Example: Set class (type) Old style: – set = newSet( ) – set = setAdd(set, newItem) – set = setDel(set, delItem) – tOrF = setIn(set, value) – set = setAnd(set, otherSet) – set = setOr(set, otherSet) New style: – set = Set( ) – set.add(newItem) – set.del(delItem) – tOrF = set.in(value) – set = set.and(otherSet) – set = set.or(otherSet)

Binding together data and ops A class definition binds together – the data an instance stores (its attributes) and – the operations on the data (called “methods”) Forces programmer to keep related code together. Allows you to create a new type.

Advantages of OO You can control how an object can be created and used. – because you write the code for how an object is constructed. You can hide how an object implements its operations (data hiding). – because you define what a user can do with objects, so the user doesn’t have to know how it works.

Example Suppose we want to define a type Scribbler, which will represent a programmable robot. We want it to be able to move around, keep track of where it is and what direction it is pointing, etc. – it might even keep track of where it saw obstacles, etc. Its attributes would be: x, y, angle, etc.

Example (2) To create one, we’d do this code: scrib = Scribbler() – calls the constructor code we’ve defined: class Scribbler: def __init__(self): self._x = 0 self._y = 0 self._angle = 0 # east Now, we control how a Scribbler is made and ensure its state is correct.

Example (3) What do we want to do a Scribbler? scrib.move(16, 12) – moves the robot there and object remembers where it is and what angle it is facing. scrib.getX() scrib.getY() scrib.getAngle() – scrib user can ask the scribbler where it is and what angle it is facing.

Example (4) scrib.seeObstacle() – ask the scribbler if it sees an obstacle. scrib.forwardUntilSeeObstacle() scrib.setAngle(newAngle) – turns the scribbler to that angle scrib.setY(newY) – moves the scribbler to new position

String representation (Review: when constructor is called, __init__() is run in the class.) When you do str(object), __str__ is run in the object’s class, if defined. – it must return a string, which should be a readable representation of that object. Very convenient to be able to do: print scrib # which calls str(scrib) before printing

__str__ code class Scribbler:... init method … def __str__(self): return “Scribbler at (%g, %g), facing %g degrees” % (self._x, self._y, self._angle) print scrib output: Scribbler at (3.0, 4.0), facing 45.0 degrees

self parameter if we want to do scrib.getAngle(), we implement: …. in Scribbler class …. def getAngle(self): return self._x Notice that scrib  self. Object before the. is the first parameter in the method definition. self is the name of the object in code in the class.

Operator Overloading if I do 3 + 4, we are adding integers. if I do “hi” + “there” we are concatenating strings. – This is overloading of the operator +. for a Set type, we might want to do this: s = Set(…) s = s + 3# add 3 to the set. We’d have to implement __add__ in Set class.

Other operators to overload Common operators to overload: – __cmp__(self, other): compares two objects. Return -1 if self other. (If implemented, you can sort objects.) – __len__(self): can use len(obj). Useful, e.g., for Set type to ask how many items are in the set. – __contains__(self, item): if implemented, can ask if something is in the object.