Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(python) Next, the methods that you will write. In the method descriptions and usage cases below, you can assume that r1 and r2 are Rational

(python)

image text in transcribed

Next, the methods that you will write. In the method descriptions and usage cases below, you can assume that r1 and r2 are Rational objects with numerator and __denominator instance variables initialized with values. Accessors and mutators: You should create accessors and mutators (or "getters" and "setters") for both the numerator and denominator instance variables. There are thus four required methods here: getNumerator(self), getDenominator(self). set Numerator(self, n), and setDenominator(self, d). Additionally, the mutators should call the reduce method to put the updated Rational in lowest terms, e.g., calling setDenominator(4) on the Rational 2/7 should update the Rational to 2/4 and then reduce it to 1/2. Only the accessors should return a value. o Usage: n = r1.getNumerator If ri is 3/4, returns 3. o Usage: d = r1.getDenominator . If r1 is 3/4, returns 4. o Usage: r1.setNumerator(7) If ri is 3/14, updates ri to 7/14 and then reduces to 1/2 o Usage: ri.setDenominator(-4) If ri is 3/4, updates ri to 3/-4 isValid(self): Rational numbers cannot have a 0 in the denominator. This method should return True if the Rational is valid, and should return False if the Rational has a 0 denominator. o Usage: val = r1.isValido If ri is 7/4, returns True If ri is -7/0, returns False add(self, num2) : Given input Rational num2 . you should add num2 to the current Rational , updating the self.__.numerator and self._ denominator instance variables to the sum of self and num2. You should also call the reduce method to put the Rational in lowest terms. Nothing is returned by this method. o Usage: r1.add(r2) If ri is 1/4 and r2 is 1/8, updates r1 to 3/8 sub(self, num2) : Same as the add method, but subtracting num2 from self. o Usage: r1.sub(r2) If r1 is 1/4 and r2 is 1/8, updates ri to 1/8 mult(self, num2) : Same as the add method, but multiplying num2 by self. o Usage: r1.mult(r2) . If r1 is 1/4 and r2 is 1/8, updates ri to 1/32 div(self, num2) : Same as the add method, but dividing num2 out of self. o Usage: r1.diverz) If ri is 1/4 and r2 is 1/8, updates ri to 2/1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

The effects of inflation in capital budgeting analysis? GT=75

Answered: 1 week ago

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago