[MATLAB logo] Exploiting MATLAB's Data Structures

Solution #8 - Roman calculator

The object oriented extensions built into MATLAB allow you to overload common syntax such as a + b to do anything you want! In this problem, you will create a roman object that can add two roman numbers.

The object oriented extensions in MATLAB hinge on the concept of an object variable. object variables are like normal variables that live in the workspace except that they can have special m-files defined for them called methods. When an object variable is used as an input argument to a function, MATLAB will look for and find a method with the same name as the function if it exists.

Methods must be placed into a directory with the same name as the class of the object is goes with preceeded by an '@' sign. For example, the methods for the roman object you will create should live in a directory called @roman.

You can overload the operators involved in expression as well by creating methods with special names. For instance to overload the '+' operator you must create a method with the name plus.m that takes two input arguments and returns a single result (for the list of special names see help datatypes).

The constructor method for an object is a method with the same name of as the class. The constuctor's job is to create the object so this is where you must define and initialize the object's fields. An object is first created as a structure and then passed through the class function to turn it into an object:

function obj = myclass(x)
%MYCLASS Constructor for the MYCLASS object.
%  OBJ = MYCLASS(X) returns a MYCLASS object with value X.

obj.dat = x;
obj = class(obj,'myclass');

This method, when placed into a directory called @myclass, can be used to create a myclass variable.

Goal: Create a constructor and overloaded method for plus.

Key concepts: object oriented programming, overloading mathematical syntax, constructors, method directories, creating directories with Windows 95

Key functions: class, romancalc, methods

Solution

  1. Run romancalc
    romancalc
  2. Press some buttons to verify that it does nothing useful
    errors occur
  3. Create a directory named @roman in the minicourse directory
    !mkdir @roman
    or use the Windows 95 Explorer.
  4. Create the overloaded constructor, roman(v). The roman object should have one field, val, that contains the decimal value of the object. A few other methods have already been created for you that use this field to do their work (you can use methods roman to see their names)
    function obj = roman(v)
    %ROMAN constructor
    if isa(v,'roman')
    obj = v;
    return
    end

    obj.val = v;
    obj = class(obj,'roman');
  5. Create the function plus to add two roman numerals
    function c = plus(a,b)
    %PLUS for ROMAN objects
    a = roman(a);
    b = roman(b);
    c = roman(0);
    c.val = a.val + b.val;
  6. Optionally, create the function mtimes to multiply two roman numerals
    function c = mtimes(a,b)
    %MTIMES for ROMAN objects
    a = roman(a);
    b = roman(b);
    c = roman(0);
    c.val = a.val * b.val;
  7. Try your object out at the command line.
    a = roman(5);
    b = roman(3)
    a + b
    a * b
    a + 2
    5 + b
  8. Now try using romancalc again.
    The calulator is working for + and *.

Copyright © 1997 The MathWorks, Inc.
clay@mathworks.com
$Revision$ $Date$