Create a class called RationalNumber for performing arithmetic with fractions. Write a driver program to test your class.

Use integer variables to represent the data of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values, in case no initializers are provided, and should store the fraction in reduced form (i.e., the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator). Provide methods for each of the following:
a) Adding two RationalNumbers. The result should be stored in reduced form.
b) Subtracting two RationalNumbers. The result should be stored in reduced form.
c) Multiplying two RationalNumbers. The result should be stored in reduced form.
d) Dividing two RationalNumbers. The result should be stored in reduced form.
e) Printing RationalNumbers in the form a/b, where a is the numerator and b is the denominator.
f) Printing RationalNumbers in floating-point format.


```
# Rational class for fractions.

def gcd( x, y ):
"""Returns the greatest common denominator of two values"""

while y > 0:
z = y
y = x % z
x = z

return x

class RationalNumber:
"""Represents a rational number"""

def __init__( self, top = 0, bottom = 1 ):
"""Initializes RationalNumber object"""

# use integers for numerator and denominator
top = int( top )
bottom = int( bottom )

# error check
if bottom == 0:
raise ValueError, "Cannot divide by zero"

# store fraction in reduced form
common = gcd( top, bottom )
self.numerator = top / common
self.denominator = bottom / common

def add( self, number ):
"""Returns the sum of two RationalNumber objects"""

top = self.numerator * number.denominator + \
number.numerator * self.denominator
bottom = self.denominator * number.denominator

return RationalNumber( top, bottom )

def subtract( self, number ):
"""Returns the difference of two RationalNumber objects"""

top = self.numerator * number.denominator - \
number.numerator * self.denominator
bottom = self.denominator * number.denominator

return RationalNumber( top, bottom )

def multiply( self, number ):
"""Returns the product of two RationalNumber objects"""

top = self.numerator * number.numerator
bottom = self.denominator * number.denominator

return RationalNumber( top, bottom )

def divide( self, number ):
"""Returns the quotient of two RationalNumber objects"""

top = self.numerator * number.denominator
bottom = self.denominator * number.numerator

return RationalNumber( top, bottom )

def printFraction( self ):
"""Prints RationalNumber in fraction format"""

print "%d/%d" % ( self.numerator, self.denominator )

def printDecimal( self ):
"""Prints RationalNumber in decimal format"""

print float( self.numerator ) / self.denominator
# Exercise 7.4: ex07_04.py
# Driver to test the Rational class.

from Rational import RationalNumber

# create Rational objects
rational1 = RationalNumber( 24, 36 ) # simplifies to 2/3
rational2 = RationalNumber( 11, 55 ) # simplifies to 1/5
defaultRational = RationalNumber()

# displaying RationalNumbers in fraction format
print "This is defaultRational",
defaultRational.printFraction()
print "This is rational1:",
rational1.printFraction()
print "This is rational2:",
rational2.printFraction()

# displaying RationalNumbers in floating-point format
print "This is defaultRational in floating-point:",
defaultRational.printDecimal()
print "This is rational1 in floating-point:",
rational1.printDecimal()
print "This is rational2: in floating-point:",
rational2.printDecimal()

# adding two RationalNumbers
print "\nThe sum of rational1 and rational2 is",
rational1.add( rational2 ).printFraction()

# subtracting one RationalNumber from another
print "The difference of rational1 and rational2 is",
rational1.subtract( rational2 ).printFraction()

# multiplying two RationalNumbers
print "The product of rational1 and rational2 is",
rational1.multiply( rational2 ).printFraction()

# dividing one RationalNumber by another
print "The quotient of rational1 and rational2 is",
rational1.divide( rational2 ).printFraction()
```
This is defaultRational 0/1
This is rational1: 2/3
This is rational2: 1/5
This is defaultRational in floating-point: 0.0
This is rational1 in floating-point: 0.666666666667
This is rational2: in floating-point: 0.2
The sum of rational1 and rational2 is 13/15
The difference of rational1 and rational2 is 7/15
The product of rational1 and rational2 is 2/15
The quotient of rational1 and rational2 is 10/3

Computer Science & Information Technology

You might also like to view...

A ________ table is a table within another table

Fill in the blank(s) with correct word

Computer Science & Information Technology

Viewing the Property Sheet of a form is possible in either the Design View or Layout View.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Which of the following are Boolean operators?

a. AND b. OR c. NOT d. All of these

Computer Science & Information Technology

Given the program, which of the following class member accesses are legal?

``` #include using namespace std;class DayOfYear{public: void input(); void output();// other public members private: int month; int day; // other private members };int main(){ DayOfYear birthDay; birthDay.input(); // a) birthDay.day = 25; // b) cout << birthDay.month; // c) cout << birthDay.output(); // d) if(birthDay.month == 1) // e) cout << "January\n"; ``` What will be an ideal response?

Computer Science & Information Technology