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.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
Lecture 5: Interfaces.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
Fall 2006AE6382 Design Computing1 Object Oriented Programming in Matlab Intro to Object- Oriented Programming (OOP) An example that creates a ASSET class.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Object-Oriented PHP (1)
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSSE501 Object-Oriented Development
Review of C++ Programming Part II Sheng-Fang Huang.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Chapter 8 More Object Concepts
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Introduction to Object-Oriented Programming
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 12 Support for Object oriented Programming.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Programming in Java CSCI-2220 Object Oriented Programming.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
M1G Introduction to Programming 2 5. Completing the program.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
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.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
OOP: Creating a Class Topics More OOP concepts
Scripts & Functions Scripts and functions are contained in .m-files
Java Programming Language
Presentation transcript:

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 ASSET class to subclasses of STOCKS, BONDS and SAVINGS. Learning Objectives We’re now going to introduce you to some advanced computing concepts, and we’ll illustrate a simple application that creates a new data type called an ASSET with subtypes. Topics

Fall 2006AE6382 Design Computing2 Matlab’s Built-in Classes Matlab’s built-in classes: –others in additional Toolboxes You can define your own Matlab classes: –data structure (the fields) is a Matlab struct –methods (functions) are stored in special directory that is a subdirectory of a directory in the Matlab path –a constructor is a method that is used to create an object (an instance of a class) charNUMERICjava class cellfunction handle int8, uint8, int16, uint16, int32, uint32 singledouble sparse user class structure ARRAY

Fall 2006AE6382 Design Computing3 Let’s create a Matlab class called ASSET What is an ASSET? –it is an item with a monetary value –examples could be a stock, or a bond, or a savings account –it could also be a physical item with a monetary value (car, house) –each asset has a different way of defining its monetary value Properties: descriptor date current_value ASSET class Inherited Fields: descriptor date current_value STOCK Fields: num_shares share_price asset STOCK class Inherited Fields: descriptor date current_value BOND Fields: int_rate asset BOND class Inherited Fields: descriptor date current_value SAVINGS Fields: int_rate asset SAVINGS class Inherited Fields: descriptor date current_value PROP Fields: mort_rate asset PROP class Superclass (Parent) Subclass (Child)

Fall 2006AE6382 Design Computing4 Creating the Object: Constructors An object must first be created using the “definition” provided in the Class (this is called instantiation) –this is like defining a new double variable (e.g., A(3,1)=5.4) –the created object now has access to all the methods that are defined in the class from which it was created –OOP languages have sophisticated ways to keep these methods and data known only inside the object or shared with one or more other classes of objects (Matlab is very simple as we’ll see later) –we’ll need a special method called a constructor to create an object (all classes must define a constructor method) –in some OOP languages, you must also provide a destructor method to remove an object when you’re done

Fall 2006AE6382 Design Computing5 ASSET Constructor function a=asset(varargin) % ASSET constructor function for asset object % Use: a = asset(descriptor, current_value) % Fields: % descriptor = string % date = date string (automatically set to current) % current_value = asset value ($) switch nargin case 0 % no arguments: create a default object a.descriptor = 'none'; a.date = date; a.current_value = 0; a = class(a,'asset'); % this actually creates the object case 1 % one arg means return same if (isa(varargin{1},'asset')) a = varargin{1}; else error ('ASSET: single argument must be of asset class.') end case 2 % create object using specified values a.descriptor = varargin{1}; a.date = date; a.current_value = varargin{2}; a = class(a,'asset'); % create the object otherwise error('ASSET: wrong number of arguments.') end a is a structure where: a.date=date string a is a structure where: a.date=date string varargin is a cell array with all the nargin arguments All constructors must handle these 3 cases Polymorphism: different use of class() in constructor

Fall 2006AE6382 Design Computing6 Displaying the Object… We need to add a method to display the object –Matlab always calls display for this purpose –we must add a method in our ASSET class… function display(a) % DISPLAY(a) displays an ASSET object str = sprintf('Descriptor: %s\nDate: %s\nCurrent Value:%9.2f',... a.descriptor, a.date, a.current_value); disp(str) >> a=asset('My asset', 1000) Descriptor: My asset Date: 29-Nov-2001 Current Value: Example showing how display() is used if the semicolon is omitted in a command line There are lots of possible variations depending on how you want the display to look…

Fall 2006AE6382 Design Computing7 Comments Every class must define a constructor and a display method (or it must be inherited from a superclass) –by convention, the constructor should handle at least the following 3 situations: –create an empty object if no arguments are supplied, or –make a copy of the object if one is supplied as the only argument. or –create a new object with specified properties. The class() function (method) is polymorphic: –class(A) if used anywhere will return the type of variable A –class(a,’asset’) is only allowed inside a constructor and it tags a as being of class (type) ‘asset’ –class(a,’asset’,parent1) tags a as above and also it inherits methods and fields from a parent1 object (class).

Fall 2006AE6382 Design Computing8 Examples with our new class Try the following examples: –Make sure you have created a subdirectory in the Matlab work directory (or a directory in the Matlab path) –copy the functions we’ve described into this directory –type in the examples at the Command prompt… >> a=asset('My asset', 1000) Descriptor: My asset Date: 10-Oct-2002 Current Value: >> b=asset(a); >> whos Name Size Bytes Class a 1x1 418 asset object b 1x1 418 asset object Grand total is 92 elements using 2428 bytes >> c=asset Descriptor: none Date: 10-Oct-2002 Current Value: 0.00 Creates an empty object Creates an object Creates a copy of a

Fall 2006AE6382 Design Computing9 Our Class has no Methods yet… >> a+b ??? Error using ==> + Function '+' not defined for variables of class 'asset'. >> x=a(1); >> x Descriptor: My asset Date: 29-Nov-2001 Current Value: >> class(x) ans = asset >> whos Name Size Bytes Class a 1x1 418 asset object b 1x1 418 asset object c 1x1 410 asset object x 1x1 418 asset object Grand total is 88 elements using 1664 bytes Arithmetic operations are not defined Using an index simply references the entire object (x is a copy of a)

Fall 2006AE6382 Design Computing10 function val = get(a, prop_name) % GET: Get asset properties from specified object 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 'de' val = a.descriptor; case 'da' val = a.date; case 'cu' val = a.current_value; otherwise error(['GET: ',prop_name,' is not a valid asset property.']); end Listing (getting) Property Values from Object We will want to extract property values from an object Must check for valid input Note the tricky way to remove all nonletters in the string ! You can test for some or all of the property name string (shortens how much the user must type) GET is a “standard” name

Fall 2006AE6382 Design Computing11 Changing Property Values in an Object W must be able to change the values of some of the properties in the object: –we’ll create a method (function) called get() for this –it will be part of the asset class located in directory function a = set(a,varargin) % SET: set asset properties and return updated object. More than one % property can be set but must provide: ‘prop_name’, value pairs if rem(nargin,2) ~= 1 error('SET: prop_name, values must be provided in pairs.') end 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 'de' a.descriptor = value; case 'da' a.date = value; case 'cu' a.current_value = value; otherwise error('SET: invalid prop_name provided.') end NOTE: you should include code to check for proper values as well.

Fall 2006AE6382 Design Computing12 Example using get and set Try the following examples… –Make sure you have created a subdirectory in the Matlab work directory (or a directory in the Matlab path) –copy the functions we’ve described into this directory –type in the examples at the Command prompt… >> a Descriptor: My asset Date: 10-Oct-2002 Current Value: >> get(a,'Descriptor') ans = My asset >> a=set(a,'CurrentValue',2000) Descriptor: My asset Date: 10-Oct-2002 Current Value: >> a=set(a,'Date',datestr(datenum(date)+1)) Descriptor: My asset Date: 11-Oct-2002 Current Value: Get a property value Set a property value Change date…

Fall 2006AE6382 Design Computing13 Important Notes We didn’t need a return value using get, but… We MUST show the same object we’re modifying as the return value for set –this is because Matlab does not allow us to modify the values of the arguments in a function and have them returned to the calling workspace!!! –We could use the assignin() function to avoid this problem (see also the evalin() function for another variation on this) –Other OOP languages don’t have this problem… >> a=set(a,'CurrentValue',2000) Descriptor: My asset Date: 29-Nov-2001 Current Value: >> a=set(a,'CurrentValue',2000) Descriptor: My asset Date: 29-Nov-2001 Current Value: set() must return the same object as provided in the argument list This is perhaps the most awkward, inelegant aspect of OOP in Matlab. It is VERY non-standard when compared to Java, C++ or SmallTalk, etc.

Fall 2006AE6382 Design Computing14 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 this new class? –create a subclass of assets called STOCK that will include fields for data specific to a stock asset (see next slide) –the methods for this new subclass will be in subdirectory –we could continue to create subclasses for BOND and PROPERTY types of assets, each with their own extended data values and perhaps new methods.

Fall 2006AE6382 Design Computing15 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 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 ASSET & STOCK classes Summary