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

I need help with this problem and notating it Write a fraction class whose objec

ID: 3769039 • Letter: I

Question

I need help with this problem and notating it

Write a fraction class whose objects will represent fractions. You should provide the following member functions: Two constructors, a default constructor which 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. (6 pts for specification and implementation) Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as four value returning functions that return a fraction object. They should be named AddedTo, Subtract, MultipliedBy, and DividedBy. Note the returned fraction object must be in reduced form. (20 pts for specification and implementation) A boolean operation named isGreaterThan that compares two fraction objects to determine whether one fraction object is greater than the other. (5 pts for specification and implementation) An input operation named getFraction that prompts the user for two integer values to be used to define a new fraction object. One value is to be used for the numerator and the other for the denominator of the fraction object. on the screen in the form numerator/denominator. (5 pts for specification and implementation) An output operation named showFraction that displays the value of a fraction object on the screen in the form numerator/denominator. (4 pts for specification and implementation) NOTE: You may not implement the above member functions as inline functions but should include the function member declarations (or prototypes) in the class specification file (fraction.h) and then write the member function definitions in the implementation file (fraction.cpp). Your class specification should have exactly two data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented. When adding or subtracting fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator. I am providing a client program for you below. You should copy and paste this into a file and use it as your client program that will be a part of your mutlitfile project. The output that should be produced when the provided client program is run with your completed class specification and implementation files is also given below, so that you can check your results. Since this project requires multiple files you will need to first create a new project in your compiler and then write the source code for your class specification file, the implementation file and then add the client code below to the project. For an example with explanations of such C++ class related multifile projects review the Software Engineering Tip: Seperating Class, Specification, Implementation, and Client Code in Chapter 7.11 in your Gaddis textbook (pgs. 446-452) I strongly suggest that you design your class incrementally. For example, you should first implement only the constructors and the output function, comment out parts of the client code that is not needed and then test what you have so far. Once this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program and comment out code that relates to member functions that you have not yet implemented. As you can see from the sample output given below, except for the arithmetic operations you are not required to change improper fractions into mixed numbers for printing. Just print it as an improper fraction. You are, however, required to reduce fractions that are the result of an arithmetic operation. See the reduced fraction result for the multiplication and division in the sample output. You are not required to deal with negative numbers, either in the numerator or the denominator. Here is the client program. /*Client.cpp is set-up as a fraction class implementation and test driver program - DO NOT CHANGE SOURCE CODE All necessary class objects are declared and defined here to test various class related operations. HINT: see comments for specific class related function calls*/ #include #include "fraction.h" using namespace std; int main() { fraction f1(9,8); //calling a parameterized class constructor fraction f2(2,3); fraction result; //calling a default class constructor const fraction f3(12, 8); const fraction f4(202, 303); fraction f5,f6; cout<<"C++ CLASS MULTI-FILE PROJECT"< This is what I have but #using "function.h" is coming up as a n error in xcode::::: // fraction.h #ifndef FRACTION_H #define FRACTION_H class fraction{ public: int gcd(int a, int b); int num, den; fraction(); fraction(int n, int d); fraction AddedTo(fraction f); fraction Subtract(fraction f); fraction MultipliedBy(fraction f); fraction DividedBy(fraction f); bool isGreaterThan(fraction f); void getFraction(); void showFraction(); }; #endif // fraction.cpp #include "fraction.h" #include using namespace std; int fraction::gcd(int a, int b){ if(b == 0) return a; else return gcd(b, a % b); } fraction::fraction(){ num = 0; den = 1; } fraction::fraction(int n, int d){ num = n; den = d; } fraction fraction::AddedTo(fraction f){ int n, d; fraction temp; n = num * f.den + den * f.num; d = den * f.den; temp.num = n / gcd(n, d); temp.den = d % gcd(n, d); return temp; } fraction fraction::Subtract(fraction f){ int n, d; fraction temp; n = num * f.den - den * f.num; d = den * f.den; temp.num = n / gcd(n, d); temp.den = d % gcd(n, d); return temp; } fraction fraction::MultipliedBy(fraction f){ int n, d; fraction temp; n = num * f.num; d = den * f.den; temp.num = n / gcd(n, d); temp.den = d % gcd(n, d); return temp; } fraction fraction::DividedBy(fraction f){ int n, d; fraction temp; n = num * f.den; d = den * f.num; temp.num = n / gcd(n, d); temp.den = d % gcd(n, d); return temp; } bool fraction::isGreaterThan(fraction f){ if(num * f.den > den * f.num) return true; else return false; } void fraction::getFraction(){ cout << "Enter the numerator: "; cin >> num; cout << "Enter the deniminator: "; cin >> den; } void fraction::showFraction(){ cout << num / gcd(num, den) << " / " << den / gcd(num, den); } // a.cpp #include #include "fraction.h" using namespace std; int main() { fraction f1(9,8); //calling a parameterized class constructor fraction f2(2,3); fraction result; //calling a default class constructor const fraction f3(12, 8); const fraction f4(202, 303); fraction f5,f6; cout<<"C++ CLASS MULTI-FILE PROJECT"<

