Question

In: Computer Science

C++ Create a class for working with fractions. Only 2 private data members are needed: the...

C++

Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class:

a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced form. Make sure the denominator is not set to 0 or a negative value.
b. Add two fractions and store the sum in reduced form.
c. Subtract two fractions and store the difference in reduced form.
d. Multiply two fractions and store the product in reduced form.
e. Divide two fractions and store the quotient in reduced form.
f. Print a fraction.

g. Change a fraction to its reciprocal. For example, the reciprocal of the fraction 5/8 is 8/5

Your main should instantiate two fractions and call each of the class methods. The two fractions should be printed a long with the sum, difference, product, quotient, and after being changed to its reciprocal. (Please do not overload the operators for this program.)

Solutions

Expert Solution

#include <iostream>

using namespace std;

class Fraction

{

public:

Fraction (int num=1, int den=1);

int gcd(int a, int b);

int get_num( ) const;

int get_den( ) const;

int numerator;

int denominator;

void reduce();

Fraction operator+(Fraction fraction);

Fraction operator/(Fraction fraction);

Fraction operator-(Fraction fraction);

Fraction operator*(Fraction fraction);

};

int Fraction::get_num(void)const {

  return this->numerator;

}

int Fraction::get_den(void)const {

  return this->denominator;

}

Fraction::Fraction(int n , int d) {

  this->numerator = n;

  this->denominator = d;

}

Fraction Fraction::operator*(Fraction fraction) {

  Fraction res;

res.numerator = (this->numerator * fraction.get_num());

  res.denominator = (this->denominator * fraction.get_den());

  return res;

}

Fraction Fraction::operator-(Fraction fraction) {

  Fraction res;

  if (this->denominator == fraction.get_den()) {

    res.numerator = (this->numerator - fraction.get_num());

    res.denominator = (this->denominator);

  } else {

    res.numerator = ((this->numerator * fraction.get_den()) - (fraction.get_num() * this->denominator));

    res.denominator = (this->denominator * fraction.get_den());

  }

  return res;

}

Fraction Fraction::operator+(Fraction fraction) {

  Fraction res;

  if (this->denominator == fraction.get_den()) {

    res.numerator = (this->numerator + fraction.get_num());

    res.denominator = (this->denominator);

  } else {

    res.numerator = ((this->numerator * fraction.get_den()) + (fraction.get_num() * this->denominator));

    res.denominator = (this->denominator * fraction.get_den());

  }

  return res;

}

Fraction Fraction::operator/(Fraction fraction) {

  Fraction res;

  res.denominator = (this->denominator * fraction.get_num());

  res.numerator = (this->numerator * fraction.get_den());

  return res;

}

int Fraction::gcd(int X, int Y)

{

if (X == 0)

return Y;

return gcd(Y%X, X);

}

void Fraction::reduce(void) {

  int g = gcd(this->numerator, this->denominator);

    this->numerator /= g;

    this->denominator /= g;

}

int main()

{

int n;

int num , den;

do{

cout<<"1. Addition"<<endl;

cout<<"2. Substraction"<<endl;

cout<<"3. Multiplication"<<endl;

cout<<"4. Division"<<endl;

cout<<"5. Reduce a fraction"<<endl;

cout<<"6. Show decimal equivalent"<<endl;

cout<<"7. Exit"<<endl;

cout<<"Enter your choice: ";

cin>>n;

switch(n){

case 1:

{

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fa(num, den);

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fb(num, den);

Fraction f = fa + fb;

f.reduce();

cout<<"Result: "<<f.numerator<<"/"<<f.denominator<<endl<<endl;

}

break;

case 2:

{

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fc(num, den);

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fd(num, den);

Fraction f1 = fc - fd;

f1.reduce();

cout<<"Result: "<<f1.numerator<<"/"<<f1.denominator<<endl<<endl;

}

break;

case 3:

{

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fe(num, den);

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction ff(num, den);

Fraction f3 = fe * ff;

f3.reduce();

cout<<"Result: "<<f3.numerator<<"/"<<f3.denominator<<endl<<endl;

}

break;

case 4:

{

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fg(num, den);

cout<<"Enter 1st numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fh(num, den);

Fraction f4 = fg / fh;

f4.reduce();

cout<<"Result: "<<f4.numerator<<"/"<<f4.denominator<<endl<<endl;

}

break;

case 5:

{

cout<<"Enter numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fh(num, den);

fh.reduce();

cout<<"Result: "<<fh.numerator<<"/"<<fh.denominator<<endl<<endl;

}

break;

case 6:

{

cout<<"Enter numerator and denominator (sepatrated by space)";

cin>>num>>den;

Fraction fi(num, den);

cout<<"Result: "<<fi.numerator*1.0/fi.denominator<<endl<<endl;

}

break;

case 7:

exit(0);


}


}while(num != 7);

return 0;

}

===========================================

SEE OUTPUT

PLEASE COMMENT if there is any concern.

=========================================


Related Solutions

Create a class for working with complex numbers. Only 2 private float data members are needed,...
Create a class for working with complex numbers. Only 2 private float data members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class:a. A default constructor that uses default arguments in case no initializers are included in the main.b. Add two complex numbers and store the sum.c. Subtract two complex numbers and store the difference.d. Multiply two complex numbers and store the product.e. Print...
create a class in C++ having the following 4 private members: one double variable representing the...
create a class in C++ having the following 4 private members: one double variable representing the balance of a bank account on string variable representing the bank account number a function to deposit money from the bank account a function to withdraw money from the bank account the following 2 public members: -two wrapper functions. - one parameterless constructor - one constructor initializing the account balance and account number - sample: #include <iostream> using namespace std; class account { public:...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Rectangle Class in C++ Create a Rectangle class that has two data members: length_ and width_....
Rectangle Class in C++ Create a Rectangle class that has two data members: length_ and width_. Create the corresponding accessor and mutator functions to access and change both data members. Create a member function area that returns the area of the Rectangle object. Finally, create a function longest_rectangle that accepts two Rectangle objects as parameters and returns the Rectangle object with the longer length. Write the Rectangle class and the longest_rectangle function prototype in rectangle.hpp. Take note that longest_rectangle is...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items               int xCoord;               int yCoord;               int zCoord; write the setters and getters. They should be inline functions               void setXCoord(int)             void setYCoord(int)            void setZCoord(int)               int getXCoord()                     int getYCoord()                   int getZCoord() write a member function named void display() that displays the data items in the following format      blank line      xCoord is                          ????????      yCoord is                          ????????      zCoord...
Create a "Date" class that contains: three private data members: month day year (I leave it...
Create a "Date" class that contains: three private data members: month day year (I leave it to you to decide the type) "setters" and "getters" for each of the data (6 functions in total) One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive. one default constructor (no arguments) one constructor with three arguments: month, day,...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT