Search for question
Question

Object Oriented Programming (OOP)

3. (8 pts) In this problem, we will write a class that can represent rational numbers, i.e. fractions

9

a) Create a class Rational which is initialized by two integers, p and q, the nominator and

denominator and then add a method to print the rational number as (the_str___ or

__repr____ method is useful). (2 pts)

30

b) We would like to represent by instead, hence write a function that computes the

greatest common divisor and ensure that every rational number is simplified. (2 pts)

c) Add four methods to the Rational class, so that we can perform addition, subtraction,

multiplication, and division on rational numbers. (2 pts)

d) Add two methods to the Rational class to compare whether two rational numbers are equal

and to convert a rational number to a floating-point representation. (2 pts)

Fig: 1