Description
For this assignment, design and implement the methods for a class that can be used to represent a rational number (ie. fraction).
The Rational class
The class contains two data members:
- an integer that holds the value of the numerator of the fraction
- an integer that holds the value of the denominator of the fraction
Constructors
There are two constructors for this class.
The first is the default constructor. It takes no arguments. It creates an object with both the numerator and denominator set to 1.
The second constructor is an alternate constructor that creates an object that contains a specific fractional value. It takes two integer arguments: the numerator and denominator values, respectively. The constructor should call the setRational method that is described below to initialize the data members and reduce the fraction.
Methods
The following methods are required for the Rational class. Make sure to pay attention to whether methods should be public or private and to code them accordingly.
void displayFloat()
This public method displays the rational number in its decimal form with 5 digits after the decimal point. It takes no arguments and returns nothing.
void displayFraction()
This public method displays the rational number in its fractional form (i.e. numerator/denominator). It takes no arguments and returns nothing.
void display()
This public method displays the rational number in both its decimal and fractional forms. It takes no arguments and returns nothing.
The method should call the displayFraction and displayFloat methods to do the displaying. The format for the display should be something similar to (assuming the fraction to display is 17/22):
17/22 or 0.77273
void setNumerator( int newNumerator )
This public method is used to change the numerator value. It takes one argument: an integer that holds the new numerator value. It returns nothing.
The method should use the passed in argument to initialize the data member that holds the numerator value and then call the reduce method that is described below to reduce the fraction.
void setDenominator( int newDenominator )
This public method is used to change the denominator value. It takes one argument: an integer that holds the new denominator value. It returns nothing.
The method should use the passed in argument to initialize the data member that holds the denominator value and then call the reduce method that is described below to reduce the fraction.
void setRational( int newNumerator, int newDenominator )
This public method is used to change fractional value. It takes two argument: an integer that holds the new numerator value and an integer that holds the new denominator value. It returns nothing.
The method should use the passed in arguments to initialize the corresponding data members and then call the reduce method that is described below to reduce the fraction.
Rational add( int addNumerator, int addDenominator )
This public method adds two rational numbers. It takes two arguments: an integer that holds the numerator of the rational number being added to the current instance and an integer that holds the denominator of the rational number being added to the current instance. It returns a Rational object that holds the sum of the addition.
The two rational numbers that are being added are the “current instance”, which is the Rational object that calls the add method. This means that the “current instance” value can be accessed by using the data member that holds the numerator and the data member that holds the denominator.
The other rational number is represented by the two passed-in arguments.
Before two rational numbers can be added, they must have a common denominator. Therefore, the first thing to do is to find the least common multiple of the two denominators by calling LCM method that is described below. This will be the common denominator for the two rational numbers.
Once the common denominator has been found, calculate the amount that each numerator will need to be multiplied by so that the two fractions will have the common denominator value.
For example, if the Rational object that calls the add method (the “current instance”) has a numerator data member with the value 3 and a denominator data member with the value 11; and the passed in values are 1 for addNumerator and 2 for addDenominator, then the least common multiple is 22.
For the current instance to have the common denominator of 22, the denominator data member would need to be multiplied by 2 (11 * 2 = 22). This means that the numerator would also need to be multiplied by 2. Lets call the value 2 currMultValue.
For the argument to have the common denominator of 22, the addDenominator would need to be multiplied by 11 (2 * 11 = 22). This means that addNumerator would also need to be multiplied by 11. Lets call the value 11 argMultValue.
Now that the multipliers are known, the updated numerators can be found. The updated numerators are the numerator data member times currMultValue (lets call this product updatedCurrNumerator) and addNumerator times argMultValue (lets call this product updatedArgNumerator).
Now that the two updated numerator values are known, updatedCurrNumerator and updatedArgNumerator can be added to calculate the numerator of the result.
To finish the method, create a Rational object with a numerator data member equal to the numerator of the result and denominator data member equal to the common denominator. Return the Rational object.
For example, if the current instance has a numerator data member with a value of 3 and a denominator data member with a value of 11, and add(1,2) is the calling statement for the method, then the method should create and return a Rational object with a numerator data member with a value of 17 and a denominator data member with a value of 22:
3 1 6 11 17 - + - => - + -- => -- 11 2 22 22 22
Rational subtract( int subNumerator, int subDenominator )
This public method subtracts two rational numbers. It takes two arguments: an integer that holds the numerator of the rational number being subtracted from the current instance and an integer that holds the denominator of the rational number being subtracted from the current instance. It returns a Rational object that holds the difference of the subtraction.
The logic for this method is similar to the add method.
For example, if the current instance has a numerator data member with a value of 7 and a denominator data member with a value of 12, and subtract(1,3) is the calling statement for the method, the method should create and return a Rational object with a numerator data member with a value of 1 and a denominator data member with a value of 4:
7 1 7 4 3 1 - - - => - - - => - => - 12 3 12 12 12 4
Rational multiply( int multNumerator, int multDenominator )
This public method multiplies two rational numbers. It takes two arguments: an integer that holds the numerator of the rational number the current instance is being multiplied by and an integer that holds the denominator of the rational number the current instance is being multiplied by. It returns a Rational object that holds the product of the multiplication.
The two rational numbers that are being multiplied are the “current instance”, which is the Rational object that calls the multiply method. This means that the “current instance” value can be accessed by using the data member that holds the numerator and the data member that holds the denominator.
The other rational number is represented by the two passed-in arguments.
Unlike addition and subtraction, there is no need to find a common denominator. With multiplication, the two numerator (and denominator) values can simply be multiplied together to calculate the result.
Like the add and subtract methods, to finish this method, create a Rational object with a numerator data member equal to the product of multiplying the two numerators and a denominator data member equal to the product of multiplying the two denominators. Return the Rational object.
For example, if the current instance has a numerator data member with a value of 7 and a denominator data member with a value of 12, and multiply(1,3) is the calling statement, the method should create and return a Rational object with a numerator data member with a value of 7 and a denominator data member with a value of 36:
7 1 7 - * - => - 12 3 36
Rational divide( int divNumerator, int divDenominator )
This public method divides two rational numbers. It takes two arguments: an integer that holds the numerator of the rational number the current instance is being divided by and an integer that holds the denominator of the rational number the current instance is being divided by. It returns a Rational object that holds the result of the division.
As with multiplication, there is no need to find a common denominator. The division operation is actually performed by multiplying the current instance by the reciprocal of the passed-in values. The reciprocal is found by simply making the numerator the denominator and the denominator the numerator.
The logic for this method is similar to the multiply method.
For example, if the current object has a numerator data member with a value of 2 and a denominator data member with a value of 73, and divide(5,8) is the calling statement, the method should create and return a Rational object with a numerator data member with a value of 16 and a denominator data member with a value of 365:
2 5 2 8 16 - / - => - * - => -- 73 8 73 5 365
void reduce()
This private method reduces a Rational object to its simplest form. It takes no arguments and returns nothing.
To reduce a fraction to its simplest form, find the greatest common divisor of the data member that holds the numerator and the data member that holds the denominator.
Divide the data member that holds the numerator by the greatest common divisor and save the result in the data member that holds the numerator.
Divide the data member that holds the denominator by the greatest common divisor and save the result in the data member that holds the denominator.
int GCD( int val1, int val2 )
This private method calculates and returns the greatest common divisor of two integer values.
Use the following method in this program to find the greatest common divisor:
int Rational::GCD( int val1, int val2 ) { int remainder, num1, num2; //set variables num1 and num2 to the two passed in values num1 = val1; num2 = val2; //continue dividing num1 by num2 until a remainder of 0 is found while (true) { //find the remainder when num1 is divided by num2 remainder = num1 % num2; //if the remainder is 0, get out of the loop if( remainder == 0 ) break; //set num1 to the current num2 value num1 = num2; //set num2 to the remainder num2 = remainder; }//end while //After the loop is done executing, the variable num2 will contain the //greatest common divisor. return num2; }
int LCM( int val1, int val2 )
This private method calculates and returns the least common multiple of two integer values.
Use the following method in this program to find the least common multiple:
int Rational::LCM( int val1, int val2 ) { int num1, num2, lcm; //set variables num1 and num2 to the two passed in values num1 = val1; num2 = val2; //set lcm to the first passed in value lcm = num1; //while lcm is less than or equal to the product of num1 and num2 while( lcm <= num1 * num2 ) { //if the remainder of lcm divided by num1 is 0 and // the remainder of lcm divided by num2 is 0, get out of the loop if( lcm % num1 == 0 and lcm % num2 == 0 ) break; //increment lcm by 1 lcm++; }//end while //return the least common multiple return lcm; }
int main()
main() will be used to test the various constructors and methods of the Rational class. It should start by creating 5 Rational objects.
- Rational object 1 should be created with the default constructor.
- Rational object 2 should be created with the alternate constructor and should have a numerator of 11 and a denominator of 30.
- Rational object 3 should be created with the alternate constructor and should have a numerator of 2 and a denominator of 30.
- Rational object 4 should be created with the alternate constructor and should have a numerator of 5 and a denominator of 6.
- Rational object 5 should be created with the alternate constructor and should have a numerator of 3 and a denominator of 7.
Use the 5 Rational objects to perform the following operations. The output that is produced by each step should have informative labels such as “The default constructor produces: “. Use the output at the end of the assignment write-up for an idea of how the results should be displayed.
- Verify the results of the default constructor by displaying the contents of Rational object 1 as a fraction by calling the displayFraction method for the object.
- Verify the results of the alternate constructor by displaying the contents of Rational objects 2, 3, 4, and 5 as a fraction and as floating point values by calling the display method for each of the objects.
- Display the contents of Rational object 3 as a fraction by calling the displayFraction method for the object, change the numerator of the object to 8 by calling the setNumerator method, and call the displayFraction method to display the updated object value.
- Display the contents of Rational object 4 as a fraction by calling the displayFraction method for the object, change the denominator of the object to 73 by calling the setDenominator method, and call the displayFraction method to display the updated object value.
- Display the contents of Rational object 1 as a fraction by calling the displayFraction method for the object, change the numerator to 4 and the denominator to 6 by calling the setRational method for the object, and call the displayFraction method to display the updated object value.
Now test the arithmetic methods that were written. They should all display formulas that contain what is being added/subtracted/etc… and the result as both a fraction and floating point value. For example:
5/73 + 3/7 = 254/511 or 0.49706
- Add 3/7 to Rational object 4 by using the add method. The resulting sum should be saved in a 6th Rational object. Call the display method to display the 6th Rational object as a fraction and floating point value.
- Add 3/7 to the 6th Rational object. The resulting sum should be saved in the 6th Rational object. Call display for the 6th Rational object.
- Add 1/2 to Rational object 3. Save the resulting sum in a 7th Rational object. Call the display method for the 7th Rational object.
- Add 15/16 to Rational object 1. Save the resulting sum in an 8th Rational object. Call the display method for the 8th Rational object.
- Subtract 5/7 from the 6th Rational object by using the subtract method. The resulting difference should be saved in a 9th Rational object. Call the display method to display the 9th Rational object as a fraction and floating point value.
- Subtract 3/6 from the 7th Rational object. The resulting difference should be saved in a 10th Rational object. Call the display method for the 10th Rational object.
- Subtract 101/117 from the 8th Rational object. The resulting difference should be saved in an 11th Rational object. Call the display method for the 11th Rational object.
- Multiply the 9th Rational object by 1/1 by using the multiply method. The resulting product should be saved in a 12th Rational object. Call the display method to display the 12th Rational object as a fraction and floating point value.
- Multiply the 10th Rational object by 7/30. The resulting product should be saved in a 13th Rational object. Call the display method to display the 13th Rational object.
- Multiply the 11th Rational object by 2/4. The resulting product should be saved in a 14th Rational object. Call the display method to display the 14th Rational object.
- Divide the 9th Rational object by 5/8 by using the divide method. The result should be saved in a 15th Rational object. Call the display method to display the 15th Rational object as a fraction and floating point value.
- Divide the 13th Rational object by 5/15. The result should be saved in a 16th Rational object. Call the display method to display the 16th Rational object.
- Divide Rational object 5 by 2/4. The result should be saved in a 17th Rational object. Call the display method to display the 17th Rational object.
Programming Requirements
- Each method MUST have a documentation box like a function.
- Hand in a copy of the source code using Blackboard.
Output
The output should resemble the following:
***** Testing the constructors ***** The default constructor produces 1/1 The alternate constructor with 11, 30 produces 11/30 or 0.36667 with 2, 30 produces 1/15 or 0.06667 with 5, 6 produces 5/6 or 0.83333 with 3, 7 produces 3/7 or 0.42857 ***** Testing the set methods ***** setNumerator(8) changes 1/15 to 8/15 setDenominator(73) changes 5/6 to 5/73 setRational(4, 6) changes 1/1 to 2/3 ***** Testing the add method ***** 5/73 + 3/7 = 254/511 or 0.49706 254/511 + 3/7 = 473/511 or 0.92564 8/15 + 1/2 = 31/30 or 1.03333 2/3 + 15/16 = 77/48 or 1.60417 ***** Testing the subtract method ***** 473/511 - 5/7 = 108/511 or 0.21135 31/30 - 3/6 = 8/15 or 0.53333 77/48 - 101/117 = 1387/1872 or 0.74092 ***** Testing the multiply method ***** 108/511 * 1/1 = 108/511 or 0.21135 8/15 * 7/30 = 28/225 or 0.12444 1387/1872 * 2/4 = 1387/3744 or 0.37046 ***** Testing the divide method ***** 108/511 / 5/8 = 864/2555 or 0.33816 28/225 / 5/15 = 28/75 or 0.37333 3/7 / 2/4 = 6/7 or 0.85714