1 answer

C++...please help follow exact steps output needs to be the exact same Create a class called...

Question:

Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when its declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction would be stored in the object as I in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: a) Adding two Rational numbers. The result should be stored in reduced form. b) Subtracting two Rational numbers. The result should be stored in reduced form. c) Multiplying two Rational numbers. The result should be stored in reduced form. d) Dividing two Rational numbers. The result should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator 0 Printing Rational numbers in floating-point format Create an object of class Rational in the main function and test all functions. Sum of vo frutiom+ ch a ad - ch b dbd Differna of tro fradions Product of two fractions:?× e) ad bc Division of taro fradtions: 73+778-29724 29/24 = 1.20833 3-7/813/24 13/24-0.541667 1/3 x 7/8-7/24 724 = 0.291667 1/3 1 718-8/21 8/21-0, 380952

C++...please help follow exact steps output needs to be the exact same

Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction would be stored in the object as I in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: a) Adding two Rational numbers. The result should be stored in reduced form. b) Subtracting two Rational numbers. The result should be stored in reduced form. c) Multiplying two Rational numbers. The result should be stored in reduced form. d) Dividing two Rational numbers. The result should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator 0 Printing Rational numbers in floating-point format Create an object of class Rational in the main function and test all functions. Sum of vo frutiom+ ch a ad - ch b dbd Differna of tro fradions Product of two fractions:?× e) ad bc Division of taro fradtions: 73+778-29724 29/24 = 1.20833 3-7/813/24 13/24-0.541667 1/3 x 7/8-7/24 724 = 0.291667 1/3 1 718-8/21 8/21-0, 380952

Answers

#include<bits/stdc++.h>

using namespace std;

/*

a/b+c/d=(a*d+b*c)/b*d

a/b-c/d=(a*d-b*c)/b*d

a/b*c/d=(a*c)/b*d

*/

class Rational

{ private:

int numerator,denominator; //private variables

public:

//constructor

Rational()

{

numerator=0;

denominator=1;

simplify();//to simplify numerator and denominator

}

//simplifies the fraction

void simplify()

{

int x=__gcd(numerator,denominator); //find gcd

numerator=numerator/x; //divide the gcd

denominator=denominator/x;

}

//constructor

Rational(int numerator,int denominator)

{

setNumerator(numerator); //set numerator

setDenominator(denominator);//set denominator

simplify();

}

//add function , returns a Rational object

Rational add( Rational obj)//add

{

Rational temp,r;

//finding numerator

int n=numerator*obj.denominator+obj.numerator*denominator;

int d=denominator*obj.denominator; //denominator

//simplify fraction

//move - sign to numerator

if(d<0)

{

d=d*-1;

n=n*-1;

}

//return new object

return Rational(n,d);

}

Rational sub(Rational obj)//subtract

{

Rational temp,r;

//find numerator

int n=numerator*obj.denominator-obj.numerator*denominator;

int d=denominator*obj.denominator;

//simplify the fraction(4/4 is reduced to 1/1)

int x=__gcd(n,d);

n=n/x;

d=d/x;

//for -ve sign

if(d<0)

{

d=d*-1;

n=n*-1;

}

return Rational(n,d);

}

Rational multiply(Rational obj)//multiply

{

Rational temp,r;

//numerator

int n=numerator*obj.numerator;

int d=denominator*obj.denominator;

//simplify

int x=__gcd(n,d);

n=n/x;

d=d/x;

//-ve sign

if(d<0)

{

d=d*-1;

n=n*-1;

}

return Rational(n,d);

}

Rational divide(Rational obj)

{

int n=numerator*obj.denominator;

int d=denominator*obj.numerator;

return Rational(n,d);

}

//function to set numerator

void setNumerator(int n)

{

numerator=n;

}

//function to set denominator

void setDenominator(int n)

{

denominator=n;

}

void printRational()

{

cout<<numerator<<"/"<<denominator;

}

void printFloat()

{

cout<<(float)numerator/denominator<<endl;

}

};

int main(int argc, char const *argv[])

{

/* code */

Rational a(1,3),b(7,8),c;

c=a.add(b); //add

a.printRational();//print a

cout<<" + ";

b.printRational(); //print b

cout<<" = " ; //print =

//add a+b

c.printRational(); //print c

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

//subtraction

c=a.sub(b);//a-b

a.printRational();

cout<<" - ";

b.printRational();

cout<<" = " ;

//add a+b

c.printRational(); //print

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

c=a.multiply(b);

a.printRational();

cout<<" * ";

b.printRational();

cout<<" = " ;

//add a+b

c.printRational(); //print

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

c=a.divide(b);

a.printRational();

cout<<" / ";

b.printRational();

cout<<" = " ;

//add a+b

c.printRational(); //print

cout<<endl;

c.printRational();

cout<<" = ";

c.printFloat();

cout<<endl;

return 0;

}

.

Similar Solved Questions

1 answer
The following programaverage stores integer numbers in onlarray coled table and compute the average of innermost...
The following programaverage stores integer numbers in onlarray coled table and compute the average of innermost array The average of each integer or ay is stored in other array called some Answer the three below using this program public class averageMultidis public static void main(String[] args) ...
1 answer
Calculate the molar solubility of Fe(OH)2 in a buffer solution where the pH has been fixed...
Calculate the molar solubility of Fe(OH)2 in a buffer solution where the pH has been fixed at the indicated values. Ksp = 7.9 x 10-16. (a) pH 13.2...
1 answer
Which of the following is true about the filtrate of the glomerulus? A It contains little...
Which of the following is true about the filtrate of the glomerulus? A It contains little or no glucose B It contains little or no protein C It contains little or no sodium D It contains little or no urea E It contains little or no potassium...
1 answer
How do I use the method of moments to find the estimate of #theta# in the pdf #f(x) = {[theta e^(–thetax)",", x >= 0],[0",","otherwise"]:}#?
How do I use the method of moments to find the estimate of #theta# in the pdf #f(x) = {[theta e^(–thetax)",", x >= 0],[0",","otherwise"]:}#?...
1 answer
Brrect Question 10 0/1 pts Primate social adaptations like physical adaptations to environment come with, a....
brrect Question 10 0/1 pts Primate social adaptations like physical adaptations to environment come with, a. Advantages to food acquisition b. Trades off to support fitness c. Issue dealing with predation d. Mutations e. None of these...
1 answer
Why was it necessary to isolate mitochondria to study and also use engineered vesicles that either...
why was it necessary to isolate mitochondria to study and also use engineered vesicles that either did or did not contain atp synthetase? by the first ATP s r aasing the enzyme to function as an ATPase and result in ATP ATP synthase would likely drive the ATP synh reverse direction, s Original Paper...
2 answers
A bar of unknown material is subjected to an axial tension load P. The bar, having...
A bar of unknown material is subjected to an axial tension load P. The bar, having initial dimensions L = 500 mm and a = b = 25 mm, is observed to elongate by 20 mm. Further, it is observed that the volume of the bar increases by 600 mm3. The bulk modulus of the material is measured at 20,000 MPa. A...
1 answer
Instructions Prepare an incremental analysis to determine whether the current machine should be replaced. E7.15 (LO...
Instructions Prepare an incremental analysis to determine whether the current machine should be replaced. E7.15 (LO 6), AN Veronica Mars, a recent graduate of Bell's accounting program, evaluated the operating performance of Dunn Company's six divisions. Veronica made the following presentat...
1 answer
Converget 1) Determine the given dBouned tive, increasing ,dlecreasin tive or ar alternotin ard...
Converget 1) Determine the given dBouned tive, increasing ,dlecreasin tive or ar alternotin ard converpnt, d a) an-In na :25. ot , divergent. b) an n-0 - ni(バ Converget 1) Determine the given dBouned tive, increasing ,dlecreasin tive or ar alternotin ard converpnt, d a) an-In na :25. ot , d...
1 answer
4. Zn(s) + H2SO4(aq) → ZnSO4(0) + H2 50.0g of H2SO4 produce how many grams of...
4. Zn(s) + H2SO4(aq) → ZnSO4(0) + H2 50.0g of H2SO4 produce how many grams of H? Type of Rxn 5. Nas) + H2O + NaOH(aq) + H2O) 5.00 moles of H2O produce how many moles of H2? Type of Rxn 6. Cus) + Se → CuS Type of Rxn How many grams of S are needed to produce 10.0g of Cus?...
1 answer
How might the ways in which we produce, deliver, and pay for care be changed in...
How might the ways in which we produce, deliver, and pay for care be changed in order to increase access, improve quality, and the the cost of healthcare services? Please include references in the response....
1 answer
10. Graph two periods of the following function. Label key x values. State the period and...
10. Graph two periods of the following function. Label key x values. State the period and list the key x values. y=sin Period: 21 Key x values:...
1 answer
40. The Screw Right Company claims their 34 inch screws are within ±0.23 of the claimed mean diameter of 0.750 inches with a standard deviation of 0.115 inches. The following data were recorded. 0.757...
40. The Screw Right Company claims their 34 inch screws are within ±0.23 of the claimed mean diameter of 0.750 inches with a standard deviation of 0.115 inches. The following data were recorded. 0.757 0.723 0.754 0.737 0.757 0.741 0.722 0.741 0.743 0.742 0.740 0.758 0.724 0.739 0.736 0.735 0....
1 answer
3) BBE ended their first year of business with the following rights and obligations [Please complete...
3) BBE ended their first year of business with the following rights and obligations [Please complete independently from #2] (create only a balance sheet): • BBE owns a delivery truck with a gross value of $200,000, of which $50,000 was depreciated in the first year. • BBE is owed $25,000 ...
1 answer
Please show all of your work and please make sure your handwriting is clear and easy...
Please show all of your work and please make sure your handwriting is clear and easy to read. Thank you! elasticity of substitution 10. Compared to isoquant Qı, production for isoquant Q2 has the and the elasticity of labor demand. a. lower; lower b. higher; higher c. higher; lower d. lower; hi...