Question
In python 3 I need help with writing a script that can do the following Let's look at some examples of rational numbers: P Q
In python 3 I need help with writing a script that can do the following
Let's look at some examples of rational numbers:
P | Q | Rational Number | Decimal Approximation |
---|---|---|---|
1 | 4 | 1/4 | 0.25 |
3 | 1 | 3/1 | 3.0 |
7 | 2 | 7/2 | 3.5 |
35 | 10 | 35/10 | 3.5 |
-1 | 2 | -1/2 | -0.5 |
2 | 7 | 2/7 | 0.285714 |
-4 | -5 | 4/5 | 0.8 |
3 | -2 | -3/2 | -1.5 |
6 | 0 | illegal | illegal |
Notice that there is no requirement that the fraction be in "reduced form". Also, the fraction can be "improper", that is the numerator (top number) can be bigger than the denominator (bottom). That's why we're using the term rational numbers, rather than fractions.
Also, the Q > 0 requirement changed several of the rational numbers from being simple expressions of P / Q. Examples of this are shown in the last three lines of the table.
Instructions
Create a class named rational that represents rational numbers. Put this class in a module named rational_numbers.
Initial Implementation
The starting point for the class will just create rational objects and provide a way to print them out:
Like the point class we created in the demo, the rational class will need two "internal" variables to hold the values of the numerator and the denominator, P and Q.
Write a constructor (__init__) that will accept two int values for the numerator and denominator of the rational number being created.
Write a method (function inside the class) that returns a string representation of the rational number in the form (P/Q), including the parentheses. This method needs to use the special name __str__.
The initial implementation does not have do anything about the Q > 0 requirement.
Here is some code to test your class:
import rational_numbers def main(): a = rational_numbers.rational(3, 4) b = rational_numbers.rational(1, 2) print('a =', a) print('b =', b) main()
Running this program will create the following output:
a = (3/4) b = (1/2)
Basic Implementation
The basic implementation adds two operations to the rational class:
Write a method named plus that accepts a rational parameter and returns a new rational number which is the sum of the two rationals.
Write a method named star that accepts a rational parameter and returns a new rational number which is the product of the two rationals
The basic implementation does not have implement the Q > 0 requirement, either.
Here is a little refresher on addition and multiplication of fractions.
When multiplying two fractions, simply multiply the numerators for the value of the numerator of the product and multiply the denominators for the value of denominator of the product.
abcd=acbdabcd=acbd
Adding two fractions is a bit more complicated. We need to find a common denominator. Then we add the two numerators and put the sum over the common denominator.
ab+cd=adbd+bcbd=ad+bcbdab+cd=adbd+bcbd=ad+bcbd
Remember, there is no need to have the fractions in "reduced" form.
Here is some code to test the basic implementation:
import rational_numbers def main(): a = rational_numbers.rational(3, 4) b = rational_numbers.rational(1, 2) print('a =', a) print('b =', b) print(a, '+', b, '=', a.plus(b)) print(a, '*', b, '=', a.star(b)) main()
Running this program will create the following output:
a = (3/4) b = (1/2) (3/4) + (1/2) = (10/8) (3/4) * (1/2) = (3/8)
Please note: The plus and star functions shall return rational_number objects, not string values.
Here is some code to test this feature of star:
import rational_numbers def main(): b = rational_numbers.rational(1, 2) c = b.star(b) print('b =', b) print('c =', c) print(c, '+', b, '=', c.plus(b)) print(c, '*', b, '=', c.star(b)) main()
Running this program will create the following output:
b = (1/2) c = (1/4) (1/4) + (1/2) = (6/8) (1/4) * (1/2) = (1/8)
This basic implementation is worth 7 points: 5 point for the initial implementation and 2 for addition and multiplication.
Complete Implementation
The complete implementation has three enhancements:
support for the Q > 0 requirement
the option of creating a rational number and providing only a single int parameter to the constructor
a method named toFloat that returns a float approximation of the rational value
The Q > 0 requirement has two parts. First, if Q is negative, the values of P and Q need to be adjusted so that Q ends up positive. The other case, when Q = 0, is discussed below.
Here is some code to test your class handling negative Q values:
import rational_numbers def main(): a = rational_numbers.rational(3, -4) b = rational_numbers.rational(-1, -2) print('a =', a) print('b =', b) main()
Running this program will create the following output:
a = (-3/4) b = (1/2)
Here is some code to test your class accepting one value in the constructor:
import rational_numbers def main(): a = rational_numbers.rational(3) b = rational_numbers.rational(1, 2) print('a =', a) print('b =', b) print(a, '+', b, '=', a.plus(b)) print(a, '*', b, '=', a.star(b)) main()
Running this program will create the following output:
a = (3/1) b = (1/2) (3/1) + (1/2) = (7/2) (3/1) * (1/2) = (3/2)
Here is some code to test your toFloat method:
import rational_numbers def main(): a = rational_numbers.rational(3, 4) b = rational_numbers.rational(1, 2) print(a, '=', a.toFloat()) print(b, '=', b.toFloat()) main()
Running this program will create the following output:
(3/4) = 0.75 (1/2) = 0.5
[EXTRA-CREDIT] The Q > 0 requirement presents a problem if the second parameter to the constructor is zero. Since this would create an illegal rational value, the constructor needs to "crash". We can do this by checking the value of Q. If it is zero, the constructor should signal the illegal condition using an exception.
Use the ZeroDivisionError when you raise the exception. Include a description message.
Here is some code to test your class handling zero Q values:
import rational_numbers def main(): a = rational_numbers.rational(3, 0) main()
Running this program will create the following output:
Traceback (most recent call last): File "C:/teaching/work/use_rational.py", line 7, in
Here is some other code to test your class handling zero Q values:
import rational_numbers def main(): try: a = rational_numbers.rational(3, 0) except ZeroDivisionError: print('Houston, we have a problem.') main()
Running this program will create the following output:
Houston, we have a problem.
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