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 "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,...
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...
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...
Write in C++ language. (Employee Record): Create a class named 'Staff' having the following members: Data...
Write in C++ language. (Employee Record): Create a class named 'Staff' having the following members: Data members - Id – Name - Phone number – Address - AgeIt also has a function named 'printSalary' which prints the salary of the staff.Two classes 'Employee' and 'Officer' inherits the 'Staff' class. The 'Employee' and 'Officer' classes have data members 'Top Skill' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a officer by making an...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
Create separate class with these members a, b, c, x, y, z int a b c...
Create separate class with these members a, b, c, x, y, z int a b c float x y z Demonstrate 3) A two arg float, int constructor, and a three arg int, float, float constructor to instantiate objects, initialize variables read from the keyboard, display the sum Note:- Please type and execute this above java program and also give the output for both problems. (Type a java program)
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT