Question

In: Computer Science

Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...

Programming II: C++ - Programming Assignment

Fraction Object with Operator Overloads

Overview

In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object.

When completing this assignment, the student should demonstrate mastery of the following concepts:

· Mathematical Modeling - Fractions

· Operator Overloading – Binary Operators (Internal Overload)

· Operator Overloading – Binary Operator (External Overload)

Assignment

Write a C++ object that implements a “fraction” object. In more precise terms, your object will contain the data needed to represent a rational expression. In mathematics, it is understood that all of the real numbers can be defined as the quotient of two integer values. Your fraction object should contain attributes that represent these two integers. When implementing your fraction object, you should minimally include the following components:

· You should provide a constructor method that initialized the rational expression to (0/1).

· You should provide a constructor method that allows client code to manually set integer values for the numerator and denominator.

· Your object should restrict mutation of itself so only valid rational expressions exist within the object (i.e. a value of 4/0 should be rejected).

· Your object should contain a series of getter and setter methods that allow safe mutation of the numerator and denominator.

· Your getter and setter methods must contain meaningful interfaces that communicate whether values were effectively modified by client code requests through return values. The driver code you write to demonstrate the object should also demonstrate these interfaces.

· Your object should contain overloaded binary arithmetic operators for addition, subtraction, multiplication, and division.

· Your object should always reduce the value represented by the contained rational expression to its lowest terms.

· Your object should contain a binary external operator overload for the stream insertion (<<) operator.

· Your driver code should instantiate a few instances of fractions and demonstrate all the arithmetic operations and displaying the object’s information on the console with overloaded behavior when interacting

with cout (i.e. Your driver should have a statement resembling: cout << myFractionObject;

Solutions

Expert Solution


Fraction.h

#ifndef Fraction_H
#define Fraction_H
#include<iostream>
using namespace std;

class Fraction{

    private:
        int numerator;
        int denominator;
        int GCD(int, int);

    public:
        Fraction();
        Fraction(int,int);
        void setNumerator(int);
        void setDenominator(int);
        int getNumerator();
        int getDenominator();
        Fraction operator +(Fraction);
        Fraction operator -(Fraction);
        Fraction operator *(Fraction);
        Fraction operator /(Fraction);
        friend ostream & operator << (ostream &, const Fraction&);
};
#include"Fraction.cpp"

#endif







Fraction.cpp


#include"Fraction.h"
using namespace std;

// private member function to calculate
// gcd, to help reducing the Fraction to 
// simplest term
int Fraction::GCD(int n, int d)
{
    int result;
    if(n % d == 0)
         result = d;
    else
        result = GCD(d, n%d);
    return result;
}

// defualt constructor
Fraction::Fraction()
{
    numerator = 0;
    denominator = 1;
}

// parameterized constructor
// to initialize the fraction with user defined
// numerator and denominator
Fraction::Fraction(int n, int d)
{
    // checking whether denominator is not 0
    if(d != 0)
    {
        // reducing the numbers
        int gcd = GCD(n, d);
        setNumerator(n/gcd);
        setDenominator(d/gcd);
    }
    // else make the fraction as 0
    else
    {
        numerator = 0;
        denominator = 1;
    }

}

// setting the numerator
void Fraction::setNumerator(int n)
{
    numerator = n;
}

// setting the denominator
void Fraction::setDenominator(int d)
{
    denominator = d;
}

// returning the numerator
int Fraction::getNumerator()
{
    return numerator;
}

// returning the denominator
int Fraction::getDenominator()
{
    return denominator;
}

// overloaded addition operator
Fraction Fraction::operator +(Fraction f)
{
    int newNumer = (numerator * f.getDenominator() + denominator*f.getNumerator());
    int newDenom = (denominator * f.getDenominator());
    Fraction result(newNumer, newDenom);
    return result;
}

// overloaded subtraction operator
Fraction Fraction::operator -(Fraction f)
{
    int newNumer = (numerator * f.getDenominator() - denominator*f.getNumerator());
    int newDenom = (denominator * f.getDenominator());
    Fraction result(newNumer, newDenom);
    return result;
}

// overloaded multiplication operator
Fraction Fraction::operator *(Fraction f)
{
    int newNumer = numerator*f.getNumerator();
    int newDenom = denominator*f.getDenominator();
    Fraction result(newNumer, newDenom);
    return result;
}

// overloaded division operator
Fraction Fraction::operator /(Fraction f)
{
    int newNumer = (numerator*f.getDenominator());
    int newDenom = (denominator*f.getNumerator());
    Fraction result(newNumer, newDenom);
    return result;
}

// overloaded extraction operator for cout
ostream & operator <<(ostream &out, const Fraction &f)
{
    Fraction newF = f;
    if(f.numerator < 0 && f.denominator > 0)
    {
        newF.setNumerator(newF.numerator*-1);
        out << "-" << newF.numerator << "/" << newF.denominator;
     }
    else if(f.numerator > 0 && f.denominator < 0)
    {
        newF.setDenominator(newF.denominator * -1);
        out << "-" << newF.numerator << "/" << newF.denominator;
     }
     else
     {
        out << f.numerator << "/" << f.denominator;
     }
    return out;
}




Driver.cpp


#include"Fraction.h"

int main()
{
    // creating fraction objects
    Fraction myFractionObject1(2, 7);
    Fraction myFractionObject2(3, 7);

    // performing the binary operations
    Fraction addedObjects = myFractionObject1 + myFractionObject2;
    Fraction subtractedObjects = myFractionObject1 - myFractionObject2;
    Fraction multipliedObjects = myFractionObject1 * myFractionObject2;
    Fraction deividedObjects = myFractionObject1 / myFractionObject2;

    // printing the results
    cout << "myFractionObject1 = " << myFractionObject1 << endl;
    cout << "myFractionObject2 = " << myFractionObject2 << endl << endl;
    cout << "Sum of " << myFractionObject1 << " + " << myFractionObject2 
         << " = " << addedObjects << endl;
    cout << "Difference of " << myFractionObject1 << " - " << myFractionObject2 
         << " = " << subtractedObjects << endl;
    cout << "Product of " << myFractionObject1 << " * " << myFractionObject2 
         << " = " << multipliedObjects << endl;
    cout << "Division of " << myFractionObject1 << " / " << myFractionObject2 
         << " = " << deividedObjects << endl;
}

Related Solutions

Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions. Please keep all of your code in one file for this week. We will separate...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT