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

*****************C++ CODE*************C++ CODE***********C++ CODE************C++

ID: 3744785 • Letter: #

Question

*****************C++ CODE*************C++ CODE***********C++ CODE************C++ CODE**********************

********************************************************************************************************************************

**************PLEASE CODE IN C++**************CODE USING C++*************C++*************************************

Note:

1. You just need to complete the skeleton codes in two files:

1) the interface Frac.h

*************************************************************************************************

// a Fraction object holds one Fraction number, one fraction

#ifndef FRAC_H

#define FRAC_H

#include

using namespace std;

class Fraction { // not fully commented

public:

Fraction(int = 0, int = 1); // default constructor

Fraction add(const Fraction &);

/* add the prototypes of required methods here.

...

*/

void printFractionAsFloat();

private:

int numerator;

int denominator;

void reduce(); // utility function, reduce to lowest

terms

};

#endif

************************************************************************************************

2) the document and the implementation Frac.cpp

***********************************************************************************************************

// a Fraction object holds one Fraction number, one fraction

#include "Frac.h"

//------------------------------ Fraction --------------------------------

----

// default constructor: parameters are numerator and denominator

respectively

// if the number is negative, the negative is always stored in the

numerator

Fraction::Fraction(int n, int d) {

numerator = (d < 0 ? -n : n);

denominator = (d < 0 ? -d : d);

reduce();

}

//(a)--------------------------------- add -------------------------------

-------

// overloaded +: addition of 2 Fractions, current object and parameter

Fraction Fraction::add(const Fraction& a) {

Fraction t;

t.numerator = a.numerator * denominator + a.denominator * numerator;

t.denominator = a.denominator * denominator;

t.reduce();

return t;

}

//(b)------------------------------ subtract -----------------------------

-------

// subtraction of 2 Fractions, current object and parameter

//(c)------------------------------ multiply -----------------------------

-------

// multiplication of 2 Fractions, current object and parameter

//(d)-------------------------------- divide -----------------------------

-------

// division of 2 Fractions, current object and parameter,

// division by zero crashes

************************************************************************************************************************************************************

2. A driver program (do not change it!) to test your class:

FracDriver.cpp

*********************************************************************************************************************

// Fraction Driver part 1

#include "Frac.h"

int main() {

Fraction x(-3,8), y(-15,-20), z, w(5);

x.printFraction();

cout << " + ";

y.printFraction();

z = x.add(y);

cout << " = ";

z.printFraction();

cout << endl;

z.printFraction();

cout << " = ";

z.printFractionAsFloat();

cout << endl << endl;

x.printFraction();

cout << " - ";

y.printFraction();

z = x.subtract(y);

cout << " = ";

z.printFraction();

cout << endl;

z.printFraction();

cout << " = ";

z.printFractionAsFloat();

cout << endl << endl;

x.printFraction();

cout << " * ";

y.printFraction();

z = x.multiply(y);

cout << " = ";

z.printFraction();

cout << endl;

z.printFraction();

cout << " = ";

z.printFractionAsFloat();

cout << endl << endl;

x.printFraction();

cout << " / ";

y.printFraction();

z = x.divide(y);

cout << " = ";

z.printFraction();

cout << endl;

z.printFraction();

cout << " = ";

z.printFractionAsFloat();

cout << endl;

return 0;

}

/*Sample run:

- 3/8 + 3/4 = 3/8

3/8 = 0.375

- 3/8 - 3/4 = -9/8

- 9/8 = -1.125

- 3/8 * 3/4 = -9/32

- 9/32 = -0.28125

- 3/8 / 3/4 = -1/2

- 1/2 = -0.5

Press any key to continue . . .

*/

***************************************************************************************************************************************************************************

3. For submission, you need to upload the two source files: your Frac.h and Frac.cpp (please do NOT zip them)

Create a class called Fraction for performing arithmetic with fractions. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor function that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions for each of the following: a) Addition of two Fraction numbers. See the solution as an example below. b) Subtraction of two Fraction numbers. c) Multiplication of two Fraction numbers. d) Division of two Fraction numbers e) Printing Fraction numbers in the form a/b where a is the numerator and b is the denominator f) Printing Fraction numbers in double floating-point format.

Explanation / Answer

**********************************

Frac.h

**********************************


#ifndef FRAC_H

#define FRAC_H

#include <iostream>

using namespace std;

class Fraction { // not fully commented
public:

Fraction(int = 0, int = 1); // default constructor
Fraction add(const Fraction &);
Fraction subtract(const Fraction &);
Fraction multiply(const Fraction &);
Fraction divide(const Fraction &);
void printFractionAsFloat();
void printFraction();

private:

int numerator;
int denominator;
void reduce(); // utility function, reduce to lowest terms

};

#endif

********************************

Frac.cpp

********************************

#include "Frac.h"

Fraction::Fraction(int n, int d) {

numerator = (d < 0 ? -n : n);

denominator = (d < 0 ? -d : d);

reduce();

}

Fraction Fraction::add(const Fraction& a) {

Fraction t;

t.numerator = a.numerator * denominator + a.denominator * numerator;

t.denominator = a.denominator * denominator;

t.reduce();

return t;

}

Fraction Fraction::subtract(const Fraction& a) {

Fraction t;

t.numerator = a.denominator * numerator - a.numerator * denominator;

t.denominator = a.denominator * denominator;

t.reduce();

return t;

}

Fraction Fraction::multiply(const Fraction& a) {

Fraction t;

t.numerator = a.numerator * numerator;

t.denominator = a.denominator * denominator;

t.reduce();

return t;

}

Fraction Fraction::divide(const Fraction& a) {

Fraction t;

try

{

if(denominator == 0)

{

throw denominator;

}

if(a.numerator == 0)

{

throw a.numerator;

}

t.numerator = a.denominator * numerator;

t.denominator = a.numerator * denominator;

t.reduce();

}catch(int i)

{

cout<<"Divide By Zero Exception ";

}

return t;

}

void Fraction::reduce()

{

int n1 = abs(numerator);

int n2 = abs(denominator);

while(n1 != n2)

{

if(n1 > n2)

n1 -= n2;

else

n2 -= n1;

}

numerator = numerator/n1;

denominator = denominator/n1;

}

void Fraction::printFractionAsFloat()

{

double value = numerator/denominator;

cout<<value;

}

void Fraction::printFraction()

{

cout<<numerator<<"/"<<denominator;

}