Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a Frac tion class ( frac tion.py ) that represents a ra tional number ( i . e . , numbers that can be
Write a Frac
tion class frac
tion.py that represents a ra
tional number ie numbers that can be
expressed in the form of ab where a and b are integers For example, a variable with a value
of would be created as follow:
onehalf Frac
tion
Your class should provide access to the numerator and denominator through the methods
numerator and denominator, and support the following methodsopera
tions:
reprself: return a string representa
tion of a Frac
tion object, eg Frac
tion
strself: return a string value of a Frac
tion object, eg
Add, subtract, mul
tiply, and divide frac
tions. Each method should return a Frac
tion object with
the answer and be implemented by overloading the appropriate operator.
Comparisons:
Your Frac
tion class should ensure that your frac
tions are represented using a canonical unique
form. That is your frac
tions should be in reduced form. Other ques
tions to consider are how
is represented and how a nega
tive number is represented. Handling these ques
tions about
frac
tion representa
tion are part of robust programming.
Greatest common divisor: To represent a frac
tion in reduced form, you need to calculate the
greatest common divisor of the numerator and denominator. This can be done using the
Euclidean algorithm. Assume that we want gcda b where a b Then, using integer
arithme
tic, we can calculate the gcd using the following algorithm:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started