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

this is my assignment Write a fraction class whose objects will represent fracti

ID: 3763946 • Letter: T

Question

this is my assignment

Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions:

A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly.

Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a fraction object. They should be named addedTo, subtract, multipliedBy, and dividedBy. In these functions you will need to declare a local "fraction" variable, assign to it the result of the mathematical operation, and then return it.

A boolean operation named isEqualTo that compares two fraction objects for equality. Since you aren't reducing your fractions, you'll need to do this by cross-multiplying. A little review: if numerator1 * denominator2 equals denominator1 * numerator2, then the fractions are equal.

An output operation named print that displays the value of a fraction object on the screen in the form numerator/denominator.

Your class 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.

Here's a hint for how you will set up your arithmetic operation functions: You need two fractions. One is the parameter, one is the calling object. The function multiplies the calling object times the parameter and returns the result. In some ways it is similar to the comesBefore() function from the lesson. That function needs two fractions, and one is the calling object and one is the parameter.

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 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.

I strongly suggest that you design your class incrementally. For example, you should first implement only the set function and the output function, 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, you are not required to reduce fractions or change improper fractions into mixed numbers for printing. Just print it as an improper fraction. You are also not required to deal with negative numbers, either in the numerator or the denominator.

Before you submit your assignment, make sure to carefully read section 1D of the Style Conventions, "Commenting in Classes".

Here is the client program.

This client should produce the output shown here:

#include <iostream>
using namespace std;
int main()
{
fraction f1;
fraction f2;
fraction result;

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;

system ("PAUSE");//exclude statement if not using Dev-C++
return 0;
}

// FRACTION.H

class fraction
{
public:
fraction (); //default class constructor
fraction (int numerator2, int denominator2); // class constructor
void set (int numerator2, int denominator2);
fraction MultipliedBy (fraction otherfraction) const;
fraction DividedBy (fraction otherfraction) const;
fraction AddedTo (fraction otherfraction) const;
fraction Subtract (fraction otherfraction) const;
bool isEqualTo (fraction otherfraction) const;
void print() const;
private:
int numerator;
int denominator;
};

// END OF FRACTION.H

// FRACTION.CP

// Fraction class implementation file
#include <iostream>
using namespace std;
int gcd(int x,int y)
{
while(x!=y)
{
if(x>y)
return gcd(x-y,y);
else
return gcd(x,y-x);
}
return x;
}
fraction::fraction ()
{
numerator=0;
denominator=1;
}

fraction::fraction (int numerator2, int denominator2)
{
int GCD = gcd(numerator2, denominator2);
numerator=numerator2/GCD;
denominator=denominator2/GCD;
}

void fraction::set (int numerator2, int denominator2)
{
numerator=numerator2;
denominator=denominator2;
}

fraction fraction ::MultipliedBy(fraction otherfraction) const
{
fraction local;
int num = numerator*otherfraction.numerator;
int den = denominator*otherfraction.denominator;
local.set(num/gcd(num,den),den/gcd(num,den));
return local;
}

fraction fraction:: DividedBy (fraction otherfraction) const
{
fraction local;
int num = numerator*otherfraction.denominator;
int den = denominator*otherfraction.numerator;
local.set(num/gcd(num,den),den/gcd(num,den));
return local;
}

fraction fraction ::AddedTo (fraction otherfraction) const
{
fraction local;
int num = numerator*otherfraction.denominator+denominator*otherfraction.numerator;
int den =denominator*otherfraction.denominator;
local.set(num/gcd(num,den),den/gcd(num,den));
return local;
}

fraction fraction ::Subtract (fraction otherfraction) const
{
fraction local;
int num = numerator*otherfraction.denominator-denominator*otherfraction.numerator;
int den =denominator*otherfraction.denominator;
local.set(num/gcd(num,den),den/gcd(num,den));
return local;
}

bool fraction::isEqualTo (fraction otherfraction) const
{
return (numerator == otherfraction.numerator &&denominator == otherfraction.denominator);   
}

void fraction::print() const
{
cout << numerator << "/" << denominator ;
}

//END OF FRACTION.cPP

Explanation / Answer

Hi,

I tried to compile code and getting below error.

'fraction' was not declared in this scope
expected ';' before 'f2'
expected ';' before 'result'
'f1' was not declared in this scope
'f2' was not declared in this scope
'result' was not declared in this scope At global scope:
'fraction' does not name a type
'fraction' does not name a type
'result' does not name a type
'cout' does not name a type
'f3' does not name a type
'cout' does not name a type
'f4' does not name a type
'cout' does not name a type
'result' does not name a type
'cout' does not name a type
expected constructor, destructor, or type conversion before '(' token
expected unqualified-id before 'return'
expected declaration before '}' token

You need to clear these errors.You logic is correct but whatever data type you define not in correct manner.