From datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second.

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Classes and Inheritance. 2 As the building blocks of more complex systems, objects can be designed to interact with each other in one of three ways: Association:
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Object Oriented Design and Classes
LECTURE 27 Designing Classes. The Course Class  What data members do we need to represent a class?
Creating your own classes class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name.
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Object Oriented Design and Classes. What is a class? Programming mechanism to support OOD Data and the methods that operate on that data – collectively.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
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.
Guide to Programming with Python Chapter Eight (Part II) Object encapsulation, privacy, properties; Critter Caretaker game.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
Computer Science 111 Fundamentals of Programming I Search Algorithms.
Computer Science 111 Fundamentals of Programming I More Data Modeling.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Introduction to Object-Oriented Programming
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 18 What are programs for? Classes and Objects.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Halting Problem Introduction to Computing Science and Programming I.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
OOP Lecture 06. OOP? Stands for object oriented programming. You’ve been using it all along. Anything you use the dot ‘.’ on is an object.
Water Conservation
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Object Oriented Programming In Python
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
PYTHON PROGRAMMING Week 3 - Tuesday. STARTER Hour of Code Video.
Stack – Data Structure. Stack Definition class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item):
Objects, If, Routines. Creating Objects an object is a fundamental software element that has a state and a set of behaviors. Ex: bank account or student.
Functions CMSC 201 – Lab 5. Overview Objectives for today's lab:  Practice breaking down a program into multiple functions  Practice writing function.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
Adapted from slides by Marty Stepp and Stuart Reges
Guide to Programming with Python
Adapted from slides by Marty Stepp and Stuart Reges
Object-Oriented Programming (OOP) in Python
Example: Vehicles What attributes do objects of Sedan have?
COMP280:Introduction to Software Development Week 10, Lecture 28
Fundamentals of Programming I More Data Modeling
Engineering Computing
Guide to Programming with Python
Introduction to Object-Oriented Programming (OOP) II
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,
Elapsed Time By Mrs. Stephanie Estes.
Suggested Layout ** Designed to be printed on white A3 paper.
Fundamentals of Programming I Commonly Used Methods More Modeling
15-110: Principles of Computing
Fundamentals of Programming I More Data Modeling
Guide to Programming with Python Book is by Michael Dawson
CSE 231 Lab 11.
polymorphism Inheritance Base class Derived class
Lecture 18 Python OOP.
Object-Oriented Programming and class Design
Making our own Classes and objects
Object-Oriented Design AND CLASS PROPERTIES
Introduction to Object-Oriented Programming (OOP) II
Presentation transcript:

from datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def converttomus(self): return self.micro+self.sec* self.min*60* self.hr*60*60* def time_diff(self,watch): x = self.converttomus() y = watch.converttomus() diff = abs(x - y) return(diff) time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print(time1.time_diff(time2)) Class Stopwatch?

Private Attributes and Methods Private Attributes and methods (functions): Only other methods of the object can access or invoke these attributes/methods In our stopwatch class, we might want to make converttomus a private method So only other methods within the class can use it – there really isn’t a reason for anything else to use that method or for the object to use that method directly.

Private methods and attributes class Critter(object): """A virtual pet""" def __init__(self, name, mood): print("A new critter has been born!") self.name = name # public attribute self.__mood = mood # private attribute def talk(self): print("I'm“ + self.name) print("Right now I feel“ + self.__mood) def __privatemethod(self): print("This is a private method.") def publicmethod(self): print("This is a public method.") self.__privatemethod() # main crit = Critter(“Poochie", "happy") crit.talk() crit.public_method() print(crit.name) __________________________________________________________________________ Can we do print(crit.__mood)???? print(crit.mood) ???? crit.__privatemethod() ??? crit.privatemethod() ???

Example: class Checkbook(object): """A virtual pet""" def __init__(self, name, balance): print("Thanks for opening an account " + name) self.name = name # public attribute self.__balance = balance # private attribute def addmoney(self, amount): self.__balance += amount self.printbalance() def __warning(self,num): print("Warning: This transaction will overdraw your account by " + str(num) + ". Thus it has not been completed.") def spendmoney(self, amount): if amount > self.__balance: self.__warning(amount-self.__balance) else: self.__balance -= amount self.printbalance() def printbalance(self): print(“Your current balance is “ + self.__balance) # main myckbook = Checkbook(“Harriet Smith”, 3.25) myckbook.addmoney(6.75) #??? print(myckbook.__balance) #??? myckbook.printbalance() #??? myckbook.spendmoney(11.25) #??? myckbook.__warning(1.25) #??? __________________________________________________________________________

from datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def __converttomus(self): return self.micro+self.sec* self.min*60* self.hr*60*60* def time_diff(self,watch): x = self.__converttomus() y = watch.__converttomus() diff = abs(x - y) return(diff) time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print(time1.time_diff(time2)) #What if we did: print time1 Remember Class Stopwatch?

Printing an object time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print time1 Results? Probably not what we want we probably want to print out a StopWatch object’s properties (hour, minute, second, microsecond)

Creating String: class StopWatch: def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def __str__(self): vstr = "The time was: " amflag = True; if (self.hr > 12): vstr += str(self.hr - 12) amflag = False else: vstr += str(self.hr) vstr += ":" if self.min < 10: vstr += "0" vstr += str(self.min) if (amflag): vstr += " a.m. " else: vstr += " p.m. " vstr += str(self.sec) vstr += " seconds and " vstr += str(self.micro) vstr += " microseconds." return(vstr) #main time1 = StopWatch() print time1