Sei sulla pagina 1di 3

Lab No.

(5)

Object Oriented Programming Part 2


Function handle (@) This handle is used in calling funtions indirectly. It is used as follows:
handle = @functionname

returns a handle to the specified MATLAB function constructs an anonymous_function and returns a handle to that function. The body of the function, to the right of

Handle = @(arglist) anonymous_function

the parentheses, is a single MATLAB statement or command. arglist is a comma-separated list of input arguments. Execute the function by calling it by means of the function handle, Handle.

Standard set of methods for Matlab Classes: Class constructor: used to create instances of the class. Generally, it should handle 3 possible combinations of input arguments: o No input arguments (nargin==0) o An object of the same class as an input argument o Specific input arguments to creat an object of the class Display: Matlab calls a display method whenever an object is the result of a statement that is not terminated by a semicolon
function display(obj)

Set and get: these are to provide access to an objects data


function value = get.PropertyName(obj) function obj = set.PropertyName(obj,value)

Identifying objects: This can be done using the functions: class (a) returns a string specifying the class name of the object a isa(a,class_name) checks whether a is an object of the specified name

Experiments (Procedures):
1. The ODE program

Re-write the program that solves first order differential equations using Eulers method. This time with a function handle that specifies the RHS of the equation. What if the user does not specify the number of point? Re-write your function to handle such a function call.
2. The date class

Declare a class named date. Its attributes are: day, month and year. a. Over-write the set method such that the day property cant be set to a value <0 b. Over-write the get method such that the day property can be read as You are specifying the day no: (#) c. Over-write the display method such that whenever an object is created/updated, the objects class and properties appear as well.

Homework # 5:
1. The Definite Integral program

The Definite integral of a particular function can be approximated by applying the following formula: ( ) ( ( ) )

a. Write an appropriate Matlab function that would evaluate this formula for a certain function, given the limits of integral (a & b) and the number of points (n). b. Use the function obtained in part (a), to find . Try it with 100

point first, then with 1000. Which is closer to the right answer? What is your observation? c. What if the number of points is not passed as a parameter? Your function should also be able to handle such a call.

2. The date class

Complete the date class such that: a. The constructor of the class accepts the various input combinations specified in page 1 of this sheet: - No input parameters - An object of the class as an input - Specific input parameters b. The set method is defined for all properties with the constraints: - The day should always be a positive integer, less than 31,30, 29 or 28 based on the specified month and year. - The month should always be a positive integer, less than 30. - The year should always be a positive integer. c. The get method is defined for all properties. Whenever it is called, the objects class and the specified property value should be read. d. Add a new method setformat that determines how a date object is displayed, for example, if the user chooses short format (S), a date object should be displayed as: dd/mm/yyyy. If he chooses long format (L), a date object should be displayed as dd-mm-yyyy, where mm is the months name in this case: [Jan, Feb, March..Dec] Hint: you might need to add new properties that specify the type of format to implement this method.

Potrebbero piacerti anche