INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Python Objects and Classes
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Object Oriented Design and Classes
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Modules and Objects Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Design and Classes. What is a class? Programming mechanism to support OOD Data and the methods that operate on that data – collectively.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
C++ fundamentals.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
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.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Writing Classes (Chapter 4)
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
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.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object Oriented Programming In Python
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 1 Object Orientation: Objects and Classes.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Fundamentals of Programming I More Data Modeling
suggested reading: Java Ch. 6
Object Oriented Programming
Chapter Three - Implementing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Adapted from slides by Marty Stepp and Stuart Reges
15-110: Principles of Computing
JAVA CLASSES.
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Programming For Big Data
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1

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/18/2012i206 Lab 1 - Exercise2

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/18/2012i206 Lab 1 - Exercise3

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/18/2012i206 Lab 1 - Exercise4

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 2.Set your full name in the __author__ special attribute at the top of each python file 3.Set your in the __ __ special 4.Set your python version in the __python_version attribute – You can determine your python version by typing this in the command line: >>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/18/2012i206 Lab 1 - Exercise5

Lab Exercise - Style Guidelines Example header of a python program: #!/usr/bin/env python __author__ = 'Alex Chung' __ __ = __python_version = '2.6.1' __can_anonymously_use_as_example = True 1/18/2012i206 Lab 1 - Exercise6

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/18/2012i206 Lab 1 - Exercise7

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 = “Alex Chung” personalAccount.accountNumber = “234324ff3” personalAccount.balance = We can also use the dot notation to access the values of the object’s attributes: >>>print(personalAccount.name) Alex Chung >>>print(personalAccount.accountNumber) ff3 Lab Exercise – Defining Class Attributes 1/18/2012i206 Lab 1 - Exercise8

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(“Alex Chung”, “234324ff3”, ) Lab Exercise – Defining Class Attributes 1/18/2012i206 Lab 1 - Exercise9

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”, ) >>>personalAccount.deposit(100.0) >>>print(personalAccount.balance) Lab Exercise – Adding Methods to Class 1/18/2012i206 Lab 1 - Exercise10

Lab Exercise Your code should look like this: #!/usr/bin/env python __author__ = 'Alex Chung' __ __ = __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 personalAccount = BankAccount("Alex Chung", "233444", ) print (personalAccount.balance) personalAccount.deposit(100.0) print (personalAccount.balance) 1/18/2012i206 Lab 1 - Exercise11

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/18/2012i206 Lab 1 - Exercise12