Read everything before doing anything

Create a class called `Rational`

for performing
arithmetic with fractions. You will write this class to conform
to the following specifications.

- The class has two attributes:
`numerator`

and`denominator`

. - Here is an example of the use of the constructor for this
class.
$fraction = Rational->new(numerator => 3, denominator => 4);

Implement the following methods:

`gcd()`

is a method which returns the greatest common divisor of the object’s numerator and denominator. If either the numerator or the denominator is zero, return zero. Example of use:`$divisor = $fraction->gcd();`

`numerator()`

to set and get the`numerator`

attribute. (See page 273 for information about get-set methods.)`denominator()`

will set and get the`denominator`

attribute.`reduce()`

reduces the rational number to lowest term using this algorithm:- Find the GCD of the numerator and the denominator.
- If it is non-zero, divide numerator and denominator by GCD.

This method changes the object’s attributes; it does

*not*return a value.`add()`

adds the rational number to another rational number. It returns a rational numer, reduced to lowest terms. Example of use:`$fraction3 = $fraction1->add($fraction2);`

Algorithm: Where this

`Rational`

is`a/b`

and the addend is`c/d`

: if the denominators are equal then the result is`(a+c)/b`

, otherwise`((a*d)+(b*c))/(b*d)`

. Reduce this result to lowest terms.`subtract()`

subtracts the rational number given as its argument from the current rational number. It returns a rational number, reduced to lowest terms. Example of use:`$fraction3 = $fraction1->subtract($fraction2);`

. Thus, if`$fraction1`

had the value 3/4 and`$fraction2`

had the value 1/2, the result in`$fraction3`

would be 1/4.Algorithm 1: Where this rational number is

`a/b`

and the minuend is`c/d`

, if the denominators are equal then the result is`(a-c)/b`

, otherwise`((a*d)-(b*c))/(b*d)`

. Reduce this result to lowest terms.Algorithm 2: Where this rational number is

`a/b`

and the minuend is`c/d`

, create a new`Rational`

with value`-c/d`

. Calculate`(a/b)+(-c/d)`

by using the`add()s`

method.`multiply()`

multiplies the rational number given as its argument by the current rational number. It returns the result, reduced to lowest terms. Example of use:`$fraction3 = $fraction1->multiply($fraction2);`

Algorithm: Where this rational number is

`a/b`

and the multiplicand is`c/d`

, calculate`(a/b)*(c/d)`

as`(a*c)/(b*d)`

. Reduce this result to lowest terms.`divide()`

divides the current rational number by the rational number given as its argument. It returns the result, reduced to lowest terms. Example of use:`$fraction3 = $fraction1->divide($fraction2);`

In this example, if`$fraction1`

has the value 2/5 and`$fraction2`

has the value 3/7, the result in`$fraction3`

will be 14/15.Algorithm 1: Where this rational number is

`a/b`

and the divisor is`c/d`

, calculate`(a/b)/(c/d)`

as`(a*d)/(b*c)`

. Reduce this result to lowest terms.Algorithm 2: Where this rational number is

`a/b`

and the divisor is`c/d`

, create a new`Rational`

with value`d/c`

, then calculate`(a/b)*(d/c)`

using the`multiply()`

method.`to_string()`

returns the string representation of the rational number in the form

.*numerator*/*denominator*

To reduce a fraction to its lowest terms, you must divide both
the numerator and denominator by their greatest common divisor.
Here is code that calculates the greatest common divisor. You should
copy and paste this into your `Rational`

class.

sub gcd { my $self = shift; my $a = $self->{numerator}; my $b = $self->{denominator}; my $mod; # ensure that both numbers are positive $a = -$a if ($a < 0); $b = -$b if ($b < 0); # avoid division by zero if ($a == 0 || $b == 0) { $b = 0; } else { if ($a < $b) # a must always be greater than b { ($b, $a) = ($a, $b); # swap them } while (($mod = $a % $b) != 0) { $a = $b; $b = $mod; } } return $b; }

You may download `test_rational.pl`,
a program which lets you enter fractions and operations and see the results.
(Enter fractions in the form `2/3`.)

Email your file `Rational.pm`

to
`david.eisenberg@evc.edu`

.
The subject line
of your email *must* include your full name, the class name, and
the assignment title. Here are two correct examples:

CIT042 - Jane Bloggs - Objects Objects program, CIT042, Jane Bloggs