Introduction to Object-Oriented Programming Concepts

info 206 lab exercise 1 l.w
1 / 14
Embed
Share

Learn about classes, objects, and Python programming through lab exercises covering the basics of object-oriented programming. Explore the concepts of objects, classes, attributes, methods, and more in this informative tutorial.

  • Object-Oriented Programming
  • Python
  • Classes
  • Objects
  • Lab Exercises

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/23/2013 i206 Lab 1 - Exercise 1

  2. Logistics What development environments are people using? 1/23/2013 i206 Lab 1 - Exercise 2

  3. Logistics Python version: let s decide as a class. 1/23/2013 i206 Lab 1 - Exercise 3

  4. Reference Object An object associates data with the particular operations that can use or affect that data. ~ Apple Similar to the real-world, objects have both state and behavior. ~Java Objects encapsulate both data and methods that interacts with the data. Objects can store data in fields, also known as instance variables (attributes) Attributes can be a variety of data types Attributes can also store references to other objects Objects can have methods An object s attributes and methods are defined by its class 1/23/2013 i206 Lab 1 - Exercise 4

  5. Reference Class: The class is the blueprint for creating an object. The class definition declares the data fields that become part of every object of the class, and it defines a set of methods that all objects in the class can use. Classes are used to create objects, which are then called instances of that class A method is a function that is associated with a particular class. Unlike a function, a method has to be called by an instance of the class where the method was defined. 1/23/2013 i206 Lab 1 - Exercise 5

  6. Lab Exercise The Bank Account Class The object-oriented bank account example is a common demonstration of OOP by modeling a bank account class. Let s see if we can do the same. We ll create a program called PythonBanking. Let s start by launching your favorite IDE for Python programming. 1/23/2013 i206 Lab 1 - Exercise 6

  7. Lab Exercise - Style Guidelines 1. The first line of all your python programs must be: #!/usr/bin/env python #! a.k.a. shebang indicates that the file is a script /usr/bin/env python tells the computer to use this interpreter to read this file Set your full name in the __author__ special attribute at the top of each python file Set your name@ischool.edu email in the __email__ special attribute Set your python version in the __python_version attribute You can determine your python version by typing this in the command line: 2. 3. 4. >>python version 5. __can_anonymously_use_as_example specifies whether or not we can use your code (or portion of your code) anonymously for example and/or demonstration 1/23/2013 i206 Lab 1 - Exercise 7

  8. Lab Exercise - Style Guidelines Example header of a python program: #!/usr/bin/env python __author__ = Sarah Van Wart' __email__ = vanwars@ischool.berkeley.edu' __python_version = 3.2.3' __can_anonymously_use_as_example = True 1/23/2013 i206 Lab 1 - Exercise 8

  9. Lab Exercise - Declaring a Python Class Before an object can be instantiated we first need to define the blueprint for the object with class definition: class BankAccount(object): represents a generic banking account This header indicates that the new class is a BankAccount, which inherits from the generic object class. The body is where we define the class variables and functions but currently it is empty. To create a BankAccount object, you call BankAccount like a function: personalAccount = BankAccount() The return value is a reference to a BankAccount object. Creating a new object is called instantiation, and the object is an instance of the class. 1/23/2013 i206 Lab 1 - Exercise 9

  10. Lab Exercise Defining Class Attributes Let s extend our BankAccount class to add class attributes to hold the account name and number. class BankAccount(object): represents a generic banking account name = accountNumber = balance = 0 We can assign values to the attributes with dot notation: personalAccount.name = Sarah Van Wart personalAccount.accountNumber = 6498-0001 personalAccount.balance = 100.0 We can also use the dot notation to access the values of the object s attributes: >>>print(personalAccount.name) Sarah Van Wart >>>print(personalAccount.accountNumber) 6498-0001 1/23/2013 i206 Lab 1 - Exercise 10

  11. Lab Exercise Defining Class Attributes We can also use the init (short for initialization) method that gets invoked when an object is instantiated: #inside class BankAccount def __init__(self, accountName, accountNumber, accountBalance): self.name = accountName self.accountNumber = accountNumber self.balance = accountBalance To create a BankAccount object with __init__: personalAccount = BankAccount( SarahVan Wart , 6498-0001 , 1000.0) 1/23/2013 i206 Lab 1 - Exercise 11

  12. Lab Exercise Adding Methods to Class Methods are semantically the same as functions, in which, they can take arguments and return a result. However, methods are defined inside a class definition and they are called explicitly by the class instances. For example, we want to deposit money into a bank account. # inside class BankAccount: def deposit(self, amount): self.balance = self.balance + amount Using the earlier personalAccount: >>>personalAccount = BankAccount( Alex Chung , 234324ff3 , 1000.0) >>>personalAccount.deposit(100.0) >>>print(personalAccount.balance) 1100.0 1/23/2013 i206 Lab 1 - Exercise 12

  13. Lab Exercise Your code should look like this: #!/usr/bin/env python __author__ = Sarah Van Wart' __email__ = vanwars@ischool.berkeley.edu' __python_version = '3.2.2' __can_anonymously_use_as_example = 'True' """OOP Demonstration with Bank Account Program""" class BankAccount(object): """represents a generic banking account" def __init__(self, accountName, accountNumber, accountBalance): self.name = accountName self.accountNumber = accountNumber self.balance = accountBalance def deposit(self, amount): self.balance = self.balance + amount if __name__=="__main__": pa = BankAccount('Sarah Van Wart', '6498-0001', 0.0) pa.deposit(10) print(pa.name, pa.balance) 1/23/2013 i206 Lab 1 - Exercise 13

  14. A Little Extra Modify the BankAccount class to help John Doe figure out his ending balance. John Doe opens a new savings account with $50. He earns monthly (every 30 days) 5% interest if his account has a balance of < $500 and 10% if his balance exceeds $500. What will his ending balance be after 6 months if he deposits $200 every 30 days (after his initial deposit of $50 at day 0)? 1/23/2013 i206 Lab 1 - Exercise 14

Related


More Related Content