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

Below is a program that includes a class, main program and client program: Class

ID: 3765902 • Letter: B

Question

Below is a program that includes a class, main program and client program:

Class Fraction:

#ifndef _fraction_H
#define _fraction_H

class fraction{
public: //declare the public class fraction
int numerator;
int denominator;
fraction(); //parent fraction function   
fraction(int num, int denum); //fraction function with parameters
void reference();
fraction multipliedBy(fraction f) const; //multiply operation
fraction dividedBy(fraction f) const; //divide operation
fraction addedTo(fraction f) const; //addition operation
fraction subtract(fraction f) const; //subtraction operation
bool isEqualTo(fraction f) const; //compare both fractions for equality
void print() const; //prints the value of a "fraction" as numerator/denominator
void set(int num, int denum);


};

#endif

Main Program:

Main program


#include <iostream>
#include "fraction.h"

using namespace std;

//This is the set function, that calls the class fraction. This sets the value num (from the client program) equal to the value of the numerator

void fraction::set(int num, int denom)
{

numerator=num; //set the value of num from client file equal to numerator
denominator=denom; //set the value of denom from client file equal to denominator

}

//This function sets the appropriate numerator and denominator values for the final result

void fraction::reference(){
int firstNumber;
int secondNumber;
int result;
int val;

if (numerator >= denominator){ //if numerator is >= denominator set firstNumber=numerator
firstNumber = numerator;
secondNumber = denominator;
}
else {
firstNumber = denominator;
secondNumber = numerator;
}
result = 2;
val = 1;
while (result <= secondNumber){
while ((secondNumber % result) == 0){ //when remainder is equal to 0
secondNumber = secondNumber / result; //perform the division operation (denominators are equal)
if ((firstNumber % result) == 0){
val *= result;
firstNumber = firstNumber / result;
}
}
result++;
}
numerator /= val;
denominator /= val;
}

//In function fraction() set numerator = 0 and denominator = 1

fraction::fraction() {

numerator = 0;
denominator = 1;
}


//In function fraction user parameter num and denom to set these equal to numerator and denominator

fraction::fraction(int num, int denom) {
numerator = num;
denominator = denom;
reference(); // call function reference
}

//prints numerator over denominator

void fraction::print() const {

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

}


//value returning function (addedTo)

fraction fraction::addedTo(fraction f) const{
int num;
int denom;
denom = denominator * (f.denominator); // added denominator is first fraction denominator * second fraction denominator
num = (numerator * (f.denominator)) + (denominator * (f.numerator)); // added numerator is (first fraction numerator * second fraction denominator) added to (first fraction denominator * second fraction numerator)
fraction result;
result.numerator = num;
result.denominator = denom;
result.reference();
return result;
}


//value returning function (subtract)

fraction fraction::subtract(fraction f) const{
int num;
int denom;

denom = denominator * (f.denominator); //multiplies denominators
num = (numerator * (f.denominator)) - (denominator * (f.numerator)); //performs operation on numerator
fraction result;   
result.numerator = num; //sets result numerator equal to num
result.denominator = denom; //set result denominator equal to denom
result.reference();
return result;
}


//value returning function (multiplication)


fraction fraction::multipliedBy(fraction f) const{
int num;
int denom;
num = numerator * (f.numerator); //multiplies numerators
denom = denominator * (f.denominator); //multiplies denominators
fraction result;
result.numerator = num; //sets numerator result equal to num
result.denominator = denom; //sets denominator result equal to denom
result.reference();
return result;
}

//value returning function (multiplication)

fraction fraction::dividedBy(fraction f) const{
int num;
int denom;
num = numerator * (f.denominator); //multiplies numerators
denom = denominator * (f.numerator); //multiplies first fraction denominator by second fraction numerator
fraction result;
result.numerator = num; //sets numerator result equal to num
result.denominator = denom; //sets denominator result equal to denom
result.reference();
return result;
}


//compares fractions to determine equality


bool fraction::isEqualTo(fraction f) const{   
if (numerator == f.numerator && denominator == f.denominator){
return true; //returns true if fractions are equal
}
else{
return false; //returns false if fractions are not equal
}
}

Client Program:

This client should produce the output shown here:

1. Delete your set() function. 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.

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

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

Explanation / Answer

Delete your set() function. 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.

class Fraction
{
private:
    int F_nNumerator;
    int F_nDenominator;

public:
    Fraction()
    {
         F_nNumerator = 0;
         F_nDenominator = 1;
    }

    Fraction(int Numerator, int Denominator=1)
    {
        assert(Denominator != 0);
        F_nNumerator = Numerator;
        F_nDenominator =Denominator;
    }

    int GetNumerator() { return F_nNumerator; }
    int GetDenominator() { return F_nDenominator; }
    double GetFraction() { return static_cast<double>(F_nNumerator) / F_nDenominator; }
};
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!!
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.

client module:

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

void read(const char* msg11, Fraction& f) {
     int no, deno;
     cout << "Enter " << msg11; << endl;
     cout << "the Numerator : ";
     cin >> no;
     cout << " the Denominator : ";
     cin >> deno;
     f = Fraction(no, deno);
}

int main() {
     Fraction leftop, rightop, resultop, refop;

     cout << "The Fraction Calculator ";
     cout << "=================== ";

     read("Left Operand : ", leftop);
     read("Right Operand : ", rightop);

     cout << "The Result : " << endl;
     leftop.display();
     cout << " + ";
     rightop.display();
     cout << " = ";
     resultop = leftop + rightop;
     resultop.display();
     cout << endl;

     read("2nd Right the Operand : ", rightop);

     cout << "The Result : " << endl;
     resultop.display();
     cout << " += ";
     rightop.display();
     cout << " => ";
     resultop += rightop;
     resultop.display();
     cout << endl;

     read("Reference : ", refop);
     if (resultop == refop)
         cout << "Result == Reference" << endl;
     else
         cout << "Result != Reference" << endl;
}


Main program:

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

void read(const char* msg11, Fraction& f) {
     int no, deno;
     cout << "The Enter " << msg11 << endl;
     cout << " The Numerator : ";
     cin >> no;
     cout << " The Denominator : ";
     cin >> deno;
     f = Fraction(no, deno);
     cin.ignore();
}

int main() {
     char opa[3];
     bool quit = false;
     Fraction leftop, rightop, resultop, refop;

     cout << "Fraction of Calculator ";
     cout << "=================== ";

     read("Left Operand : ", leftop);

     do {
         cout << "+= -= *= /= ## to quit : ";
         cin.get(opa, 3);
         char c11 = cin.get();
         if (c11 != ' ' ||
          opa[1] != '=' && opa[1] != '#') {
             cerr << "Try Again!" << endl;
             cin.ignore(2000, ' ');
         }
         else if (strcmp(opa, "##") == 0) {
             read("The Reference : ", refop);
             quit = true;
         }
         else {
             read("Right Operand : ", rightop);

             cout << "Result : " << endl;
             leftop.display();

             switch (opa[0]) {
                 case '+':
                     cout << " += ";
                     leftop += rightop;
                     break;
                 case '-':
                     cout << " -= ";
                     leftop -= rightop;
                     break;
                 case '*':
                     cout << " *= ";
                     leftop *= rightop;
                     break;
                 case '/':
                     cout << " /= ";
                     leftop /= rightop;
                     break;
             }
             rightop.display();
             cout << " => ";
             leftop.display();
             cout << endl;
         }
     } while (!quit);

     if (leftop == refop)
         cout << "Result == Reference"
              << endl;
     else
         cout << "Result != Reference"
              << 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