Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Sample code for ADA topic 1 Date: 2 0 - 1 0 - 2 0 2 2 Following should be inside ratNum.py
Sample code for ADA topic
Date:
Following should be inside "ratNum.py
# find greatest common divisor
# using the fact that gcdab gcdbc where c a b
# if b is zero, gcdab gcda a
def gcdab:
while b :
r a b
a b
b r
return a
# find least common multiple using gcd function
def lcma b:
return ab gcdab
# ratNum representing rational numbers ab
class ratNum:
# constructor for creating a rational number, default is zero
def initself a b:
self.a a
if b : self.b b
else:
printb shound be greater than zero!"
self.b
def inputself: # input a rational number from keyboard
while True:
a b inputinput ab b: split
a b inta intb
if b : break
printb shound be greater than zero!"
self.a a
self.b b
def strself: # convert to str for display
return fselfaselfb
def addself y: # rational number addition
b lcmselfb yb
a self.a b self.b ya b yb
return ratNumab
def subself y: # rational number subtraction
b lcmselfb yb
a self.a b self.b ya b yb
return ratNumab
def mulself y: # rational number multiplication
a self.a ya
b self.b yb
g gcda b;
a a g
b b g
return ratNumab
def truedivself y: # rational number division
if ya : printDivision by zero!"
else:
a self.a yb
b self.b ya
g gcda b
a a g
b b g
return ratNumab
def eqself y: # comparing two rational numbers
diff self ya
if diff : return
elif diff : return
return
# end of "ratNum.py
# Following should be inside "main.py
# from ratNum import
z ratNum; zinput
z ratNum; zinput
printz z zz # Task
printz z zz # Task
printzx z zz # Task
printz z zz # Task
printz z zz # Task
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