Python Programming in Context

Slides:



Advertisements
Similar presentations
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
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 Design and Classes
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
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.
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.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
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.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Animation and Double-Buffering. The animation methods described here are based on standard techniques of double-buffering applicable to most high-level.
Graphics and Procedures Programming Right from the Start with Visual Basic.NET 1/e 5.
Python Programming in Context Chapter 2. Objectives To understand how computers can help solve real problems To further explore numeric expressions, variables,
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Chapter 6 – Interacting Objects: Newton’s Lab. topics: objects interacting with each other, using helper classes, using classes from the Java library.
You SHOUD HAVE.. C:\100 folder A desktop shortcut to “IDLE (Python34)”
Python Programming in Context Chapter 12. Objectives To introduce the concept of inheritance To create a working object-oriented graphics package To provide.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Python Programming in Context Chapter 13. Objectives To write an event driven program To understand and write callback functions To practice with lists.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
If..else Use random numbers to compute an approximation of pi Simulation of a special game of darts Randomly place darts on the board pi can be computed.
Graphing Motion. You will need: 3 colored pencils: red, blue, green A ruler if straight lines are important to you.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Objects and Classes Procedural Programming A series of functions performing specific tasks Data items are passed from one function to another by arguments.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
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.
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
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,
Python Programming in Context Chapter 11. Objectives To explore classes and objects further To design a large multiclass application To implement a graphical.
INTRO2CS Tirgul 8 1. What We’ll Be Seeing Today  Introduction to Object-Oriented Programming (OOP).  Using Objects  Special methods 2.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program.
6. Classes & Objects Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Adapted from slides by Marty Stepp and Stuart Reges
Python programming - Defining Classes
Adapted from slides by Marty Stepp and Stuart Reges
Classes and Objects – digging deeper
CS320n – Elements of Visual Programming
Object-Oriented Programming (OOP) in Python
Software Development Java Classes and Methods
turtle module Simple graphics programming How to use it ?
CS-104 Final Exam Review Victor Norman.
12. Classes & Objects Let's Learn Python and Pygame
Classes In C#.
CS 100: Roadmap to Computing
Guide to Programming with Python
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 31: Encapsulation
CS 100: Roadmap to Computing
Week 8 Classes and Objects
Adapted from slides by Marty Stepp and Stuart Reges
Object Oriented Programming in Python
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
12. Classes & Objects a class factory makes objects + functions
Classes, Objects and lists
Migrating to Object-Oriented Programs
Using Modules.
Presentation transcript:

Python Programming in Context Chapter 10

Objectives To explore classes and objects further To understand how to construct a class To write constructors, accessor methods, and mutator methods To understand the concept of self To explore instance data To implement a graphical simulation using objects

Object Oriented Programming Objects are instances of Classes Objects can perform methods Instance Data What an object knows about itself Methods What an object can do

Turtle objects Instance Data Methods Color Heading Tail position Forward Backward Up

Astronomy Design classes to represent planets, sun, moons, etc. Use these classes to write programs that manipulate the solar system

Planet Class Instance Data Name Mass Distance from sun radius

Listing 10.1 class Planet: def __init__(self, iname, irad, im, idist): self.name = iname self.radius = irad self.mass = im self.distance = idist

Figure 10.1

Types of Methods Constructor Accessor Mutator Used to make a new instance of the class Accessor Used to get information from an object Get instance data Mutator Used to change something about an object Change instance data

Listing 10.2 def getName(self): return self.name def getRadius(self): return self.radius def getMass(self): return self.mass def getDistance(self): return self.distance

Listing 10.3 def getVolume(self): v = 4.0/3 * math.pi * self.radius**3 return v def getSurfaceArea(self): sa = 4.0 * math.pi * self.radius**2 return sa def getDensity(self): d = self.mass / self.getVolume() return d

Listing 10.4 def setName(self, newname): self.name = newname

Listing 10.5 def show(self): print(self.name)

Listing 10.6 def __str__(self): return self.name

Figure 10.2

Namespaces Namespaces and reference work as they did before self is the name of the implicit parameter that always refers to the object itself Never explicitly pass a value to the implicit parameter

