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 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...
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...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to do fraction arithmetic. To get you used to the idea of unit testing, this homework does not require a main method. You can create one if you find it useful, however, we will not be grading or even looking at that code. You should be comfortable enough with the accuracy of your test cases that you do not need to use print statements or...
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...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT