Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
Functions M-file with the function keyword. First line: –[myout1, myout2,..] = myfunction(a, b,…) Function called by name of m-file, not function name,
Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 07: Lecture Notes (CSIT 104) 1111 Exploring Microsoft Office Excel 2007 Chapter 7 Data Consolidation, Links, and Formula Auditing.
Fall 2006AE6382 Design Computing1 Object Oriented Programming in Matlab Intro to Object- Oriented Programming (OOP) An example that creates a ASSET class.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Using Microsoft Outlook: Basics. Objectives Guided Tour of Outlook –Identification –Views Basics –Contacts –Folders –Web Access Q&A.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
Introduction to Matlab & Data Analysis
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
CSC 142 Computer Science II Zhen Jiang West Chester University
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
Introduction to Exception Handling and Defensive Programming.
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 Reverse a String iPhone/iPad, iOS Development Tutorial.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Tableau Server URL Parameterization and Limits. Background This short set of material covers how Tableau Server Views can be invoked via URLs while passing.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
OOP: Creating a Class Topics More OOP concepts
Final and Abstract Classes
Database application MySQL Database and PhpMyAdmin
CS1371 Introduction to Computing for Engineers
Scripts & Functions Scripts and functions are contained in .m-files
Understanding Inheritance
Topics Introduction to File Input and Output
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Final and Abstract Classes
Topics Introduction to File Input and Output
Using Microsoft Outlook: Outlook Support Number
Presentation transcript:

Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend the ASSET class to a subclass of STOCK Consider others like: BONDS & SAVINGS. Learning Objectives We will continue exploring objects and will construct a subclass called STOCK from the parent ASSET class Topics

Fall 2006AE6382 Design Computing2 Creating a Subclass Our ASSET class is of limited use because we will need to use it to describe many different kinds of assets –We could simply keep adding more properties to cover all situations we might encounter… –but this makes for very messy objects with lots of unused or unknown (and therefore dangerous) data –A more elegant way to handle this problem is to create a subclass of ASSET for each type of asset we might work with. Consider a STOCK subclass of ASSET –We will want to include the number of shares and the share price –We’ll also need to update the CurrentValue property in the parent ASSET object Properties: descriptor date current_value ASSET class Inherited Fields: descriptor date current_value STOCK Fields: num_shares share_price STOCK class

Fall 2006AE6382 Design Computing3 Creating the STOCK Subclass STOCK will be a subclass (child) of ASSET Create directory in WORK for class files function s = stock(varargin) % STOCK Stock class constructor % s=stock(descriptor, num_shares, share_price) switch nargin case 0 % no arguments so create default object s.num_shares = 0; s.share_price = 0; a = asset('none',0); % create dummy object to define parent class s = class(s,'stock',a) % create STOCK object as subclass of ASSET case 1 % single argument so make copy if (isa(varargin{1},'stock')) s = varargin{1}; else error('STOCK: Input argument is not a STOCK object') end case 3 s.num_shares = varargin{2}; s.share_price = varargin{3}; current_value = s.num_shares.* s.share_price; a = asset(varargin{1},current_value); % create parent object s = class(s,'stock',a); % create STOCK object as child of parent otherwise error('STOCK: Wrong number of input arguments.') end define the STOCK properties create a “parent” object now create the STOCK “child” object Note: parent object must be created with specified property values

Fall 2006AE6382 Design Computing4 STOCK display Method Just like display() for ASSET function display(s) % DISPLAY(a) displays a STOCK object display(s.asset) % display properties in parent object str = sprintf('Number of shares: %g\nShare Price: %3.2f\n',... s.num_shares, s.share_price); disp(str) First display parent property values Then display subclass property values NOTE: the 3 rd line shows how you access properties in another object ( obj_name.obj_type )

Fall 2006AE6382 Design Computing5 STOCK get Method function val = get(s, prop_name) % GET: Get STOCK properties from specified object if nargin==1 % list object’s properties if only object is given disp('STOCK properties:') disp(' Descriptor, Date, NumberShares, SharePrice, CurrentValue') return end if ~ischar(prop_name), error('GET: prop_name must be string.'), end prop_name = lower(prop_name(isletter(prop_name))); %remove nonletters if (length(prop_name) < 2), error('GET: prop_name must be at least 2 chars.'), end switch prop_name(1:2) case 'nu' val = s.num_shares; case 'sh' val = s.share_price; case 'de' val = get(s.asset,'descriptor'); % call ASSET GET method case 'da' val = get(s.asset,'date'); % call ASSET GET method case 'cu' val = get(s.asset,'current_value'); % call ASSET GET method otherwise error(['GET: ',prop_name,' is not a valid asset property.']); end Display object properties get STOCK properties get parent ASSET properties using its SET method

Fall 2006AE6382 Design Computing6 STOCK set Method Must define which properties can be set and which will be computed Providing error checking is IMPORTANT function s = set(s,varargin) % SET: set STOCK properties and return updated object. % Use: xyz=stock(xyz,'prop_name', value,...); % (Note: you must assign results to same or new object) % stock(xyz) returns list of settable properties. if nargin==1 % list settable properties if only object is given disp('STOCK properties:') disp(' Descriptor, Date, NumberShares, SharePrice, CurrentValue') return end if rem(nargin,2) ~= 1 error('SET: prop_name, values must be provided in pairs.') end Display object properties if only object is given Make sure that proper number of arguments are provided… continued on next slide

Fall 2006AE6382 Design Computing7 STOCK set Method (cont’d) for k=2:2:nargin-1 prop_name = varargin{k-1}; if ~ischar(prop_name), error('SET: prop_name must be string.'), end prop_name = lower(prop_name(isletter(prop_name))); %remove nonletters value = varargin{k}; switch prop_name(1:2) case 'nu' s.num_shares = value; case 'sh' s.share_price = value; case 'de' s.asset = set(s.asset,'descriptor',value); %set parent property otherwise error('SET: invalid prop_name provided.') end % update current value of stock in parent: value = s.num_shares.* s.share_price; s.asset = set(s.asset,'CurrentValue',value,'Date',date); Set properties in STOCK object Must compute STOCK value and then set in parent field Set property in parent ASSET object Note that not all properties can or should be settable!

Fall 2006AE6382 Design Computing8 Examples Here we create a STOCK object with initial values and then use the SET method to update the price. >> s1=stock('Intel',25,15) Descriptor: Intel Date: 03-Dec-2001 Current Value: Number of shares: 25 Share Price: >> set(s1); STOCK properties: Descriptor, Date, NumberShares, SharePrice, CurrentValue >> s1=set(s1,'SharePrice', 20) Descriptor: Intel Date: 03-Dec-2001 Current Value: Number of shares: 25 Share Price: Note that SET reports the settable properties for this calling syntax

Fall 2006AE6382 Design Computing9 Example: Changing Property Values Use set to change a property value REMEMBER: you must reassign the output to the object or the change will not be permanent! >> set(s1,'SharePrice', 30) Descriptor: Intel Date: 03-Dec-2001 Current Value: Number of shares: 25 Share Price: >> display(s1) Descriptor: Intel Date: 03-Dec-2001 Current Value: Number of shares: 25 Share Price: Update the share price It is reported in the automatic display when no semicolon is provided But… if you try a subsequent display(), the price was not changed!

Fall 2006AE6382 Design Computing10 Creating the subsref Method In order to be able to access STOCK fields using Matlab’s array indexing or structure field notation, we must define subsref for this class… function b = subsref(s, index) % SUBSREF Define field name indexing for STOCK objects fc = fieldcount(s.asset); %find out how many fields are in parent object switch index.type case '()' if (index.subs{:} <= fc) b = subsref(s.asset,index); else switch index.subs{:} - fc case 1 b = s.num_shares; case 2 b = s.share_price; otherwise error(['SUBSREF: Index must be in range 1 to ',num2str(fc+2)]) end case '.' switch index.subs case 'num_shares' b = s.num_shares; case 'share_price' b = s.share_price; otherwise b = subsref(s.asset,index); end case '{}' error('SUBSREF: Cell array indexing not supported for STOCK objects.') end Each case defines a different type of indexing: array, field, or cell See Matlab documentation for further explanation of this method

Fall 2006AE6382 Design Computing11 Creating the subsasgn Method In order to be able to assign values to STOCK fields using Matlab’s array indexing or structure field notation, we must define subsasgn for this class… function s = subsasgn(s, index, val) % SUBSASGN Define index assignment for STOCK objects fc = fieldcount(s.asset); % get # fields in parent object switch index.type case '()' if (index.subs{:} <= fc) s.asset = subsasgn(s.asset,index,val); else switch index.subs{:} - fc case 1 s.num_shares = val; case 2 s.share_price = val; otherwise error(['SUBSASGN: Index must be in range 1 to ',num2str(fc+2)]) end case '.' switch index.subs case 'num_shares' s.num_shares = val; case 'share_price' s.share_price = val; otherwise s.asset = subsasgn(s.asset,index,val); end case '{}' error('SUBSASGN: Cell array indexing not supported for asset objects.') end Each case defined a different type of indexing: array, field, or cell See Matlab documentation for further explanation of this method

Fall 2006AE6382 Design Computing12 Example: Using subsref We’ll create a STOCK object and then access its properties using array indexing >> s2=stock('oracle',20,35) Descriptor: oracle Date: 03-Dec-2001 Current Value: Number of shares: 20 Share Price: >> s2(1) ans = oracle >> s2(4) ans = 20 >> s2.share_price ans = 35 >> s2{1} ??? Error using ==> stock/subsref SUBSREF: Cell array indexing not supported for STOCK objects. Access the first property using array indexing The 4-th property is the number of shares You can also use the internal field names (if you know them) Note that we didn’t define cell indexing for our STOCK objects

Fall 2006AE6382 Design Computing13 Other Methods for STOCK While these methods provide powerful capabilities for our new objects, there are some other methods we might want to consider… –Methods to convert the data from one form into another convert between different numeric formats like double and int32 this is more important for a simple class like char or double –Method to convert the data into a string or cell array toString is a common OOP method name for this purpose converting to a cell array may be more useful when an object contains many different fields –We will have to create loadobj and saveobj methods if we want to be able to save our objects to a. mat file along with the rest of the workspace

Fall 2006AE6382 Design Computing14 Where to from Here? With only a little modification we can create an entirely new subclass that is based on our STOCK class –We can do this by specifying that our new class will inherit the structure and methods from a parent or superclass (or from several parent classes). –This is called “inheritance” and is an important feature of OOP. Consider a BOND class –It has an identical structure to a STOCK, –But there will be different properties –We will create new display, get and set methods –We will use the set and get methods from ASSET to modify properties in the parent

Fall 2006AE6382 Design Computing15 Summary… OOP has let us create an entirely new data type in Matlab –this is done by creating a class to define this data type –we must also create all the methods (functions) that can be used with this special data type –the internal details are hidden inside the class What else could we do with these new classes? –A portfolio might contain a mix of STOCK, BOND and SAVINGS assest… How can we keep this together? –AGGREGATION allows us to construct a new class whose contents are objects from other classes –See Matlab reference docs for an example of how to do this.

Fall 2006AE6382 Design Computing16 Summary Action Items Review the lecture Perform the exercises See if you can create the BOND class and get it working like we did for the STOCK class. Consider how this might be extended to other applications Learning Objectives OOP concepts STOCK subclasse Other subclasses Aggregation Summary