Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

make the change to the program given below so that if setDenominator receive 0,

ID: 655689 • Letter: M

Question

make the change to the program given below so that if setDenominator receive 0, it does nothing. Don't just arbitrarily make it 2.

. Expand the following Fraction class to include all of the following  (all examples are for a fraction half that has a numerator of 1 and a denominator of 2):

*Overload ==, <, <=, >, >= operators (use the decimal values to compare);

*Overload = operator (assigns the numerator and denominator to the fraction);

*.Overload ++ so that 1/3 becomes 2/3 for example.

.*Overload -- so that 2/3 becomes 1/3 for eaxample.

.*It is OK if the numerator is negative.

include a representation of the class and

Sample output showing several different fractions and an example where the user tried to assign 0 to the denominator. Your output should show all of the methods listed above

the program is given here:

#include <iostream>
#include <string>
using namespace std;

// Fraction class declaration
class Fraction
{
public:
// The constructors
Fraction();
Fraction(int n, int d);

// getter and setter Function(method)

int getNumerator();
int getDenominator();


void setNumerator(int n);
void setDenominator(int d);

// other functions
double toDecimal();
const string toString();
void reduce();

private:
int numerator;
int denominator;
};

// Fraction class implementation
Fraction::Fraction()
{
numerator = 1;
denominator = 2;
}

Fraction::Fraction(int n, int d)
{
setNumerator(n);
setDenominator(d);
}

int Fraction::getNumerator()
{
    return numerator;
}

int Fraction::getDenominator()
{
   return denominator;
}

void Fraction::setNumerator(int n)
{
numerator = n;
}

void Fraction::setDenominator(int d)
{
if(d != 0)
     denominator = d;
else
   denominator = 2;
}

double Fraction::toDecimal()
{
return (double)numerator / denominator;
}

const string Fraction::toString()
{
string result = "";

result.append(to_string((long double)numerator));
result.append("/");
result.append(to_string((long double)denominator));

return result;
}

void Fraction::reduce()
{
    int n = numerator;
int d = denominator;
int gcd = 1;
int largest;

    if(n < 0)
         n = -n;

    if(n > d)
         largest = n;
    else
         largest = d;
   
    for(int i = largest; i >= 2; i--)
{
         if(n % i == 0 && d % i == 0)
  {
             gcd = i;
             break;
        }
    }

    numerator = numerator / gcd;
    denominator = denominator / gcd;   
}

// testing the Fraction class
int main()
{
Fraction f(11, 33);
  
cout << "Numerator: " << f.getNumerator() << endl;
cout << "Denominator: " << f.getDenominator() << endl;
cout << "Fraction: " << f.toString() << endl;

f.reduce();

cout << "When reduce: " << f.toString() << endl;
cout << "In decimal: " << f.toDecimal() << endl;

cout << endl;
system("pause");
return 0;
}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

// Fraction class declaration
class Fraction
{
public:
// The constructors
Fraction();
Fraction(int n, int d);

// getter and setter Function(method)

int getNumerator();
int getDenominator();


void setNumerator(int n);
void setDenominator(int d);

// other functions
double toDecimal();
const string toString();
void reduce();

private:
int numerator;
int denominator;
};

// Fraction class implementation
Fraction::Fraction()
{
numerator = 1;
denominator = 2;
}

Fraction::Fraction(int n, int d)
{
setNumerator(n);
setDenominator(d);
}

int Fraction::getNumerator()
{
return numerator;
}

int Fraction::getDenominator()
{
return denominator;
}

void Fraction::setNumerator(int n)
{
numerator = n;
}

void Fraction::setDenominator(int d)
{
if(d != 0)
denominator = d;
//else
// denominator = 2;
//}

double Fraction::toDecimal()
{
return (double)numerator / denominator;
}

const string Fraction::toString()
{
string result = "";

result.append(to_string((long double)numerator));
result.append("/");
result.append(to_string((long double)denominator));

return result;
}

void Fraction::reduce()
{
int n = numerator;
int d = denominator;
int gcd = 1;
int largest;

if(n < 0)
n = -n;

if(n > d)
largest = n;
else
largest = d;

for(int i = largest; i >= 2; i--)
{
if(n % i == 0 && d % i == 0)
{
gcd = i;
break;
}
}

numerator = numerator / gcd;
denominator = denominator / gcd;   
}

// testing the Fraction class
int main()
{
Fraction f(11, 33);
  
cout << "Numerator: " << f.getNumerator() << endl;
cout << "Denominator: " << f.getDenominator() << endl;
cout << "Fraction: " << f.toString() << endl;

f.reduce();

cout << "When reduce: " << f.toString() << endl;
cout << "In decimal: " << f.toDecimal() << endl;

cout << endl;
system("pause");
return 0;
}

In the above program I have made three lines as comment to get result.