Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Lecture 5.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Programming with MATLAB
Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Solving ODEs UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Solving Linear Equations UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
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.
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Regression UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Introduction to C and CMex E177 April 1, Copyright , Andy Packard. This work is licensed under the.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
Inheritance in Matlab Classes E177 February 14, Copyright , Andy Packard. This work is licensed.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
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.
Guards1. 2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Discussion 4 eecs 183 Hannah Westra.
OOP: Creating a Class Topics More OOP concepts
Introduction to Java E177 April 29, me. berkeley
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Computing with C# and the .NET Framework
Scripts & Functions Scripts and functions are contained in .m-files
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
Class Info E177 January 22, me. berkeley
Regular Expressions: Searching strings for patterns April 24, 2008 Copyright , Andy Packard and Trent Russi. This work is licensed under the Creative.
Andy Wang Object Oriented Programming in C++ COP 3330
E177, Graphics Objects in Matlab February 26, me
@ReferAssignGetSet February 21, me. berkeley
Logical Operations In Matlab.
Class Intro/TDD Intro 8/23/2005
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
E177, Reference/Assignment for Classes: Intro February 12, Copyright , Andy Packard. This work.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Andy Wang Object Oriented Programming in C++ COP 3330
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Access methods: get and set The public properties of the object are –Retrieved using get –Modified using set, which can control changes that are allowed Properties have names to identify them, so a natural syntax is PropertyValue = get(Object,PropertyName) set(Object,PropertyName,PropertyValue) For the e77poly class, let’s make Symbol a public property –Its value is the single character symbol used to represent the indeterminate variable of the polynomial –Since the user will be able to control its value, we won’t allow binary operations (like + and * ) unless the symbols match.

Writing get is pretty easy function value = get(p,PropName) switch PropName case 'Symbol' value = p.symbol; otherwise error('Unknown property'); end The convention (we’ll see next week) in Matlab for value = get(Object) is to return a struct, whose fieldnames are the public properties. Easy to add this in, using nargin. Limited access

Writing set is a little harder Why won’t this work? function set(p,PropName,value) switch PropName case 'Symbol‘ if isa(value,’char’) & isscalar(value) p.symbol = value; else error(’Invalid Symbol value’); end otherwise error('Unknown property'); end Controlling what is allowed

Writing set is a little harder Without some trickery, the command >> set(a,PropName,Value) will not work as intended. In other words, after executing >> initvalue = get(a,PropName); >> newvalue = some_new_value; >> set(a,PropName,newvalue) the result of >> isequal(get(a,PropName),newvalue) is false, and the result of >> isequal(get(a,PropName),initvalue) is true. Why? (week 2/3): variables are passed to functions by value, changing them in the function only changes the function’s copy.

Accessing other workspaces Two built-in Matlab commands to circumvent this by –Allowing assignment of variables in other workspaces –Within a function, giving access to the name of an input argument variable in the caller’s workspace Specifically, these two functions are assignin –copy a variable from the current workspace into a different workspace, with a new, given name. inputname –Access the name (in the caller’s workspace) of an input argument variable.

Example with assignin >> >> v1 = [1 -2 3]; >> W = EXAMP1(v1); >> function [B] = EXAMP1(A) assignin(’caller’,’NEWV’,2*A); B = 3*A; BASE WORKSPACE v1: [1 -2 3] NEWV: [2 -4 6] W: [3 -6 9] FUNCTION INSTANCE WS A: [1 -2 3] B: [3 -6 9] function [B] = EXAMP1(A) assignin(’caller’,’NEWV’,2*A); B = 3*A;

Example with inputname >> >> fred = [2 3]; >> joe = [5 5]; >> W = EXAMP2(fred,joe); >> function [C] = EXAMP2(A,B) D = inputname(1); E = inputname(2); C = A + B; BASE WORKSPACEFUNCTION INSTANCE WS D: ’fred’ E: ’joe’ C: [7 8] function [C] = EXAMP2(A,B) D = inputname(1); E = inputname(2) C = A + B; fred: [2 3] joe: [5 5] W: [7 8] A: [2 3] B: [5 5]

Example using both Look at the function incr.m function incr(a) assignin(’caller’,inputname(1),a+1); It increments, without an output argument! >> var = 7; >> incr(var); >> var var = 8 >>

Fixing set uses this idea Here is one working version of set function set(p,PropName,value) switch PropName case 'Symbol' p.symbol = value; % need error check otherwise error('Unknown property'); end assignin(’caller’,inputname(1),p); Could also pass back an argument, or give user the option to do either…

A version of set allowing for either… function pnew = set(p,PropName,value) switch PropName case 'Symbol' p.symbol = value; % need error check otherwise error('Unknown property'); end if nargout==0 assignin(’caller’,inputname(1),p); else pnew = p; end

Nearly finished: Methods for e77poly Constructor, e77poly converter and display methods –double, converts a polynomial to a row vector of coefficients –char, converts a polynomial to a formatted string –display, for expressions that result in an e77poly object Operator overloaded methods –plus(A,B) for A+B –minus(A,B) for A-B –uplus(A) for +A –uminus(A) for -A –mtimes(A,B) for A*B Overloaded methods –roots, polyval, polyder, polyint Access methods ( get, set e77poly.m double.m char.m display.m plus.m minus.m uplus.m uminus.m mtimes.m roots.m polyval.m polyder.m polyint.m get.m set.m