Q:

Construct a class named Money that has 5 data members for dollars, quarters, dimes, nickels, and pennies

0

Construct a class named Money that has 5 data members for dollars, quarters, dimes, nickels, and pennies. Include an ordinary function equivtotal that will calculate and return the equivalent total of the properties in an object (e.g., 5 dollars, 7 quarters, 3 dimes, 0 nickels and 6 pennies is equivalent to 7.11). Overload the disp function to display the properties. 

All Answers

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

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now