Question

In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

  • In C++
  • Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition.


\**************Homework 5 code*****************************/
#include<iostream>
using namespace std;

class Fraction
{

private:
int wholeNumber, numerator, denominator;
  
public:
//get methods
int getWholeNumber()
{
return wholeNumber;
}

int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
Fraction()// default constructor
{
int w,n,d;
cout<<"\nEnter whole number : ";
cin>>w;
cout<<"\nEnter numerator : ";
cin>>n;
cout<<"\nEnter denominator : ";
cin>>d;
while(d == 0)
{
cout<<"\nEnter denominator : ";
cin>>d;
}
wholeNumber = w;
numerator = n;
denominator = d;
}

Fraction(int numerator, int denominator, int wholeNumber = 0)// argument constructor
{
this->wholeNumber = wholeNumber;
this->numerator = numerator;
this->denominator = denominator;
reduceFraction();
}
int gcd (int x, int y) { //function to find greatest common divisor
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}
void reduceFraction()
{

int factor;
factor = gcd(numerator, denominator);
// numerator = numerator / factor;
// denominator= denominator / factor;
}

};

void display(Fraction &f)
{
if(f.getWholeNumber() > 0)
cout << f.getWholeNumber() << " " << f.getNumerator() << "/" << f.getDenominator() ;
else
cout << f.getNumerator() << "/" << f.getDenominator() ;
}

Fraction multiply(Fraction &fraction1,Fraction &fraction2)
{
  
int numerator = (fraction1.getDenominator()*fraction1.getWholeNumber()+ fraction1.getNumerator()) * (fraction2.getDenominator()*fraction2.getWholeNumber()+fraction2.getNumerator());
int denominator = fraction1.getDenominator() * fraction2.getDenominator();
int wholeNumber = numerator/denominator;
numerator = numerator%denominator; // remainder after division
Fraction product(numerator,denominator,wholeNumber);
return product;
  
}

int main()
{
Fraction f1 ,f2;
Fraction product(0,0);

product = multiply(f1,f2);
product.reduceFraction();
cout<<endl;
display(f1);
cout<<" * ";
display(f2);
cout<<" = ";
display(product);
}

  • Create a new class called MathProblem. The MathProblem class should use composition and contain one or more Fractions objects. This class should assist you in giving someone a Fractions quiz.
  • Change both classes from a single files, to a header and source code file.
  • In your main function, ask the user at least 5 questions about fractions and let them know how well they did on the quiz. (Your program should work for any fraction problems, so if I were to replace your questions with different fractions, the program's addition, multiplication, and reduction should still allow you to come up with the correct answers)

When you run the program it should look similar to......

What is 1/2 + 2/3 ?
Enter the whole number part for the fraction: 1
Enter the numerator: 1
Enter a denominator: 6
Correct!

What is 2/3 + 1/4 ?
Enter the whole number part for the fraction: 0
Enter the numerator: 11
Enter a denominator: 12
Correct!

What is 1/4 + 3/4 ?
Enter the whole number part for the fraction: 1
Enter the numerator: 0
Enter a denominator: 1
Correct!

What is 3/4 * 1/3 ?
Enter the whole number part for the fraction: 0
Enter the numerator: 1
Enter a denominator: 4
Correct!

What is 1/3 * 1/5 ?
Enter the whole number part for the fraction: 0
Enter the numerator: 1
Enter a denominator: 3
Sorry, the correct answer is 1/15.

You got 80% correct

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Updated code

#include<iostream>

using namespace std;

class Fraction

{

private:

int wholeNumber, numerator, denominator;

public:

//get methods

int getWholeNumber()

{

return wholeNumber;

}

int getNumerator()

{

return numerator;

}

int getDenominator()

{

return denominator;

}

Fraction()// default constructor

{

int w,n,d;

cout<<"\nEnter whole number : ";

cin>>w;

cout<<"\nEnter numerator : ";

cin>>n;

cout<<"\nEnter denominator : ";

cin>>d;

while(d == 0)

{

cout<<"\nEnter denominator : ";

cin>>d;

}

wholeNumber = w;

numerator = n;

denominator = d;

}

Fraction(int numerator, int denominator, int wholeNumber = 0)// argument constructor

{

this->wholeNumber = wholeNumber;

this->numerator = numerator;

this->denominator = denominator;

reduceFraction();

}

int gcd (int x, int y) { //function to find greatest common divisor

int t;

while (y>0) {

t = x % y;

x = y;

y = t;

}

return x;

}

void reduceFraction()

{

int factor;

factor = gcd(numerator, denominator);

numerator = numerator / factor;

denominator= denominator / factor;

}

};

void display(Fraction &f)

{

if(f.getWholeNumber() > 0)

cout << f.getWholeNumber() << " " << f.getNumerator() << "/" << f.getDenominator() ;

else

cout << f.getNumerator() << "/" << f.getDenominator() ;

}

Fraction multiply(Fraction &fraction1,Fraction &fraction2)

{

  

int numerator = (fraction1.getDenominator()*fraction1.getWholeNumber()+ fraction1.getNumerator()) * (fraction2.getDenominator()*fraction2.getWholeNumber()+fraction2.getNumerator());

int denominator = fraction1.getDenominator() * fraction2.getDenominator();

int wholeNumber = numerator/denominator;

numerator = numerator%denominator; // remainder after division

Fraction product(numerator,denominator,wholeNumber);

return product;

  

}

Fraction addition(Fraction &fraction1,Fraction &fraction2)

{

int numerator = (fraction1.getNumerator()*fraction2.getDenominator()+fraction2.getNumerator()*fraction1.getDenominator());

int denominator = fraction1.getDenominator() * fraction2.getDenominator();

int wholeNumber =fraction1.getWholeNumber()+fraction2.getWholeNumber();

Fraction add(numerator,denominator,wholeNumber);

return add;

}

int main()

{

Fraction f1 ,f2;

Fraction product(0,1);

Fraction add(0,1);

add=addition(f1,f2);

add.reduceFraction();

product = multiply(f1,f2);

product.reduceFraction();

cout<<endl;

display(f1);

cout<<" * ";

display(f2);

cout<<" = ";

display(product);

cout<<"\n\n";

display(f1);

cout<<" + ";

display(f2);

cout<<" = ";

display(add);

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] 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...

  • Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...

    Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT