Q:

Create the Rectangle class from this chapter. Add a function to overload the gt (greater than) operator. Instantiate at least two objects and make sure that your function works

0

 Create the Rectangle class from this chapter. Add a function to overload the gt (greater than) operator. Instantiate at least two objects and make sure that your function works.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Rectangle.m

classdef Rectangle

 

 properties

 len = 0;

 width = 0;

 end

 

 methods

 

 function obj = Rectangle(l, w)

 if nargin == 2

 obj.len = l;

 obj.width = w;

 end

 end

 

 function outarg = rectarea(obj)

 outarg = obj.len * obj.width;

 end

 

 function disp(obj)

 fprintf('The rectangle has length %.2f', obj.len)

 fprintf(' and width %.2f\n', obj.width)

 end

 

 function out = lt(obja, objb)

 out = rectarea(obja) < rectarea(objb);

 end

 

 function out = gt(obja, objb)

 out = rectarea(obja) > rectarea(objb);

 end

 end

end

>> rect1 = Rectangle(3, 5);

>> rect2 = Rectangle(2, 10);

>> rect1 > rect2

ans =

 0

>> gt(rect2, rect1)

ans =

 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now