1 / 18

I210 review (for final exam) Fall 2011, IUB

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 A4 cheat- sheet is allowed ; write your name and ID on the cheat- sheet; and turn in your cheat -sheet together with the final exam.

ayame
Download Presentation

I210 review (for final exam) Fall 2011, IUB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. I210 review(for final exam)Fall 2011, IUB

  2. What’s in the Final Exam • Multiple Choice (5) • Short Answer (5) • Program Completion (3) • Note: A single-sidedA4 cheat-sheet is allowed; write your name and ID on the cheat-sheet; and turn in your cheat-sheet together with the final exam.

  3. Basics • Variables • Data types • list • conversion • Branching structures • Loops (while & for loops) • Functions • Function header • Return values • Parameters and arguments

  4. Understanding OOP • OOP allows representation of real-life objects as software objects (e.g., a dictionary as an object) • Object: A single software unit that combines attributes and methods • Attribute: A "characteristic" of an object; like a variable associated with a kind of object • Method: A "behavior" of an object; like a function associated with a kind of object • Class: Code that defines the attributes and methods of a kind of object (A class is a collection of variables and functions working with these variables)

  5. Creating Classes for Objects class Puppy(object): def __init__(self, name, color): self.name = name self.color = color def bark(self): print "I am", self.color, self.name puppy1 = Puppy("Max", "brown") puppy1.bark() puppy2 = Puppy("Ruby", "black") puppy2.bark() • Class: Code that defines the attributes and methods of a kind of object • Instantiate: To create an object; A single object is called an Instance

  6. Special Methods def __init__(self): print "A new critter has been born!” #Constructor, __init__() #Automatically called by new Puppy object def __str__(self): rep = "Critter object\n" rep += "name: " + self.name + "\n" return rep # __str__() # return a string

  7. Private vs Public Attributes and Methods • Public:Can be directly accessed by client code • Private:Cannot be directly accessed (easily) by client code • By default, all attributes and methods are public • But, can define an attribute or method as private class Critter(object): def __init__(self, name, mood): self.name = name # public self.__mood = mood # private

  8. Understanding Inheritance • Inheritance: An element of OOP that allows a new class to be based on an existing one where the new automatically gets (or inherits) all of the methods and attributes of the existing class • The children classes get all the capabilities (methods) and properties (attributes) the parent class has; the children classes are also called derived classes • Get the code for free! (code-reuse) – inheritance allows a new class to re-use code which already existed in another class (the parent class)

  9. Derived Classes are New Classes • To create specializations of existing classes or objects by adding new attributes and methods! • often called subtyping when applied to classes. In specialization, the new class or object has data or behavior aspects that are not part of the inherited class. • Overriding (e.g., over-ridding of the + operator, so + has different meaning, addition of two numbers, concatenation of two strings, etc) – the same method that does something different Guide to Programming with Python

  10. Altering the Behavior of Inherited Methods: Overriding • Override: To redefine how inherited method of base class works in derived class • Two choices when overriding • Completely new functionality • Incorporate functionality of overridden method, add more • How to invoke base class’ methods?

  11. Inheritance Example: Animal Class class Animal(object): def __init__(self, name): # Constructor self.name = name def get_name(self): return self.name class Cat(Animal): def talk(self): return 'Meow!' class Dog(Animal): def talk(self): return 'Woof! Woof!' animals = [Cat('Missy'), Cat('Mr. Bojangles'), Dog('Lassie')] for animal in animals: print animal.talk() + ' I am ' + animal.get_name() Base class: A class upon which another is based; it is inherited from by a derived class Derived (child) class: A class that is based upon another class; it inherits from a base class

  12. Understanding Event-Driven Programming • Event-driven program: A program that responds to actions regardless of the order in which they occur (vs structured programming) • Event: Something that happens involving a program's objects (mouse moves over; click of a button, etc) • Event handler: Code that runs when a specific event occurs • Bind: To associate an event with an event handler • Event loop: A loop that checks for events and calls appropriate event handlers when they occur • GUI programs traditionally event-driven: the users control the flow of the program

  13. Buttons on Strike (I) from Tkinter import * root = Tk() #a root window, upon which other GUI elements can be added root.title("Simple GUI Demo") #add title root.geometry("400x200") #change the size of the window app = Frame(root) #create a frame, upon which other Widgets can be added app.grid() #need to invoke grid() method button = Button(app, text = "I am button #1”) button.configure(text = "I am button #2”) button['text'] = "I am button #3” button["command"] = update_count button.grid() root.mainloop() #must start up the window's event loop!!! (II) (III) (IV)

  14. Writing a Game Using Pygame & Livewires #(1) Import pygame or livewires from livewires import games #(2) Create a graphical window games.init(screen_width = 640, screen_height = 480, fps = 50) #(3) Start up window’s main loop games.screen.mainloop()

  15. Understanding the Graphics Coordinate System • Graphics screen made up of rows and columns of pixels • Specify point on screen with coordinates: x and y ; Upper-leftmost pixel is (0,0) • Can place graphics objects on screen using coordinate system screen.width screen.height

  16. Control a Sprite Sprite: A graphics object with an image class Pizza(games.Sprite): """ A bouncing pizza. """ def update(self): if games.keyboard.is_pressed(games.K_UP): self.x -= 1 if self.right > games.screen.width or self.left < 0: self.dx = -self.dx if self.bottom > games.screen.height or self.top < 0: self.dy = -self.dy

  17. Sample Question 1 • In Pygame, what is a sprite? What is a sprite collision?

  18. Sample Question 2 • Complete the following GUI code that creates a button… from ____________ import * class Application(Frame): def __init__(self, master): Frame.__init__(self, master) self.grid() ___________________________ self.create_widget() defcreate_widget(self): self.lbl = Label(self, text="Click to increment!") ________________________ self.bttn = Button(self) ...

More Related