40 lines
1.3 KiB
Matlab
40 lines
1.3 KiB
Matlab
classdef Rectangle < Shape & ColorMixin
|
|
properties
|
|
Length, Width
|
|
end
|
|
|
|
methods
|
|
function obj = Rectangle(l,w)
|
|
obj@Shape("Rectangle")
|
|
obj@ColorMixin("red")
|
|
obj.Length = l;
|
|
obj.Width = w;
|
|
obj.Area = obj.CalculateArea;
|
|
end
|
|
|
|
function area = CalculateArea(obj)
|
|
area = obj.Length*obj.Width;
|
|
obj.Area = area;
|
|
end
|
|
|
|
function Display(obj)
|
|
Display@Shape(obj)
|
|
fprintf("\tLength = %f\n", obj.Length);
|
|
fprintf("\tWidth = %f\n", obj.Width);
|
|
end
|
|
|
|
function Draw(obj)
|
|
Draw@Shape(obj);
|
|
% Draw a rectangle
|
|
rectangle('Position',[0 0 obj.Length obj.Width], ...
|
|
'FaceColor', obj.Color);
|
|
% Draw length text
|
|
text(obj.Length / 2, 0, sprintf("l = %g", obj.Length), HorizontalAlignment="center", VerticalAlignment="bottom");
|
|
% Draw width text
|
|
text(obj.Length, obj.Width/2, sprintf("w = %g", obj.Width), Rotation=90, HorizontalAlignment="center", VerticalAlignment="bottom");
|
|
% Draw area text
|
|
text(obj.Length/2, obj.Width/2, sprintf("A = %g", obj.Area), HorizontalAlignment="center", VerticalAlignment="middle");
|
|
end
|
|
end
|
|
end
|