Question

Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different class and functions from problem 1. The main function calls different functions as instructed in the problem description. Submit the CPP file during submission Problem 1. Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and the denominator (one for each data). Also include a member function that returns the value of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms (simplest form). For example, instead of outputting 20/60 the function should output 1/3. This will require finding the greatest common divisor for the numerator and denominator and then dividing both by that number. Embed your class in a test program where you create 2 fractions with user data and display their two values.

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

#include <iostream>
using namespace std;

class Fraction
{
private:
int numerator;
int denominator;


public:
Fraction() //default constructor
{
numerator = 0;
denominator = 1;
}
Fraction(int num,int den) //argument constructor
{
if(denominator != 0)
{
numerator=num;
denominator=den;
}
simplify();
}

  
     friend std::ostream& operator<<( std::ostream &output, const Fraction &F )
     {
       output << F.numerator << " / " << F.denominator;
         return output;
     }
    
      double toDouble()
      {
       return (double)numerator/denominator;
      }
     
     
      int gcd(int x, int y) // compute greatest common divisor
      {
       int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
      }
    
       void simplify() {
int factor;
factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}

int getNumerator()
{
return numerator;
}

int getDenominator()
{
return denominator;

}

};
int main()
{
Fraction r1(20,60);
Fraction r2(30,40);

cout<<"Fraction f1 : "<<r1.getNumerator()<<"/"<<r1.getDenominator();
cout<<"\nFraction f1 in decimal : ";
cout<<r1.toDouble();

cout<<"\nFraction f2 : "<<r2.getNumerator()<<"/"<<r2.getDenominator();
cout<<"\nFraction f2 in decimal : ";
cout<<r2.toDouble();

return 0;
}

Output:

Fraction f1 : 1/3
Fraction f1 in decimal : 0.333333
Fraction f2 : 3/4
Fraction f2 in decimal : 0.75

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different...
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
  • Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...

    c++ format Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Science Linux systems. If your code does not compile on CS machines you will get 0 for the assignment. Organize your files into folders by lab assignment. For this assignment, create a folder called Lab9. A. Read Chapter 18 B. Create a new class called RationalNumber to represent fractions. The class should have the following capabilities: 1) It should...

  • Design a class named Fraction. This class is used to represent a ratio of two integers,...

    Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead...

  • Construct a class named Fractions containing two integer data members named num and denom, used to...

    Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • 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...

  • 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...

  • 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)...

  • 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...

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