Question

(Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

(Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks.

  1. Adding two Rational numbers. The result should be stored in reduced form.

  2. Subtracting two Rational numbers. The result should be stored in reduced form.

  3. Multiplying two Rational numbers. The result should be stored in reduced form.

  4. Dividing two Rational numbers. The results should be stored in reduced form.

  5. Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.

  6. Printing Rational numbers in floating-point format.

TO BE DONE IN C++ LANGUAGE

Note: separate interface from implementation.(you should create three files in this project

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

//do comment if any problem arises or modification needed

Rational.h:

#pragma once
#include<iostream>
using namespace std;
class Rational
{
private:
   int numerator, denominator;
public:
   Rational();
   Rational(int, int);
   void reduce();
   int find_gcd(int, int);
   void add(Rational&);
   void subtract(Rational&);
   void multiply(Rational&);
   void devide(Rational&);
   void print_Floating_point();
   void print();
};

Rational.cpp:

#include "Rational.h"

Rational::Rational()
{
   this->numerator = 0;
   this->denominator = 0;
}
Rational::Rational(int numerator, int denominator)
{
   this->numerator = numerator;
   this->denominator = denominator;
   this->reduce();
}
void Rational:: reduce()
{
   int gcd = find_gcd(numerator, denominator);
   numerator /= gcd;
   denominator /= gcd;
}
int Rational::find_gcd(int a, int b)
{
   if (b == 0) return a;
   a %= b;
   return find_gcd(b, a);
}
void Rational::add(Rational& temp)
{
   int numerator, denominator;
   denominator = this->denominator * temp.denominator;
   numerator = this->numerator * temp.denominator + temp.numerator * this->denominator;
   Rational addition(numerator,denominator);
   cout << "Addition: ";
   addition.print();
}
void Rational::subtract(Rational& temp)
{
   int numerator, denominator;
   denominator = this->denominator * temp.denominator;
   numerator = this->numerator * temp.denominator - temp.numerator * this->denominator;
   Rational subtraction(numerator, denominator);
   cout << "Subtraction: ";
   subtraction.print();
}
void Rational::multiply(Rational& temp)
{
   int numerator, denominator;
   denominator = this->denominator * temp.denominator;
   numerator = this->numerator * temp.numerator;
   Rational mult(numerator, denominator);
   cout << "Multiplication: ";
   mult.print();
}
void Rational::devide(Rational& temp)
{
   int numerator, denominator;
   denominator = this->denominator * temp.numerator;
   numerator = this->numerator * temp.denominator;
   Rational dev(numerator, denominator);
   cout << "Division: ";
   dev.print();
}
void Rational::print_Floating_point()
{
   cout << float(this->numerator) / this->denominator;
}
void Rational::print()
{
   cout << this->numerator << "/" << this->denominator << endl;
}

main.cpp:

#include"Rational.h"
int main()
{
   Rational first(3, 6);
   cout << "First number is: ";
   first.print();
   Rational second(5, 25);
   cout << "Second number is: ";
   second.print();
   first.add(second);
   first.subtract(second);
   first.multiply(second);
   first.devide(second);
}

Output:

Add a comment
Know the answer?
Add Answer to:
(Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...
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
  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 2...

  • PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)*...

    PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)***** Also please dont use BigInteger thanks it's really appreciated for all the help PLEASE MAKE IT SO ITS USER INPUT Create a class called Rational for prforming arithmetie with fractions. Write an app to test your class Use integer variables to represent the private instance variables of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • In mathematics, a rational number is any number that can be expressed as the quotient or...

    In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, p and q, with the denominator q not equal to zero. Since q may be equal to 1, every integer is a rational number. Define a class that can represent for a rational number. Use the class in a C++ program that can perform all of the following operations with any two valid rational numbers entered at the keyboard...

  • Can somebody help me with this assignment. I will highly appreciate. Also, please display the output...

    Can somebody help me with this assignment. I will highly appreciate. Also, please display the output as well. I need to use JAVA to to write the program. This is the sample output provided by my professor. HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • Create a procedure driven program for performing arithmetic with fractions. Using a switch statement in main...

    Create a procedure driven program for performing arithmetic with fractions. Using a switch statement in main to perform the various actions. Similar to this; Menu a) Load/Reload first fraction b) Load/Reload second fraction c) Add two fractions and display answer d) Subtract two fractions and display answer e) Multiply two fractions and display answer f) Divide two fractions and display answer g) Exit Provide functions that allow for The reloading of the fractions. The addition of two rational numbers. The...

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