aWrite a FractionDemo program that instantiates several Frac

a.Write a FractionDemo program that instantiates several Fraction objects and demonstrates that their methods work correctly. Create a Fraction class with fields that hold a whole number, a numerator, and a denominator. In addition:

•Create properties for each field. The set accessor for the denominator should not allow a 0 value; the value defaults to 1.

•Add three constructors. One takes three parameters for a whole number, numerator, and denominator. Another accepts two parameters for the numerator and denominator; when this constructor is used, the whole number value is 0. The last constructor is parameterless; it sets the whole number and numerator to 0 and the denominator to 1. (After construction, Fractions do not have to be reduced to proper form. For example, even though 3/9 could be reduced to 1/3, your constructors do not have to perform this task.)

•Add a Reduce() method that reduces a Fraction if it is in improper form. For example, 2/4 should be reduced to 1/2.

•Add an operator+() method that adds two Fractions. To add two fractions, first eliminate any whole number part of the value. For example, 2 1/4 becomes 9/4 and 1 3/5 becomes 8/5. Find a common denominator and convert the fractions to it. For example, when adding 9/4 and 8/5, you can convert them to 45/20 and 32/20. Then you can add the numerators, giving 77/20. Finally, call the Reduce() method to reduce the result, restoring any whole number value so the fractional part of the number is less than 1. For example, 77/20 becomes 3 17/20.

•Include a function that returns a string that contains a Fraction in the usual display format—the whole number, a space, the numerator, a slash (/), and a denominator. When the whole number is 0, just the Fraction part of the value should be displayed (for example, 1/2 instead of 0 1/2). If the numerator is 0, just the whole number should be displayed (for example, 2 instead of 2 0/3).

b.Add an operator*() method to the Fraction class created in Exercise 11a so that it correctly multiplies two Fractions. The result should be in proper, reduced format. Demonstrate that the method works correctly in a program named FractionDemo2.

c.Write a program named FractionDemo3 that includes an array of four Fractions. Prompt the user for values for each. Display every possible combination of addition results and every possible combination of multiplication results for each Fraction pair (that is, each type will have 16 results).

Solution

PROGRAM CODE:

/*
* fraction.cpp
*
* Created on: 28-Nov-2016
* Author: kasturi
*/

#include <iostream>
#include <string>

using namespace std;
class Fraction
{
private:
   int wholeNumber;
   int numerator;
   int denominator;
public:
   Fraction(int Number, int num, int denom) {
       setWholeNumber(Number);
       setNumerator(num);
       setDenominator(denom);
   }

   Fraction(int num, int denom)
   {
       setWholeNumber(0);
       setNumerator(num);
       setDenominator(denom);
   }

   Fraction()
   {
       setWholeNumber(0);
       setNumerator(0);
       setDenominator(1);
   }

   int getWholeNumber() {
       return wholeNumber;
   }

   void setWholeNumber(int Number) {
       wholeNumber = Number;
   }

   int getNumerator() {
       return numerator;
   }

   void setNumerator(int num) {
       numerator = num;
   }

   int getDenominator() {
       return denominator;
   }

   void setDenominator(int denom) {
       if(denom == 0)
       {
           denominator = 1;
       }else denominator = denom;
   }

   void Reduce()
   {
       /*if(denominator%numerator == 0)
       {
           denominator = denominator/numerator;
           numerator = numerator/numerator;
       }
       else*/
       //{
           if(numerator%2 == 0 && denominator%2 == 0)
           {
               numerator = numerator /2;
               denominator = denominator/2;
               Reduce();
           }
           else if(numerator%3 == 0 && denominator %3 == 0)
           {
               numerator = numerator/3;
               denominator = denominator/3;
               Reduce();
           }
           else if(numerator%5 == 0 && denominator %5 == 0)
           {
               numerator = numerator /5;
               denominator = denominator/5;
               Reduce();
           }
           else if(numerator%7 == 0 && denominator %7 == 0)
           {
               numerator = numerator /7;
               denominator = denominator/7;
               Reduce();
           }
       //}

       if(numerator > denominator && wholeNumber == 0)
       {
           wholeNumber = numerator/denominator;
           numerator = numerator % denominator;
       }

   }

   Fraction operator+(Fraction f1)
   {
       int commonDen = this->denominator * f1.denominator;
       int numerator1 = (this->wholeNumber * this->denominator + this->numerator) * f1.denominator;
       int numerator2 = (f1.wholeNumber * f1.denominator + f1.numerator) * this->denominator;
       int newNumerator = numerator1 + numerator2;
       //cout<<commonDen<<\" \"<<numerator1<<\" \"<<numerator2<<\" \"<<newNumerator<<endl;
       Fraction f2(0, newNumerator, commonDen);
       f2.Reduce();
       return f2;
   }

   Fraction operator*(Fraction f1)
   {
       int denom = this->denominator * f1.denominator;
       int numerator1 = this->wholeNumber * this->denominator + this->numerator;
       int numerator2 = f1.wholeNumber * f1.denominator + f1.numerator;
       int newNumerator = numerator1 * numerator2;
       Fraction f2(0, newNumerator, denom);
       f2.Reduce();
       return f2;
   }

   string toString()
   {
       //cout<<wholeNumber<<numerator<<denominator<<endl;
       return (wholeNumber==0?\"\": to_string(wholeNumber) + \" \") + (numerator==0?\"\":(to_string(numerator) + \"/\" + to_string(denominator)));
   }
};

int main()
{
   Fraction frac1(2,3,4), frac2(0, 6,12);
   frac1.Reduce();
   frac2.Reduce();
   Fraction f3 = frac1 + frac2;
   Fraction f4 = frac1 * frac2;
   cout<<frac1.toString()<<endl;
   cout<<frac2.toString()<<endl;
   cout<<f3.toString()<<endl;
   cout<<f4.toString()<<endl;
   return 0;
}

OUTPUT:

2 3/4

1/2

3 1/4

1 3/8

a.Write a FractionDemo program that instantiates several Fraction objects and demonstrates that their methods work correctly. Create a Fraction class with field
a.Write a FractionDemo program that instantiates several Fraction objects and demonstrates that their methods work correctly. Create a Fraction class with field
a.Write a FractionDemo program that instantiates several Fraction objects and demonstrates that their methods work correctly. Create a Fraction class with field
a.Write a FractionDemo program that instantiates several Fraction objects and demonstrates that their methods work correctly. Create a Fraction class with field

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site