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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!