You are going to be working with class Rational which defines rational number objects. A rational number
Question:
You are going to be working with class Rational which defines rational number objects. A rational number is a number that can be written as a fraction. A rational number consists of a numerator and a denominator (which cannot be 0). Rational numbers also have the ability to be displayed in an original form, decimal form or reduced form. You are going to be completing various object methods to perform the tasks previously list.
You are to begin with the Rational Class and RationalTest classes provided. Make all modifications outlined in the point values below. Pay close attention to the names of the attributes, methods and return values. You are given starter files and the test files necessary. Only modify class Rational and keep the function headers provided to you for all of Part 1. You will need to add your own methods for Part 2. You will only be given credit for the lab if you follow the directions precisely.
Part 1a (15 Points)
This version requires that you complete the constructor, getOriginal(), and getDecimal() methods of class Rational.
Constructor: at this stage should have a parameter list that accepts two arguments, a numerator and denominator that are defaulted appropriately. These parameters must initialize attributes called originalNumerator and originalDenominator
getOriginal(self) should return a string representation of the original fraction. This method will be used in displayData()
getDecimal(self) should return a string representation of the original fraction as a decimal. This method will be used in displayData().
Part 1a - Output
Part 1b (15 Points)
This version requires that you first modify the constructor, complete method reduce() and getRational(), and lastly modify the displayData method
Constructor: this needs two new attributes, reducedNumerator and reducedDenominator. You can initialize these to the same values as the origingalNumerator and originalDenominator. The last line of the constructor should be a call to the reduce method that will give them actuall value
reduce(self) : the reduce method is a non return method. It is to give value to the reducedNumerator and reduceDenominator attributes. Use the getGCF method to help you reduce the original numerator and denominators
getRational(self) will return a string representation of the rational number in reduced form.
The new display data method should read:
Part 1b - Output
For Part 2 you are going to need to test your fill with the RationalTestPart2.py file.
You are going to be adding 4 static methods to class rational that will return a rational number that is the sum, difference, product and quotient of two other rational numbers. See the complex number subraction function example in the OOP Part 4 video.
Part 2a Version (10 Points)
This version requires that you write the static multiply and divide methods. These methods will accept two Rational Numbers as parameters and return a new Rational number that is the product or quotient of the two parameters.
The methods must be static method and must be named appropriately
Part 2a - Output
Part 2b Version (10 Points)
The final version requires that you write the static add and subtract methods. These methods will accept two rational numbers as parameters and return a new Rational number that is the sum or difference of the two parameters.
Part 2b - Output
Rational.py
class Rational(object):
"""This creates a rational number (fraction)"""
def __init__(self, numerator=0, denominator=1):
"""Constructor for a rational number with a numerator and denominator"""
pass #remove pass instruction before you begin
def getOriginal(self):
"""returns a string representation of the original rational number"""
pass #remove pass instruction before you begin
def getDecimal(self):
"""returns a string representatin of the rational number in decimal form"""
pass #remove pass instruction before you begin
def reduce(self):
"""reduces the rational number """
pass #remove pass instruction before you begin
def getRational(self):
"""return a string representation of the reduced rational number"""
pass #remove pass instruction before you begin
def displayData(self):
print()
print(self.getOriginal() + " equals "+ self.getDecimal())
print()
def getGCF(self,num1:int, num2:int):
"""returns the greatest common factor of 2 integer values"""
rem = None
gcf = None
while (rem != 0):
rem = num1 % num2
if (rem == 0):
gcf = num2
else:
num1 = num2
num2 = rem
return gcf;
RationalTestPart1.py
from Rational import *
def enterData():
'''Allows user to enter a valid numerator and denominator'''
num = ""
den = ""
while num == "" or den == "" or den == "0":
num = input("Please enter the numerator ---> ")
den = input("Please enter the denominator ---> ")
num = int(num)
den = int(den)
return num, den
def main():
'''main method, creates a Rational number and displays information about it'''
num, den = enterData()
rationalNumber = Rational(num, den)
rationalNumber.displayData()
main()
input("press enter to quit")
RationalTestPart2.py
from Rational import *
def enterData(str):
num = ""
den = ""
while num == "" or den == "" or den == "0":
print(str)
num = input("Please enter the numerator ---> ")
den = input("Please enter the denominator ---> ")
num = int(num)
den = int(den)
return num, den
def main():
num, den = enterData("Enter data for r1")
r1 = Rational(num, den)
num, den = enterData("Enter data for r2")
r2 = Rational(num, den)
print()
r3 = Rational.multiply(r1, r2)
print(r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getRational())
r3 = Rational.divide(r1, r2)
print(r1.getOriginal() + " / " + r2.getOriginal() + " = " + r3.getRational())
## REMOVE COMMENTS BELOW TO TEST THE 2b POINT VERSION ##
## r3 = Rational.add(r1, r2)
## print(r1.getOriginal() + " + " + r2.getOriginal() + " = " + r3.getRational())
## r3 = Rational.subtract(r1, r2)
## print(r1.getOriginal() + " - " + r2.getOriginal() + " = " + r3.getRational())
##
## this is for git to show an update
main()
input("\npress enter to quit")
Management Accounting Information for Decision-Making and Strategy Execution
ISBN: 978-0137024971
6th Edition
Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young