Question

In: Computer Science

Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...

Create a c++ class called Fraction in which the objects will represent fractions.

Remember:

  •   do not reduce fractions,

  • do not use "const,"

  • do not provide any constructors,

  • do not use three separate files.

  • You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program.

It is required that you provide these 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 has to have exactly two data members, one in order to represent the numerator of the Fraction being represented, and the other to represent the denominator of the Fraction that is being represented.

  • Hint for how you will set up your arithmetic operation functions: You need 2 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/ subtracting Fractions, keep in mind 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.

  • Use the program below. You have to 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 highly recommend for you to design your class incrementally. Ex: you should first implement only the set function and the output function, and then test what you have so far. After this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You can do this by creating your own client program to test the code at each stage; but, 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.(you may not change it or else you will not receive credit)

#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;

    }

}

This should be the output:

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.


Solutions

Expert Solution

Please find all the explanation to the code within the code itself as comments.

We need to create a Fraction class with the data members and member functions as specified in the problem statement.

We won't make any changes to the Client code and run our class code corresponding to it.

The output for the code has been added in the end and it matches the output specified in the problem statement.

Please note that we have used this pointer notation in order to distinguish the data members of the object on which the member functions are called and the data members of the object that are being passed as an argument with the member functions.

(

this pointer is an implicit pointer that points to the object in consideration. It is associated with all the non-static data members and member functions of the class and is provided by the compiler. We can use it to reference our object which is under consideration. For example, in our Fraction class, we can access the non-static data member numerator by using this pointer:

cout << this -> numerator << "\n";

like we did in the case of our print () function. As this is a pointer, we access the data associated with the corresponding object using the arrow( -> ) operator.

)

Here is the full code:



#include <iostream>
using namespace std;

// class definition
class Fraction {
                
        // two data members

        // one representing numerator
        int numerator;

        // other, representing denominator
        int denominator;

        public:

                void set (int numerator, int denominator);
                Fraction addedTo (Fraction f);
                Fraction subtract (Fraction f);
                Fraction multipliedBy (Fraction f);
                Fraction dividedBy (Fraction f);
                bool isEqualTo (Fraction f);
                void print ();
};


// set () functions sets up the attributes of the Fraction object
void Fraction :: set (int numerator, int denominator) {
        this -> numerator = numerator;
        this -> denominator = denominator;
}

// this function adds Fraction argument f the with the calling Fraction object
Fraction Fraction :: addedTo (Fraction f) {
                                
        Fraction result;

        // according to the problem statement, we can get the denominator of the sum
        // by multiplying the denominators of the two fractions
        result.denominator = (this -> denominator) * f.denominator;

        // we can find the numerator by cross multiplying the numerators and denominators and adding them
        result.numerator = ((this -> numerator) * f.denominator) + ((this -> denominator) * f.numerator);

        // return the result Fraction
        return result;
}

// this function subtracts the Fraction argument f from the calling Fraction object
Fraction Fraction :: subtract (Fraction f) {

        Fraction result;

        // according to the problem statement, we can get the denominator of the difference 
        // by multiplying the denominators of the two fractions
        result.denominator = (this -> denominator) * f.denominator;

        // we can find the numerator by cross multiplying the numerators and denominators and subtracting them
        result.numerator = ((this -> numerator) * f.denominator) - ((this -> denominator) * f.numerator);

        // return the result Fraction
        return result;
}

// this function multiplies the Fraction argument f by the calling Fraction object 
Fraction Fraction ::multipliedBy (Fraction f) {
                                
        Fraction result;

        // according to the problem statement, we can get the product of the two fractions
        // by multiplying the Fractions corresponding parameters with each other
        result.numerator = (this -> numerator) * f.numerator;
        result.denominator = (this -> denominator) * f.denominator;

        // return the result Fraction
        return result;
}

// this function divides the Fraction argument f by the calling Fraction object 
Fraction Fraction :: dividedBy (Fraction f) {
                                
        Fraction result;

        // we have found the this by using the fact that 
        // the dividing a Fraction by another is same as multiplying
        // the reciprocal of the Fraction with the other
        
        Fraction reciprocal;
        reciprocal.numerator = f.denominator;
        reciprocal.denominator = f.numerator;

    // Now, that we have the reciprocal, we will now multiply the 
                // reciprocal object with the object on which the dividedBy () function is called
        result.numerator = (this -> numerator) * reciprocal.numerator;
        result.denominator = (this -> denominator) * reciprocal.denominator;

        // return the result Fraction
        return result;
}

// this function checks if the Fraction argument f is equal to the calling Fraction object
bool Fraction :: isEqualTo (Fraction f) {
        // as provided in the problem statement
        // we can know that if the two fractions are 
        // equal or not by using this if condition

        if (((this -> numerator) * f.denominator) == ((this -> denominator) * f.numerator)) {
                return true;
        }

        // otherwise
        return false;
}

// this function will print the Fraction in the specified format
void Fraction :: print () {
        // we will print the Fraction as specified in the problem statement
        // numerator/denominator
        cout << (this -> numerator) << "/" << (this -> denominator);
}



// client program provided in the 
// problem statement
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;
    }
}

Please refer to the screenshots of the code for understanding the indentation.

Let's look at the output of the code when run with the client code provided in the problem statement:


Related Solutions

JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to...
JAVA FRACTIONS QUESTION: You will create a Fraction class in Java to represent fractions and to do fraction arithmetic. To get you used to the idea of unit testing, this homework does not require a main method. You can create one if you find it useful, however, we will not be grading or even looking at that code. You should be comfortable enough with the accuracy of your test cases that you do not need to use print statements or...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables-a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
Create a C# Application. Create a class object called “Employee” which includes the following private variables:...
Create a C# Application. Create a class object called “Employee” which includes the following private variables: firstN lastN idNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods: constructor properties CalcPay(): Calculate the regular pay and overtime pay. Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT