Object-oriented design (OOD) uses objects to design programs. To use an object,
ID: 3562078 • Letter: O
Question
Object-oriented design (OOD) uses objects to design programs. To use an object, you do not need to know the makeup of the object. This information might be hidden from you. However, you need to know the functions of the object (such as buttons) and how to use them. You can use an object by itself or with other objects, but you cannot modify the functions of the object. To create objects, you first need to learn to create classes; to know what type of classes to create, you need to know what an object stores and what operations are needed to manipulate an objects data.
The aim of OOD is to build software from software components called classes and to use the various methods provided by those classes. In OOD, you first identify the object, then identify the relevant data, and then identify operations needed to manipulate that data. One basic principle of OOD is encapsulation
Explanation / Answer
Object-Oriented Program Design
The object-oriented program design involves:
Identifying the components of the system or application that you want to build
Analyzing and identifying patterns to determine what components are used repeatedly or share characteristics
Classifying components based on similarities and differences
After performing this analysis, you define classes that describe the objects your application uses.
Classes and Objects
A class describes a set of objects with common characteristics. Objects are specific instances of a class. The values contained in an object's properties are what make an object different from other objects of the same class (an object of class double might have a value of 5). The functions defined by the class (called methods) are what implement object behaviors that are common to all objects of a class (you can add two doubles regardless of their values).
Using Objects in MATLAB Programs
The MATLAB language defines objects that are designed for use in any MATLAB code. For example, consider the try/catch programming construct.
If the code executed in the try block generates an error, program control passes to the code in the catch block. This behavior enables your program to provide special error handling that is more appropriate to your particular application. However, you must have enough information about the error to take the appropriate action.
MATLAB provides detailed information about the error by passing an MException object to functions executing the try/catch blocks.
The following try/catch blocks display the error message stored in an MException object when a function (surf in this case) is called without the necessary arguments:
In this code, ME is an object of the MException class, which is returned by the catch statement to the function's workspace. Displaying the value of the object's message property returns information about the error (the surf function requires input arguments). However, this is not all the information available in the MException object.
You can list the public properties of an object with the properties function:
Objects Organize Data
The information returned in an MException object is stored in properties, which are much like structure fields. You reference a property using dot notation, as in ME.message. This reference returns the value of the property. For example,
shows that the value of the message property is an array of class char (a text string). The stack property contains a MATLAB struct:
You can simply treat the property reference, ME.stack as a structure and reference its fields:
The file field of the struct contained in the stack property is a character array:
You could, for example, use a property reference in MATLAB functions:
Object properties can contain any class of value and can even determine their value dynamically. This provides more flexibility than a structure and is easier to investigate than a cell array, which lacks fieldnames and requires indexing into various cells using array dimensions.
Objects Manage Their Own Data
You could write a function that generates a report from the data returned by MException object properties. This function could become quite complicated because it would have to be able to handle all possible errors. Perhaps you would use different functions for different try/catch blocks in your program. If the data returned by the error object needed to change, you would have to update the functions you have written to use the new data.
Objects provide an advantage in that objects define their own operations. A requirement of the MException object is that it can generate its own report. The methods that implement an object's operations are part of the object definition (i.e., specified by the class that defines the object). The object definition might be modified many times, but the interface your program (and other programs) use does not change. Think of your program as a client of the object, which isolates your code from the object's code.
To see what methods exist for MException objects, use the methods function:
You can use these methods like any other MATLAB statement when there is an MException object in the workspace. For example:
Objects often have methods that overload (redefined for the particular class of the object) MATLAB functions (e.g., isequal, fieldnames, etc.). This enables you to use objects just like other values. For example, MException objects have an isequal method. This method enables you to compare these objects in the same way you would compare variables containing doubles. If ME and ME2 are MException objects, you can compare them with this statement:
However, what really happens in this case is MATLAB calls the MException isequal method because you have passed MException objects to isequal.
Similarly, the eq method enables you to use the == operator with MException objects:
Of course, objects should support only those methods that make sense. For example, it would probably not make sense to multiply MException objects so the MException class does not implement methods to do so.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.