2 Part C++ Programming AssignmentCode must be exactly as prescribed in prompt (Attached)
Line documentation for all blocks of code please.
11/24/2020 CSCI 240 - Fall 2020 - Program 9 faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm9.htm 1/8 CSCI 240 Fall 2020 Program 9 Classes (100 points) Overview 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() 11/24/2020 CSCI 240 - Fall 2020 - Program 9 faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm9.htm 2/8 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. 11/24/2020 CSCI 240 - Fall 2020 - Program 9 faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm9.htm 3/8 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 ) 11/24/2020 CSCI 240 - Fall 2020 - Program 9 faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm9.htm 4/8 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