Explanation / Answer

#include "fraction.h"

using namespace std;

class fraction
{
      private:
              int numerator;
              int denominator;
           
            
    
      public:
      fraction ();
      fraction (int,int);
      fraction AddedTo (fraction value) const;
      fraction MultipliedBy (fraction value) const;
      fraction Subtract (fraction value) const;
      fraction DividedBy (fraction value) const;
       fraction isGreaterThan ();
      fraction isEqualTo ();
      fraction print() const;
};

#include "fraction.h"
using namespace std;

int main()
{
    fraction f1(9,8);
    fraction f2(2,3);
    fraction result;
    fraction f3;

    cout << "The result starts off at ";
    result.print();
    cout << endl;

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.MultipliedBy(f2);
    result.print();
    cout << endl;
  
    f3 = result;
     if (f2.isGreaterThan(f3))
     {
        f2.print();
        cout <<" is greater than ";
        f3.print();
    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 f4(12, 8);
    const fraction f5(202, 303);

    result = f4.DividedBy(f5);
    cout << "The quotient of ";
    f4.print();
    cout << " and ";
    f5.print();
    cout << " is ";
    result.print();
    cout << endl;

    system ("PAUSE");
    return 0;
}


fraction::fraction()
{
numerator = 0;
denominator = 1;                   
}

fraction::fraction(int newNumerator, int newDenominator)
{
numerator=newNumerator;
denominator=newDenominator;
}

fraction fraction::AddedTo (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)+(value.numerator*denominator);   
result.denominator = denominator * value.denominator;
return result;

}

fraction fraction::Subtract (fraction value) const
{
   fraction result;
result.numerator = (numerator * value.denominator)-(value.numerator*denominator);   
result.denominator = denominator * value.denominator;
return result;      
}

fraction fraction::MultipliedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.numerator;     
result.denominator = denominator * value.denominator;
return result;
}

fraction fraction::DividedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.denominator;   
result.denominator = denominator * value.numerator;
return result;       
}

bool fraction::isGreaterThan (fraction value)
{
int fraction1;
int fraction2;
fraction1=(numerator/denominator);
fraction2=(value.numerator/value.denominator);
fraction1>fraction2;
}

bool fraction::isEqualTo(fraction value)
{
(numerator/denominator)==(value.numerator/value.denominator);
}

fraction fraction::print() const
{
int num=numerator;
int den=denominator;
   if(num>den)
   {
   for(int counter=2;counterwhile(num%counter==0 & den%counter==0)
           {
           num=(num/counter);
           den=(den/counter);
           }
           }
   }
   else
     if(den>num)
   {
   for(int counter=2;counterwhile(num%counter==0 & den%counter==0)
           {
           num=(num/counter);
           den=(den/counter);
           }
           }
   }