Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i want a solution for this code class Fraction We will implement a simple class representing a fractional data type that is immutable and that

i want a solution for this code image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
class Fraction We will implement a simple class representing a fractional data type that is immutable and that supports basic arithmetic operations, simplification, conversion to real numbers, and string representation. An illustration of the usage of the class is shown below: Note that Fraction class must raise Python's built-in exception ZeroDivisionBrror whenever the denominator is or becomes 0 . Also note that the numerators and denominators are always integers. 1. A fraction is represented internally as a numerator and denominator pair. The initialization method init_(self, numerator, denominator) simply stores the numerator and the denominator for future use. It also raise an exception when denominator is zero. This method is implemented and provided for you. f=Fraction(2,4) f.numerator, f,denominator (2, 4) =Fraction(3,0) Traceback (most recent call last): 2. The method str__(self) returns a string representation of the fraction. This method will be called by Python when str () function is called on a fraction or when a fraction is printed. No simplification should be performed on self. This method is implemented and provided for you. f,g=Fraction(8,12). Fraction (6,5) str(1);str(fg) 8/12 48/60 print(f);print(fg) 8/12 48/60 3. Write a method get. fraction (self) that returns a tuple of the numerator and the denominator: No simplification should be performed. 4. Write a private method gcd(self,m,n) that calculates and returns the greatest common divisor (GCD) of the two integers m and n. Euclidean algorithm for GCD is a simple recursive algorithm. For two integers a and b such that ab : - if a is a multiple of b, then GCD of a and b is b. - otherwise, GCD of a and b is simply GCD of b and a%b. This private method will be used by the method simplify (self) below. f= Fraction (2,4);fgcd(2,4) 2 f= Fraction (2100,4050);f.gcd(2100,4050) 150 >f= Fraction (2000,4050);f+gcd(2000,4050) 50 5. Write a method simplify (self) that replaces the fraction's internal representation with an equivalent but simplified represenation. You can simplify a fraction by dividing both numerator and denominator by GCD of them. Unlike other methods, simplify (self) does not return a new fraction, but rather changes the internal representation of self. Since the value of the fraction is not changing, we do not consider this as a violation on the notion of immutable data. ,g=Fraction(8,12).Fraction(6,5)f.simplify();f.getfraction()(2.0,3.0) \[ \begin{array}{l} \gg b h=f \cdot g ; \text { h.get_fraction }() \\ (12.0,15.0) \\ \gg \gg \text { h. simplify( ) h.get fraction( ) } \\ (4.0,5.0) \end{array} \] 6. Write a method neg-(self) that returns a new frection that is a negation of eelf. This metbod will be called by Python for unary negation. No simplification should be performed on the result. 7. Write a method _add__(self, other) that roturns a new fraction that is equal to the sum of self and other. Also write a method sub__(self, other) that returns a new fraction that is equal to the difference between self and other. These methods will be called by Python for addition and subtraction. No simplification should be performed on the resalt. 8. Write a method mul _ (self, other) that returns a new fraction that is equal to the product of self and other. Also write a method__ruediv__(self, other) that returns a new fraction that is equal to the division of self by other. These methods will be called by Python for multiplication and division. No simplification should be performed on the result. that returns True if self is equal to other and False otherwise. a method lt__(self, other) that returns True if self is less than other and False simplification should be performed on the result. f,g= Fraction (1,2), Fraction (2,3) f=f True f=g False f

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Identify the hallucinogens, and describe their effects.

Answered: 1 week ago