In: Computer Science
MATLAB CODE
Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies.
PREVIOUS CODE FOR THIS
function value = get_coin_value(coin)
%Set value to be the correct number based on coin.
%For example, if coin == 'q', value = 25
if coin == 'q'
value = 25;
elseif coin == 'd'
value = 10;
elseif coin == 'n'
value = 5;
elseif coin == 'p'
value = 1;
else
value = 0;
end
function total = insert_coins
%your code here
total = 0;
%Loop to keep asking user for coin till total is less than 115 cents
while(total<115)
coin = input('Enter q for quarter, d for dime, n for nickel,p for penny:','s');
total = total + get_coin_value(coin);
end
disp("Your NAU power juice has been dispensed")
end
classdef MyClass < handle properties
publicProp = 1;
end
properties(Access = private)
privateProp
end
methods
function obj = MyClass(value)
obj.privateProp = value;
end
function publicMethod(obj,value)
obj.privateMethod(value);
end
function res = calculateSomeValue(obj)
res = obj.publicProp*obj.privateProp;
end
end
methods (Access = private)
function privateMethod(obj,value)
obj.publicProp = obj.publicProp + value;
obj.privateProp = obj.privateProp + obj.doubleThisValue(value);
end
end
methods(Static)
function res = doubleThisValue(val)
res = 2 * val;
if coin == 'q'
value = 25;
elseif coin == 'd'
value = 10;
elseif coin == 'n'
value = 5;
elseif coin == 'p'
value = 1;
else
value = 0;
end
function total = insert_coins
total = 0;
while(total<115)
coin = input('Enter q for quarter, d for dime, n for nickel,p for penny:','s');
total = total + get_coin_value(coin);
end
disp("Your NAU power juice has been dispensed")
end
end
end
end