Question: Using the Fraction class in Python to read a data file and print outputs The above image are the instructions, and here is the provided

Using the Fraction class in Python to read a data file and print outputs

Using the Fraction class in Python to read a data file and

The above image are the instructions, and here is the provided code:

FRACTIONDAS.PY

___________________

def gcd(m,n):

while m%n != 0:

oldm = m

oldn = n

m = oldn

n = oldm%oldn

return n

class Fraction:

def __init__(self, top, bottom):

self.num = top

self.den = bottom

def __str__(self):

return str(self.num) + "/" + str(self.den)

def show(self):

print(self.num, "/", self.den)

def __add__(self, otherfraction):

newnum = self.num * otherfraction.den + \

self.den * otherfraction.num

newden = self.den * otherfraction.den

common = gcd(newnum, newden)

return Fraction(newnum // common, newden // common)

def __eq__(self, other):

firstnum = self.num * other.den

secondnum = other.num * self.den

return firstnum == secondnum

fp=open("lab02data.txt","r")

line=fp.readline() #ignore first line

line=fp.readline()

while line!="":

# x = Fraction(1, 2) this is left over from original code

# y = Fraction(2, 3) this is left over from original code

fouritems=line.split()

x = Fraction(int(fouritems[0]),int(fouritems[1]))

y = Fraction(int(fouritems[2]),int(fouritems[3]))

print("x is ",x)

print("y is ",y)

print("sum is", x + y)

print("x is the same fraction as y:", x == y)

print("The next pair of fractions ...")

line=fp.readline()

FRACTION.PY

___________________

def gcd(m,n):

while m%n != 0:

oldm = m

oldn = n

m = oldn

n = oldm%oldn

return n

class Fraction:

def __init__(self,top,bottom):

self.num = top

self.den = bottom

def __str__(self):

return str(self.num)+"/"+str(self.den)

def show(self):

print(self.num,"/",self.den)

def __add__(self,otherfraction):

newnum = self.num*otherfraction.den + \

self.den*otherfraction.num

newden = self.den * otherfraction.den

common = gcd(newnum,newden)

return Fraction(newnum//common,newden//common)

def __eq__(self, other):

firstnum = self.num * other.den

secondnum = other.num * self.den

return firstnum == secondnum

x = Fraction(1,2)

y = Fraction(2,3)

print(x+y)

print(x == y)

Create a text file named lab02data.txt with the following contents in your project (Use spaces, not tabs; do not worry about exact spacing) #this is fie ab02data.txt 3 8 1 1 -3 8 1 1 -3 4 1 4 4 11 8-22 -7 12 5-8 -5 8 3-4 6 -135 13 9-58 16 -53 1 9-13 7 -12 Write, modify, and/or update a Python program that will do the following: Do nothing with the first header record in the data file. Subsequently, for each record (line) in the file, treat the first two integers as the numerator and denominator, respectively, of a first fraction and treat the latter two integers as the numerator and denominator, respectively, of a second fraction. Then, print the following: "The first fraction is:". . . "The second fraction is:".. . "The sum of the two fractions is: ".. . "The difference of the two fractions is: " "The product of the two fractions is: ". . . "The first fraction divided by the second frastons is:".. "The first fraction is S the second fraction

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!