Construct a class named Money that has 5 data members for dollars, quarters, dimes, nickels, and pennies
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:11| Question number:10.11
All Answers
total answers (1)
Money.m
classdef Money
properties
dollars = 0;
quarters = 0;
dimes = 0;
nickels = 0;
pennies = 0;
end
methods
function obj = Money(d, q, di, n, p)
if nargin == 5
obj.dollars = d;
obj.quarters = q;
obj.dimes = di;
obj.nickels = n;
obj.pennies = p;
end
end
function total = equivtotal(obj)
total = obj.dollars + obj.quarters*.25 + ...
obj.dimes*.1 + obj.nickels*.05 + obj.pennies*.01;
end
function disp(obj)
fprintf('The total dollar amount is $%.2f\n', ...
equivtotal(obj))
end
end
end
>> amt = Money
amt =
The total dollar amount is $0.00
>> amount = Money(2, 1, 3, 4, 3)
amount =
The total dollar amount is $2.78
>>
need an explanation for this answer? contact us directly to get an explanation for this answer