Matlab question!!! Design and implement a class of fractions in MATLAB. To aid i
ID: 3570339 • Letter: M
Question
Matlab question!!!
Design and implement a class of fractions in MATLAB. To aid in displaying fractions, the sign of the number should be kept with the numerator. For example, the fractional representation of
-0.75 would be -3/4, with a numerator of -3 and a denominator of 4.
Your class should include the following functions.
a constructor that accepts 0, 1, or 2 arguments. For no arguments, the constructor should
create a fraction with numerator zero and denominator 1. For one argument a, the construction should create a fraction with numerator a and denominator 1. For two arguments a and b, the constructor should create a fraction with numerator a and denominator b and then simplify the fraction. The denominator should not be equal to zero, so if b is zero, an appropriate error message should be displayed.
a function that allows the user to change the numerator of a fraction
a function that allows the user to change the denominator of a fraction
a function returns the numerator of a fraction
a function returns the denominator of a fraction
an overload of the display function so that a fraction with numerator a and denominator b is displayed as a/b.
functions that overload the + and * operators so that they can be used with fractions (use Help
Explanation / Answer
function [y]=fraction(a,b)
if nargin==0
str=[num2str(0),'/',num2str(1)];
else if nargin==1
str=[num2str(a),'/',num2str(1)];
else if nargin==2
if b==0
disp('not valid input')
else
c=gcd(a,b);
a1=abs(a/c);
b1=abs(b/c);
if a*b<0
str=['-',num2str(a1),'/',num2str(b1)];
else
str=[num2str(a1),'/',num2str(b1)];
end
end
end
end
end
y=str;
end