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

[15 points] Add two constructors, a default constructor that assigns the value 0

ID: 3810946 • Letter: #

Question

[15 points] Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction.

Since Fractions cannot have denominators of 0, the default constructor should assign 0 to the numerator and 1 to the denominator.

[15 points] Add the const keyword to your class wherever appropriate. Your class may still work correctly even if you don't do this correctly, so this will require extra care!!

[5 points] Add a private "simplify()" function to your class and call it from the appropriate member functions. (There will probably be 5 places where you need to call it.) The best way to do this is to make the function a void function with no parameters that reduces the calling object.

As you can see from the sample output given below, you are still not required to change improper Fractions into mixed numbers for printing. Just print it as an improper Fraction. Make sure that your class will reduce ANY Fraction, not just the Fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In other words, you should ensure that all Fraction objects are reduced before the end of any member function. You are also not required to deal with negative numbers, either in the numerator or the denominator.

You must create your own algorithm for reducing Fractions. Don't look up an already existing algorithm for reducing Fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don't use Euclid's algorithm. Don't worry about being efficient. It's fine to have your function check every possible factor, even if it would be more efficient to just check prime numbers. Just create something of your own that works correctly on ANY Fraction.

Note: this part of the assignment is worth 5 points. If you are having trouble keeping up with the class, I suggest you skip this part and take the 5 point deduction.

[10 points] Put the client program in a separate file from the class, and divide the class into specification file (Fraction.h) and implementation file (Fraction.cpp), so your code will be in 3 separate files.

Add documentation to your assignment. Be sure to carefully read section 1D of the Style Conventions, "Commenting in Classes".

Your class should still have exactly two data members.

I am providing a client program for you below. You should copy and paste this and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results. Since you are not writing the client program, you are not required to include comments in it.

Here is the client program.

This client should produce the output shown here:

You may not change the client program in any way. Changing the client program will result in a grade of 0 on the project.

Explanation / Answer

Fraction.h

#ifndef FRACTION_H
#define FRACTION_H
class Fraction
{
public:
Fraction();
Fraction(int numer,int denom);
//Setter and getter methods
void setNumerator(int);
void setDenominator(int);
  
int getNumerator()const;
int getDenominator()const;

//Function declarations
void set(int num,int denom);
void print()const;
Fraction multipliedBy(const Fraction& f)const;
Fraction dividedBy(const Fraction& f)const;
Fraction addedTO(const Fraction& f)const;
Fraction subtract(const Fraction& f)const;
bool isEqualTo(const Fraction& f);
void simplify(const Fraction& f)const;

private:
mutable int numer;
mutable int denom;

};
#endif

________________

Fraction.cpp

#include <iostream>
using namespace std;
#include "Fraction.h"

Fraction::Fraction()
{
this->numer=0;
this->denom=1;
}
Fraction::Fraction(int num,int den)
{
numer=num;
denom=den;
}


//Setters and getters
void Fraction::setNumerator(int n)
{
this->numer=n;
}
void Fraction::setDenominator(int n)
{
this->denom=n;
}
int Fraction::getNumerator()const
{
return numer;
}
int Fraction::getDenominator()const
{
return denom;
}
void Fraction::set(int num,int denom)
{
this->numer=num;
this->denom=denom;
  
}
void Fraction::print()const
{
cout<<getNumerator()<<"/"<<getDenominator();
}
//Implementing the addedTo function
Fraction Fraction::addedTO(const Fraction& f )const
{
Fraction frac;
int a=numer;
int b=denom;
int c=f.numer;
int d=f.denom;
int sumnumer=(a*d + b*c);
int denom=(b*d);
frac.setNumerator(sumnumer);
frac.setDenominator(denom);
// simplify(frac);
return frac;
  
}
//Implementing the subtract function
Fraction Fraction::subtract(const Fraction& f )const
{
Fraction frac;
int a=this->numer;
int b=this->denom;
int c=f.numer;
int d=f.denom;
int subnumer=(a*d - b*c);
int denom=(b*d);
frac.setNumerator(subnumer);
frac.setDenominator(denom);
// simplify(frac);
return frac;
}
//Implementing the multiplied function
Fraction Fraction::multipliedBy(const Fraction& f )const
{
Fraction frac;
int a=this->numer;
int b=this->denom;
int c=f.numer;
int d=f.denom;
int mulnumer=(a*c);
int muldenom=(b*d);
frac.setNumerator(mulnumer);
frac.setDenominator(muldenom);
// simplify(frac);
return frac;
}
//Implementing the divided function
Fraction Fraction::dividedBy(const Fraction& f )const
{
Fraction frac;
int a=this->numer;
int b=this->denom;
int c=f.numer;
int d=f.denom;
int divnumer=(a*d);
int divdenom=(c*b);
  
frac.setNumerator(divnumer);
frac.setDenominator(divdenom);
// simplify(frac);
return frac;
}
//Implementing the isEqualTo function
bool Fraction::isEqualTo(const Fraction& f)
{
int a=this->numer;
int b=this->denom;
int c=f.numer;
int d=f.denom;
if(a*d==c*b)
return true;
else
return false;
}

void Fraction::simplify(const Fraction& f)const
{
int div;
if(f.numer < f.denom)
div = f.denom;
else if(f.numer > f.denom)
div= f.numer;
else
div=f.numer;


for(int i=div;i>=0;i--)
{
if(f.numer%i==0 && f.denom%i==0)
{
f.numer= f.numer/i;
f.denom=f.denom/i;
}
}


}

___________________

main.cpp

#include <iostream>
using namespace std;
#include "Fraction.h"
int main()
{
//Creating the Fraction class Objects
Fraction f1;
Fraction f2;
Fraction result;

cout << "The result starts off at ";
result.print();
cout << endl;
f1.set(9,8);
f2.set(2,3);
  
cout<<"The Product of ";
f1.print();
cout<<" and ";
f2.print();
cout<<" is ";
result=f1.multipliedBy(f2);
result.print();
cout<<endl;
  
cout<<"The quotient of ";
f1.print();
cout<<" and ";
f2.print();
cout<<" is ";
result=f1.dividedBy(f2);
result.print();
cout<<endl;
  
cout<<"The Sum of ";
f1.print();
cout<<" and ";
f2.print();
cout<<" is ";
result=f1.addedTO(f2);
result.print();
cout<<endl;
  
cout<<"The Difference of ";
f1.print();
cout<<" and ";
f2.print();
cout<<" is ";
result=f1.subtract(f2);
result.print();
cout<<endl;
  
if(f1.isEqualTo(f2))
cout<<"The Two Fractions are equal."<<endl;
else
cout<<"The Two Fractions are not equal."<<endl;
  
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.multipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;

}

__________________

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote