CHAPTER 9 DEFINING CLASSES & CREATING OBJECTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
The Web Warrior Guide to Web Design Technologies
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Road Map Introduction to object oriented programming. Classes
VBA Modules, Functions, Variables, and Constants
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Guide to Programming with Python
CHAPTER 10 OBJECT INHERITANCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
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.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to programming in the Java programming language.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
1 10/15/04CS150 Introduction to Computer Science 1 Reading from and Writing to Files Part 2.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Introduction to Object Oriented Programming Chapter 10.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 - Introduction to C Programming
Objects as a programming concept
PHP Classes and Objects
Road Map Introduction to object oriented programming. Classes
Chapter 2 - Introduction to C Programming
Object-Oriented Programming: Classes and Objects
CHAPTER 8: USING OBJECTS
CHAPTER 4: Conditional Structures
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CHAPTER 9 DEFINING CLASSES & CREATING OBJECTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Creating Classes  Ruby has built-in classes, but you can create your own  Imagine organizing a database for bank accounts  Create a class describing the properties and behaviors of “all” bank accounts (c) 2012 Ophir Frieder et al

Defining Classes  The next example shows you how to define your own class  This is the way you create a new class (c) 2012 Ophir Frieder et al

Example 9.1: Class Definition Syntax 1 class Classname 2 def initialize(var1, var2,..., varn) = var1 = var = varn 7 end 8 9 def method_1 10 # code 11 end def method_2 14 # code 15 end 16 end (c) 2012 Ophir Frieder et al

1 class Classname 2 def initialize(var1, var2,..., varn) = var1 = var = varn 7 end 8 9 def method_1 10 # code 11 end def method_2 14 # code 15 end 16 end To define a class, use the class keyword followed by a name def defines new activities (called methods) that may be performed Also used to define the special method initialize which is called every time a new instance is created All classes have this special method, which is called a constructor (c) 2012 Ophir Frieder et al

Class Definition: Example 9.2  We will explain class generation and object instantiation(s) using an example of a bank account management system  First, create a Class called Account  Note the Capital letter!! Example 9.2: Account Version #1 1 class Account 2 def initialize(balance) = balance 4 end 5 end (c) 2012 Ophir Frieder et al

Class Definition  The variables inside the parenthesis after initialize are the parameters that are assigned when instantiating an object 1 class Account 2 def initialize(balance) = balance 4 end 5 end constructor (c) 2012 Ophir Frieder et al

Properties of an Instantiation  An object will have a variable called “balance” with an initial value which you have to assign using a parameter 1 class Account 2 def initialize(balance) = balance 4 end 5 end (c) 2012 Ophir Frieder et al

Class Instantiation  The special is used to indicate that it is a parameter available to all methods of the class that are used by the object  Variables starting are called Instance Variables  They are available to ALL methods within the class 1 class Account 2 def initialize(balance) = balance 4 end 5 end (c) 2012 Ophir Frieder et al

Class Instantiation  You can instantiate an object of the Account class the same way you create new strings and arrays: bob = Account.new(10.00) (Note: You did NOT have to define a Method called “new”. That is done for you by Ruby.) (c) 2012 Ophir Frieder et al

Class Instantiation  The parameter passed in the parenthesis will become the initial balance of Bob’s account bob = Account.new(10.00) (c) 2012 Ophir Frieder et al

Data and Methods  Now that we know how to define the Account class, we should consider its functionality:  What variables do we need?  What methods would be useful?  No class needs particular variables and methods  The constructor is the exception to this rule  Classes are used to group functionality and data associated with it in one compartmentalized structure  Methods and variables are dictated by this goal (c) 2012 Ophir Frieder et al

Data and Methods  The Account class could use more variables to store information such as:  Name  Phone number  Social security number  Minimum required balance (c) 2012 Ophir Frieder et al

Example 9.3: Account Version #2 1 class Account 2 def initialize(balance,name,phone_number) = balance = name = phone_number 6 end 7 end We insert two new variables to the class constructor (c) 2012 Ophir Frieder et al

Example 9.3: Account Version #2 1 class Account 2 def initialize(balance, name, phone_number) = balance = name = phone_number 6 end 7 end name and phone_number will help make each instantiation unique (c) 2012 Ophir Frieder et al

Data and Methods  New instantiation of an object from the Account class: bob = Account.new(10.00, "Bob", )  Regretfully, there is absolutely nothing we can do with this class, except for instantiating new objects  It would be useful to have some real functionality (i.e., being able to withdraw and deposit) (c) 2012 Ophir Frieder et al

Example 9.4: Account Version #3 1 class Account 2 def initialize(balance, name, phone_number) = balance = name = phone_number 6 end 7 8 def deposit(amount) 9 # code 10 end def withdraw(amount) 13 # code 14 end 15 end (c) 2012 Ophir Frieder et al

1 class Account 2 def initialize(balance, name, phone_number) = balance = name = phone_number 6 end 7 8 def deposit(amount) 9 # code 10 end def withdraw(amount) 13 # code 14 end 15 end We added two new methods, but they don’t do anything yet Need to implement these two methods (c) 2012 Ophir Frieder et al

Data and Methods: Implementing Methods  Once the details of the Account class are finalized, a programmer can use it without knowing the code  They only need to know: Data needed to initialize the class Data needed for each method in the class (c) 2012 Ophir Frieder et al

Example 9.5: Account Version #4 1 class Account 2 def initialize(balance, name, phone_number) = balance = name = phone_number 6 end 7 8 def deposit(amount) += amount 10 end def withdraw(amount) -= amount 14 end 15 end (c) 2012 Ophir Frieder et al

1 class Account 2 def initialize(balance, name, phone_number) = balance = name = phone_number 6 end 7 8 def deposit(amount) += amount 10 end def withdraw(amount) -= amount 14 end 15 end Add the value of the parameter passed to the and store the result (c) 2012 Ophir Frieder et al

Now, initialize the classes to use these methods: irb(main):003:0> require ’account_4.rb’ => true irb(main):004:0> mary_account = Account.new(500, "Mary", ) => # irb(main):005:0> mary_account.deposit(200) => 700 irb(main):006:0> mary_account => # (c) 2012 Ophir Frieder et al

irb(main):003:0> require ’account_4.rb’ => true irb(main):004:0> mary_account = Account.new (500, "Mary", ) => # irb(main):005:0> mary_account.deposit(200) => 700 irb(main):006:0> mary_account => # We import definitions using the require command Mary’s account starts with $500 Deposit method adds more money to the balance (c) 2012 Ophir Frieder et al

Data and Methods: Implementing Methods Now, let’s create a method to make the output simple: Example 9.6: Display method 1 def display() 2 puts "Name: " 3 puts "Phone Number: " 4 puts "Balance: " 5 end (c) 2012 Ophir Frieder et al

Data and Methods: Implementing Methods  Let’s use the new display method to output the account data in the objects: bob_account = Account.new(500, "Bob", ) mary_account = Account.new(500, "Mary", ) bob_account.withdraw(200) mary_account.deposit(200) bob_account.display() mary_account.display() (c) 2012 Ophir Frieder et al

Data and Methods: Implementing Methods  We will move money from Bob to Mary’s account:  Two methods are called: withdraw & deposit bob_account = Account.new(500, "Bob", ) mary_account = Account.new(500, "Mary", ) bob_account.withdraw(200) mary_account.deposit(200) bob_account.display() mary_account.display() (c) 2012 Ophir Frieder et al

Data and Methods: Implementing Methods  We could make a method that does both at the same time, but this would mean the method calls two different instances (objects) of the same class  A method can call multiple different instances of the same class by passing objects as parameters into the method  In our case, we need two instances of the same class, so we will transfer one as a parameter (c) 2012 Ophir Frieder et al

Example 9.7: Transfer Method How to pass in the object: 1 def transfer(amount, target_account) -= amount 3 target_account.deposit(amount) 4 end None of our defined methods returned a value to the invoking statement. To obtain this value, a method must be defined that returns a value. (c) 2012 Ophir Frieder et al

Example 9.8: Status Method The implementation for our method: 1 def status 2 3 end The return construct returns the value to the invoking statement. Because there is no local overriding parameter the global value is accessed. (c) 2012 Ophir Frieder et al

Summary  Classes can be created by a definition process via the constructor  Classes are meant to group data and methods together  The process of instantiating objects creates compartmentalized objects with their data  Once an object has been created, it abstracts the details away from the program that uses it  You can use an object without seeing the details of that object directly (c) 2012 Ophir Frieder et al