Question

Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two...

Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two integers with division indicated. 

Requirement:
- two member variables: (int) numerator and (int) denominator. 
- two constructors: one that takes in both numerator and denominator to construct a rational number, and one that takes in only numerator and initialize the denominator as 1. 
- accessor/modifier
- member functions: add(), sub(), mul(), div(), and less(). Usage: to add rational num b and rational num a, one could call a.add(b); to compare rational num a and rational num b, one could call a.less(b). add(), sub(), mul() and div() should return a rational number.  
- member functon: factor(). This function should be a private function that should be called each time numerator/denominator is set/modified. The purpose of this function is to take the greatest common factor out of numerator/denominator. For example, rational number 2/4 should be stored as 1/2, rational number 3/9 should be stored as 1/3. 
- fully test all public functions inside main(). 

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

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class RationalNumber
{
public:
  
RationalNumber(int n);

// Initializes the rational number to wholeNumber/1
RationalNumber(int m, int n);

bool less(RationalNumber r2);

//Declaring functions
void add(RationalNumber rn);
void sub(RationalNumber rn);
void mul(RationalNumber rn);
void div(RationalNumber rn);
int getNumerator();
int getDenominator();
void setNumerator(int numer);
void setDenominator(int denom);

private:
// Declaring variables
int numerator; // the numerator of the number
int denominator; // the denominator of the number
int factor(int a, int b);
};

RationalNumber::RationalNumber(int n)
{
this->numerator=n;
this->denominator=1;
}

// Initializes the rational number to wholeNumber/1
RationalNumber::RationalNumber(int m, int n)
{
if(denominator<0)
{
this->numerator=-(m);
this->denominator=-(n);
}
else
{
this->numerator=(m);
this->denominator=(n);
}

}

bool RationalNumber::less(RationalNumber r2)
{
int a = numerator;
int b = denominator;
int c = r2.numerator;
int d = r2.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;

}
int RationalNumber::getNumerator()
{
return numerator;
}
int RationalNumber::getDenominator()
{
return denominator;
}
void RationalNumber::setNumerator(int numer)
{
this->numerator=numer;
}
void RationalNumber::setDenominator(int denom)
{
this->denominator=denom;
}

int RationalNumber::factor(int a, int b)
{
// % is modulus which is the remainder of a division
// base case
if ((a % b) == 0)
{
return b;
}
// recursive case
else
{
return factor(b, a % b);
}

}
//This method will add two rational numbers
void RationalNumber::add(RationalNumber rn)
{
int a,b,c,d;
a=getNumerator();
b=getDenominator();
c=rn.getNumerator();
d=rn.getDenominator();
int sumnumer=(a*d + b*c);
int sumdenom=(b*d);
  
int g=factor(sumnumer,sumdenom);
if(sumdenom!=1)
cout<<sumnumer/g<<"/"<<sumdenom/g;
else
cout<<sumnumer/g;

}
//This method will subtract two rational numbers
void RationalNumber::sub(RationalNumber rn)
{
int a,b,c,d;
a=getNumerator();
b=getDenominator();
c=rn.getNumerator();
d=rn.getDenominator();
int subnumer=(a*d - b*c);
int subdenom=(b*d);
int g=factor(subnumer,subdenom);
if(subdenom!=1)
cout<<subnumer/g<<"/"<<subdenom/g;
else
cout<<subnumer/g;
}
//This method will multiply two rational numbers
void RationalNumber::mul(RationalNumber rn)
{
int a,b,c,d;
a=getNumerator();
b=getDenominator();
c=rn.getNumerator();
d=rn.getDenominator();
int mulnumer=(a*c);
int muldenom=(b*d);
int g=factor(mulnumer,muldenom);
if(muldenom!=1)
cout<<mulnumer/g<<"/"<<muldenom/g;
else
cout<<mulnumer/g;
}
//This method will divide two rational numbers
void RationalNumber::div(RationalNumber rn)
{
int a,b,c,d;
a=getNumerator();
b=getDenominator();
c=rn.getNumerator();
d=rn.getDenominator();   
int divnumer=(a*d);
int divdenom=(c*b);
int g=factor(divnumer,divdenom);
if(divdenom!=1)
cout<<divnumer/g<<"/"<<divdenom/g;
else
cout<<divnumer/g;
}

int main()
{
//Creating the objects to RationalNumber class
RationalNumber rn1(1,6);
RationalNumber rn2(2);

//Getting the number entered by the user
cout<<"First Number:";
cout<<rn1.getNumerator()<<"/"<<rn1.getDenominator()<<endl;
cout<<"\nSecond Number:";
cout<<rn2.getNumerator()<<"/"<<rn2.getDenominator()<<endl;

/* calling the function and displaying
* the result after adding two rational num bers
*/
cout<<"\nAddition :";
rn1.add(rn2);
/* calling the function and displaying
* the result after subtracting two rational num bers
*/
cout<<"\nSubtraction :";
rn1.sub(rn2);
/* calling the function and displaying
* the result after multiplying two rational num bers
*/
cout<<"\nMultiplication :";
rn1.mul(rn2);
/* calling the function and displaying
* the result after dividing two rational num bers
*/
cout<<"\nDivision :";
rn1.div(rn2);

bool b=rn1.less(rn2);
if(b)
{
cout<<"\nRationalNumber#1 is Less than RationalNumber#2"<<endl;
}
else
{
cout<<"\nRationalNumber#1 is not Less than RationalNumber#2"<<endl;
}

return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two...
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
  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

  • Define a class for rational numbers. A rational number is a number that can be represented...

    Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 etc we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class rational Num...

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

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

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

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

  • code the inverse function 5. Here is the outline of a Rational number class, i.e., a...

    code the inverse function 5. Here is the outline of a Rational number class, i.e., a class that contains fractional numbers represented by an integer numerator and denominator). class Rational ( << operator operator public: Rational (int num, int denom) operator inverse function moiionu o loino private: int num, denom For partial credit for parts a-c, you can code the *, Integer class and << operators for the a. Code the operator; the result of multiplying two rationals is a...

  • Make a new class called Rational to represent rational numbers in C++ language. Use a long...

    Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...

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

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