General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/16/2009
James Atlas Overview Review Review Inheritance Cell Arrays Polymorphism Polymorphism What is it? Why use it? How does it work in Matlab?
James Atlas Inheritance The is a relationship The is a relationship Inheritance Hierarchy Inheritance Hierarchy state (properties) behavior (methods) Superclass Superclass Subclass Subclass Animal ReptileBird
James Atlas Cell Arrays Normal array Normal array zeros(1,5) is 5 doubles [ ] is 4 doubles Cell array Cell array { } is 4 cells, each with a 1x1 array of double values inside
James Atlas What is a cell? A cell is like a window into a room, and inside the room can be data of any size A cell is like a window into a room, and inside the room can be data of any size A cell array is like a hallway with many doors A cell array is like a hallway with many doors Student age=19 name=‘Joe’ credits=78 ‘hello’ normal arraycell array
James Atlas Polymorphism: What is It?
James Atlas Polymorphism: What is It? One name, many forms One name, many forms Capability to perform different actions based on the object instance
James Atlas Polymorphism: What is It? One name, many forms One name, many forms Capability to perform different actions based on the object instance Each band member is a Musician Each band member is a Musician We could tell all band members to Play Play for a drummer would perform a different action than Play for a guitarist
James Atlas Polymorphism: Why use it? Easily extensible systems Easily extensible systems Add new features with low impact on system
James Atlas Polymorphism: Why use it? Easily extensible systems Easily extensible systems Add new features with low impact on system For example, we are hosting American Idol for Musicians For example, we are hosting American Idol for Musicians In Season 1 we have guitarists, drummers, and pianists In Season 2 we add keyboardists, trumpeters and bagpipers
James Atlas Polymorphism: Why use it? Easily extensible systems Easily extensible systems Add new features with low impact on system For example, we are hosting American Idol for Musicians For example, we are hosting American Idol for Musicians In Season 1 we have guitarists, drummers, and pianists In Season 2 we add keyboardists, trumpeters and bagpipers We don’t need to change the competition format or hosting protocol!
James Atlas Polymorphism: How does it work in Matlab? Overriding Overriding Matlab constructs Matlab constructs Methods (the behavior) Classes Abstract tag
James Atlas Shape.m Class classdef Shape properties properties center_x center_x center_y center_y color color end end methods (Abstract) methods (Abstract) result = area(shape) result = area(shape) handle = draw(shape) handle = draw(shape) end endend
James Atlas Circle.m Class classdef Circle < Shape properties properties radius radius end end methods methods function circle = Circle(center_x, center_y, radius, color) function circle = Circle(center_x, center_y, radius, color) circle.center_x = center_x; circle.center_x = center_x; circle.center_y = center_y; circle.center_y = center_y; circle.radius = radius; circle.radius = radius; circle.color = color; circle.color = color; end end...endend
James Atlas Circle.m Class (cont’) classdef Circle < Shape methods methods... function handle = draw(circle) function handle = draw(circle) handle = rectangle('Position', [... handle = rectangle('Position', [... circle.center_x - circle.radius,... circle.center_x - circle.radius,... circle.center_y - circle.radius,... circle.center_y - circle.radius,... 2 * circle.radius,... 2 * circle.radius,... 2 * circle.radius],... 2 * circle.radius],... 'Curvature', [1 1], 'FaceColor', circle.color); 'Curvature', [1 1], 'FaceColor', circle.color); end end...endend
James Atlas Circle.m Class (cont’) classdef Circle < Shape methods methods... function result = area(circle) function result = area(circle) result = pi * circle.radius * circle.radius; result = pi * circle.radius * circle.radius; end endendend
James Atlas Example: Shape sorter Create rectangle and triangle Create rectangle and triangle Create a cell array of shapes Create a cell array of shapes Update each shape’s x and y center Update each shape’s x and y center Draw shapes and then put them in order by area Draw shapes and then put them in order by area Animate swaps Animate swaps