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

C++ Assignment 12.1 [15 points] This assignment will not be graded for style iss

ID: 3857910 • Letter: C

Question

C++

Assignment 12.1 [15 points]

This assignment will not be graded for style issues. If your code works correctly, you'll get 15 points. Style will be graded next week.

Note: No documentation is required on this assignment

Note: in future weeks you will be submitting the client program and class in separate files, but for this week you will be putting all of your code, class and client program, into a single file to submit it. The class declaration will come first, followed by the definitions of the class member functions, followed by the client program.

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

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.

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

Below is your code: -

#include <iostream>
using namespace std;

class Fraction{
public:
Fraction (); //default class constructor
Fraction (int num2, int den2); // class constructor
void set (int num2, int den2);
Fraction multipliedBy (Fraction temp) ;
Fraction dividedBy (Fraction temp) ;
Fraction addedTo (Fraction temp) ;
Fraction subtract (Fraction temp) ;
bool isEqualTo (Fraction temp) ;
void print() ;

private:
int num;
int den;
};

Fraction::Fraction (){
num=0;
den=1;
}

Fraction::Fraction (int num2, int den2){
num=num2;
den=den2;
}

void Fraction::set (int num2, int den2){
num=num2;
den=den2;
}

Fraction Fraction ::multipliedBy (Fraction temp){
Fraction result;
result.num=this->num*temp.num;
result.den=this->den*temp.den;
return result;
}

Fraction Fraction:: dividedBy (Fraction temp) {
Fraction result;
result.num=this->num*temp.den;
result.den=this->den*temp.num;
return result;
}

Fraction Fraction ::addedTo (Fraction temp) {
Fraction result;
result.den=this->den*temp.den;
result.num=(this->num*temp.den)+(this->den*temp.num);
return result;
}

Fraction Fraction ::subtract (Fraction temp) {
Fraction result;
result.den=this->den*temp.den;
result.num=(this->num*temp.den)-(this->den*temp.num);
return result;
}

bool Fraction::isEqualTo (Fraction temp) {
if((this->num==temp.num)&&(this->den==temp.den))
return true;
else
return false;
}

void Fraction::print() {
cout << this->num << "/" << this->den ;
}
// ------------------------
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;
}
}

// Fraction class specification file

Sample Run: -

The product of 9/8 and 2/3 is 18/24
The quotient of 9/8 and 2/3 is 27/16
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two Fractions are not equal.