Question
Using Python, create an abstract data type called Fraction (using a class). The Fraction should have: Data: A numerator (integer) A denominator (integer) Operations: A
Using Python, create an abstract data type called Fraction (using a class). The Fraction should have:
Data:
A numerator (integer)
A denominator (integer)
Operations:
A constructor (takes a numerator and denominator as parameters)
A __str__ function that returns a string representation of the Fraction
An __add__ function (overloads "+") that returns a new fraction that is the addition of self and its parameter fraction
A __sub__ function (overloads "-") that returns a new fraction that has the value of the current minus the parameter fraction
A __mul__ function (overloads "*") that returns a new fraction that is the multiplication of the current fraction and the parameter fraction
A __truediv__ function (overloads "/") that returns a new fraction that is the division of the current fraction with the parameter fraction
All of the operations should use the greatest common denominator function (below) to reduce the fraction as much as possible.
Create a greatest common denominator (gcd) function that returns the greatest common denominator two integers (parameters) (algorithm given on p. 41 of the textbook)
Test your program with a variety of fractions (positive and negative).
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