Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Python Programming (code)

 

Write a class named "Fraction" that represents a rational number (a number that can be expressed as the quotient of two integers). Implement the following methods:

  • The __init__(self, numerator, denominator) method should accept integer values for the numerator and denominator arguments and set instance attributes of the same name. If the denominator is 0, raise a ZeroDivisionErrorexception.  Use the [math.gcd](https://docs.python.org/3/library/ math.html#math.gcd) function to find the greatest common divisor (GCD) of the numerator and de- nominator and then divide each of them by it to "normalize" the fraction. For example, the fraction 28/20 will get normalized to 7/5 since the GCD of 28 and 20 is 4:

>>> x = Fraction(28, 20)

>>> x

Fraction(7, 5)

 

  • Implement the basic binary operators (+,-,*,/) so that a Fraction can be combined with either another fraction or an integer. All methods should return a new Fraction. Note that you may need to implement "reversed" operators for arithmetic with integers to fully work.
  • The __neg__ method should return a new Fraction instance with the numerator negated.
  • The __repr__ method should return a string of the form Fraction(a, b) where a and b are the numerator and denominator, respectively.

--------------------------------------------------------------------------

Few test cases:

>>> from fraction import Fraction

>>> frac_1 = Fraction(28,20)

>>> frac_

Fraction(7, 5)

 

>>> frac_2 = Fraction(1,0)

Traceback (most recent call last):

  raise ZeroDivisionError

fraction.ZeroDivisionError: Denominator cannot be zero

 

>>> frac_2 = Fraction(1,5)

>>> frac_1 + frac_

Fraction(8, 5)

>>> frac_1 * frac_

Fraction(7, 25)

>>> frac_2 - frac_

Fraction(-6, 5)

>>> frac_2 / frac_

Fraction(1, 7)

>>> frac_2 / 2

Fraction(1, 10)

>>> frac_1 / 2

Fraction(7, 10)

>>> frac_1 * 2

Fraction(14, 5)

>>> -frac_

Fraction(-1, 5)

>>> 2 - frac_

Fraction(9, 5)

>>> 2 - (-frac_2)

Fraction(11, 5)

--------------------------------------------------------------------------

Required Output:

 

class Fraction:

    def __init__(self, numerator. denominator):

        pass

 

    def __neg__():

        pass

 

    def __repr__():

        pass

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education