Figure 10.3

Figure 10.4

The Sun Class Instance Data Name Mass Radius Temperature

Listing 10.7 import math class Sun: def __init__(self, iname, irad, im, itemp): self.name = iname self.radius = irad self.mass = im self.temp = itemp def getMass(self): return self.mass def __str__(self): return self.name

Solar System Class A sun Many planets Use a list to keep the collection of planets

Listing 10.8 class SolarSystem: def __init__(self, asun): self.thesun = asun self.planets = [] def addPlanet(self, aplanet): self.planets.append(aplanet) def showPlanets(self): for aplanet in self.planets: print(aplanet)

Figure 10.5

Visualize and Animate Use a turtle to draw the sun and the planets Animate the solar system by moving the turtle Need to develop a simple understanding of planetary movement Velocity Acceleration Mass

Listing 10.9 class SolarSystem: def __init__(self, width, height): self.thesun = None self.planets = [] self.ssturtle = turtle.Turtle() self.ssturtle.hideturtle() self.ssscreen = turtle.Screen() self.ssscreen.setworldcoordinates(-width/2.0,-height/2.0,width/2.0,height/2.0) def addPlanet(self, aplanet): self.planets.append(aplanet) def addSun(self, asun): self.thesun = asun def showPlanets(self): for aplanet in self.planets: print(aplanet) def freeze(self): self.ssscreen.exitonclick()

Listing 10.10 class Sun: def __init__(self, iname, irad, im, itemp): self.name = iname self.radius = irad self.mass = im self.temp = itemp self.x = 0 self.y = 0 self.sturtle = turtle.Turtle() self.sturtle.shape("circle") self.sturtle.color("yellow") #other methods as before def getXPos(self): return self.x def getYPos(self): return self.y

Listing 10.11 class Planet: def __init__(self, iname, irad, im, idist, ic): self.name = iname self.radius = irad self.mass = im self.distance = idist self.x = idist self.y = 0 self.color = ic self.pturtle = turtle.Turtle() self.pturtle.color(self.color) self.pturtle.shape("circle") self.pturtle.up() self.pturtle.goto(self.x,self.y) self.pturtle.down() #other methods as before def getXPos(self): return self.x def getYPos(self): return self.y

Figure 10.6

Figure 10.7

Figure 10.8

Listing 10.12 class Planet: def __init__(self, iname, irad, im, idist, ivx, ivy, ic): #other instance variables as before self.velx = ivx self.vely = ivy

Listing 10.13 def moveTo(self, newx, newy): self.x = newx self.y = newy self.pturtle.goto(newx, newy) def getXVel(self): return self.velx def getYVel(self): return self.vely def setXVel(self, newvx): self.velx = newvx def setYVel(self, newvy): self.vely = newvy

Listing 10.14 def movePlanets(self): G = .1 dt = .001 for p in self.planets: p.moveTo(p.getXPos() + dt * p.getXVel(), p.getYPos() + dt * p.getYVel()) rx = self.thesun.getXPos() - p.getXPos() ry = self.thesun.getYPos() - p.getYPos() r = math.sqrt(rx**2 + ry**2) accx = G * self.thesun.getMass()*rx/r**3 accy = G * self.thesun.getMass()*ry/r**3 p.setXVel(p.getXVel() + dt * accx) p.setYVel(p.getYVel() + dt * accy)

Listing 10.15 def createSSandAnimate(): ss = SolarSystem(2,2) sun = Sun("SUN", 5000, 10, 5800) ss.addSun(sun) m = Planet("MERCURY", 19.5, 1000, .25, 0, 2, "blue") ss.addPlanet(m) m = Planet("EARTH", 47.5, 5000, 0.3, 0, 2.0, "green") m = Planet("MARS", 50, 9000, 0.5, 0, 1.63, "red") m = Planet("JUPITER", 100, 49000, 0.7, 0, 1, "black") numTimePeriods = 2000 for amove in range(numTimePeriods): ss.movePlanets() ss.freeze() createSSandAnimate()

Figure 10.9