I\'ve been having trouble getting this program completed. A complete program tha
ID: 3622108 • Letter: I
Question
I've been having trouble getting this program completed. A complete program that I could analyse and model from would be ideal! Thanks!
For this assignment, implement the methods for a class called Rational that will be used to represent rational numbers (ie. fractions).
Rational class
Use the following class definition:
class Rational
{
public:
Rational(); //default constructor
Rational( int, int );
void display();
void displayFloat();
void setNumerator( int );
void setDenominator( int );
void setRational( int, int );
void add( int, int );
void subtract( int, int );
void multiply( int, int );
void divide( int, int );
private:
int numerator;
int denominator;
int GCD( int, int );
int LCM( int, int );
};
Data Members
The data members for the Rational class are:
* numerator this is an integer that holds the numerator (or top number) of the fraction.
* denominator this is an integer that holds the denominator (or bottom number) of the fraction.
Constructors
There are two constructors for this class.
The first one is the default constructor. It takes no arguments. Its sole purpose is to create an object with both the numerator and denominator to set to 1.
The second constructor is used to create an object that contains a specific fractional value. It takes two integer arguments: the numerator and denominator values, respectively, that should be used to initialize the data members. The resulting fraction should be in its reduced form. For example, if the passed in numerator value is 2 and the passed in denominator value is 6, then the data member numerator will contain 1 and denominator will contain 3. Hint: think about using the GCD method described below.
Public Methods
void display()
This method displays the rational number in its fractional form (ie. numerator/denominator). It takes no arguments and returns nothing.
void displayFloat()
This method displays the rational number in its decimal form with 5 digits after the decimal point. It takes no arguments and returns nothing.
void setNumerator( int newNumerator )
This method is used to change the numerator value. It takes one argument: an integer that is the new numerator value. It returns nothing. Don't forget that the rational number is being stored in its reduced form, therefore it's possible that the denominator value could also change.
void setDenominator( int newDenominator )
This method is used to change the denominator value. It takes one argument: an integer that is the new denominator value. It returns nothing. Don't forget that the rational number is being stored in its reduced form, therefore it's possible that the numerator value could also change.
void setRational( int newNumerator, int newDenominator )
This method is used to change the value of the rational number. It takes two arguments: an integer that holds the new numerator value and an integer that holds the new denominator value. It returns nothing. Don't forget to store the rational number in its reduced form.
void add( int addNumerator, int addDenominator )
This method adds another rational number to an existing rational number. It takes two arguments an integer that represents the numerator of the rational number being added to the current instance and an integer that represents the denominator of the rational number being added to the current instance. It returns nothing.
Before two rational numbers can be added, they must both have the same denominator. Therefore, the first thing to do is to find the least common multiple of the two denominators. This will be the common denominator. Once the common denominator has been found, calculate the amount that both the numerator and denominator will be multiplied by so that the current instance has the common denominator value. Repeat this for the passed in values as well.
At this point, the two fractions should have the same denominator value. Now the numerator values can be added. Don't forget to make sure that the resulting rational number is in its reduced form.
For example, if the current object has a numerator value of 3, denominator value of 11, and add(1,2) is the calling statement, then:
3 1 6 11 17
- + - => - + -- => --
11 2 22 22 22
void add( Rational addRational )
This method does the same thing as the previous add method in that it adds another rational number to an existing rational number. However, its argument list is different. This version of the add method takes one argument a Rational object that should be added to the current instance. It returns nothing.
void subtract( int subNumerator, int subDenominator )
This method subtracts another rational number from an existing rational number. It takes two arguments an integer that represents the numerator of the rational number being subtracted from the current instance and an integer that represents the denominator of the rational number being subtracted from the current instance. It returns nothing.
Before two rational numbers can be subtracted, they must both have the same denominator. Therefore, the first thing to do is to find the least common multiple of the two denominators. This will be the common denominator. Once the common denominator has been found, calculate the amount that both the numerator and denominator will be multiplied by so that the current instance has the common denominator value. Repeat this for the passed in values as well.
At this point, the two fractions should have the same denominator value. Now the passed in numerator value can be subtracted from the current instance's numerator value. Don't forget to make sure that the resulting rational number is in its reduced form.
For example, if the current object has a numerator value of 7, denominator value of 12, and subtract(1,3) is the calling statement, then:
7 1 7 4 3
- - - => - - - => -
12 3 12 12 12
void subtract( Rational subRational )
This method does the same thing as the previous subtract method in that it subtracts another rational number from an existing rational number. However, its argument list is different. This version of the subtract method takes one argument a Rational object that should be subtracted from the current instance. It returns nothing.
void multiply( int multNumerator, int multDenominator )
This method multiplies two rational numbers. It takes two arguments an integer that represents the numerator of the rational number the current instance is being multiplied by and an integer that represents the denominator of the rational number the current instance is being multiplied by. It returns nothing.
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. Don't forget to make sure that the resulting rational number is in its reduced form.
For example, if the current object has a numerator value of 7, denominator value of 12, and multiply(1,3) is the calling statement, then:
7 1 7
- * - => -
12 3 36
void multiply( Rational multRational )
This method does the same thing as the previous multiply method in that it multiplies two rational numbers. However, its argument list is different. This version of the multiple method takes one argument a Rational object that the current instance should be multiplied by. It returns nothing.
void divide( int divNumerator, int divDenominator )
This method divides two rational numbers. It takes two arguments an integer that represents the numerator of the rational number the current instance is being divided by and an integer that represents the denominator of the rational number the current instance is being divided by. It returns nothing.
Unlike addition and subtraction, there is no need to find a common denominator. The division operation is actually performed by multiplying the current object by the reciprocal of the passed in values. The reciprocal is found by simply making the numerator the denominator and the denominator the numerator. Don't forget to make sure that the resulting rational number is in its reduced form.
For example, if the current object has a numerator value of 2, denominator value of 73, and divide(5,8) is the calling statement, then:
2 5 2 8 16
- / - => - * - => --
73 8 73 5 365
void divide( Rational divRational )
This method does the same thing as the previous divide method in that it divides two rational numbers. However, its argument list is different. This version of the divide method takes one argument a Rational object that the current instance should be divided by. It returns nothing.
Private Methods
The next two methods are private methods. This means that they will only be used by other methods of the class and will never be called by an "outside" function.
int GCD( int val1, int val2 )
This method calculates and returns the greatest common divisor of the two passed in integers.
To find the greatest common divisor:
set variables x and y to the two passed in values
while (true)
find the remainder when x is divided by y
if the remainder is 0
get out of the loop
endif
set x to the current y value
set y to the remainder
endwhile
After the loop is done executing, the variable y will contain the greatest common divisor.
int LCM( int val1, int val2 )
This method calculates and returns the least common multiple of the two passed in integers.
To find the least common multiple:
set variables x and y to the two passed in values
set lcm to x
while lcm is less than or equal to the product of x and y
if the remainder of lcm divided by x is 0 and the remainder of lcm divided by y is 0
get out of the loop
endif
increment lcm by 1
endwhile
After the loop is done executing, the variable lcm will contain the least common multiple.
main()
In main(), create 5 Rational objects.
*Rational Object 1 should be created with the default constructor.
*Rational Object 2 should be created with the second constructor and should have a numerator of 11 and a denominator of 30.
*Rational Object 3 should be created with the second constructor and should have a numerator of 2 and a denominator of 30.
*Rational Object 4 should be created with the second constructor and should have a numerator of 5 and a denominator of 6.
*Rational Object 5 should be created with the second 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: ".
1.Display the contents of Rational Object 1 as a fraction.
2.Display the contents of Rational Objects 2, 3, and 4 as a fraction AND as floating point values.
3.Display the contents of Rational object 3 as a fraction, change the numerator to 8, and display the new value as a fraction.
4.Display the contents of Rational object 4 as a fraction, change the denominator to 73, and display the new value as a fraction.
5.Display the contents of Rational object 1 as a fraction, change the numerator to 4 and the denominator to with one method call, and display the new value as a fraction.
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
1.Add 3/7 to Rational Object 4 by using the add method that takes two integers as arguments.
2.Add Rational Object 5 to Rational Object 4 by using the add method that takes a Rational object as its argument.
3.Add 1/2 to Rational Object 3 using either one of the add methods.
4.Add 15/16 to Rational Object 1 using either one of the add methods.
5.Subtract 5/7 from Rational Object 4 using the subtract method that takes a Rational object as its argument.
6.Subtract 3/6 from Rational Object 3 using the subtract method that takes two integers as its arguments.
7.Subtract 101/117 from Rational Object 1 using either one of the subtract methods.
8.Multiply Rational Object 4 by 1/1 using the multiply method that takes a Rational object as its argument.
9.Multiply Rational Object 3 by 7/30 using the multiply method that takes two integers as its arguments.
10.Multiply Rational Object 1 by 2/4 using either one of the multiply methods.
11.Divide Rational Object 4 by 5/8 using the divide method that takes a Rational object as its argument.
12.Divide Rational Object 3 by 5/15 using the divide method that takes two integers as its arguments.
13.Divide Rational Object 5 by 2/4 using either one of the divide methods.
Program Notes
In order to receive full credit for this assignment, you must meet the following requirements:
1.Each method must have a documentation box like a function. You may have one documentation box to cover the overloaded constructors and methods, but you MUST mention all of the overloaded methods in the box.
2.Hand in a copy of your source code using Blackboard.
Output
The default constructor produces: 1/1
The second 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
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
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
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
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
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
Explanation / Answer
Dear, #include using namespace std; // Rational class definition class Rational { public : Rational(int = 0, int = 0); // default constructor for Rational // Member functions for the class void add(int, int); void subtract(int, int); void multiply(int, int); void divide(int, int); void print(); void printRational(); private : // private data of the class int numerator; int denominator; }; // end class Time Rational::Rational(int myNumerator, int myDenominator) { // Place the actual values in the class members numerator = myNumerator; denominator = myDenominator; // check for reduced form int lcm = 0; while ( (myNumerator != 0) && (myDenominator % myNumerator != 0) ) { lcm = myDenominator % myNumerator; myDenominator = myNumerator; myNumerator = lcm; } // Make it to reduced form numerator = numerator/lcm; denominator = denominator/lcm; } // end constructor Rational // print the Rational number void Rational::print() { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.