Question

In: Computer Science

Introduction: Real work is in end. I will give you thumps up for your work. Lab2:...

Introduction: Real work is in end. I will give you thumps up for your work.

Lab2:

Create a class called Rational for performing arithmetic with fractions. Use the integer 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 initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 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. add - Adds to Rational numbers. The result should be stored in a reduced form.
  2. subtract - Subtract two Rational numbers. Store the result in reduced form.
  3. multiply - Multiplies two Rational numbers. Store the result in reduced form.
  4. divide - Divides two Rational numbers. The result should be stored in a reduced form.
  5. toRationalString - Return a string representation of a Rational number in the form a/b, where a is the numerator and b is the denominator.
  6. toDouble - Returns the Rational number as double

Output sample:

1/3 + 7/8 = 29/24
29/24 = 1.20833

1/3 - 7/8 = -13/24
-13/24 = -0.541667

1/3 + 7/8 = 7/24
7/24 = 0.291667

1/3 + 7/8 = 8/21
8/21 = 0.380952

Lab 2's Ans:


#include <iostream>
#include <sstream> // for ostringstream class
using namespace std;
class Rational {
public:
Rational(int = 0, int = 1); // default constructor

Rational add(const Rational) const;
Rational subtract(const Rational) const;
Rational multiply(const Rational) const;
Rational divide(const Rational) const;
std::string toRationalString() const; // return as string format
double toDouble() const; // return rational as double
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce(); // utility function
};

//Define the two-argument constructors
//Your code
Rational::Rational(int a, int b)
{
this->numerator = a;
this->denominator = b;
}
//I provided the code for the function add

Rational Rational::add(const Rational a) const
{
Rational t(a.numerator * denominator + a.denominator * numerator,
a.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
}
//Define the function subtract
//Your code
Rational Rational::subtract(const Rational a) const
{
Rational t(a.numerator * denominator - a.denominator * numerator,
a.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
}

//Define the function multiply
//Your code
Rational Rational::multiply(const Rational a) const
{
Rational t(numerator * a.numerator, denominator * a.denominator);
t.reduce(); // store the fraction in reduced form
return t;
}

//Define the function divide
//Your code
Rational Rational::divide(const Rational a) const
{
Rational t(numerator * a.denominator, denominator * a.numerator);
t.reduce(); // store the fraction in reduced form
return t;
}

string Rational::toRationalString() const
{
if (denominator == 0) { // validates denominator
return "\nDIVIDE BY ZERO ERROR!!!";
}
else if (numerator == 0) { // validates numerator
return "0";
}
else {
ostringstream output;
output << numerator << '/' << denominator;
return output.str();
}
}

double Rational::toDouble() const
{
return static_cast<double>(numerator) / denominator;
}
//The function reduce is provided for you. This function reduces the fraction.

void Rational::reduce()
{
int largest = numerator > denominator ? numerator : denominator;

int gcd = 0; // greatest common divisor

for (int loop = 2; loop <= largest; ++loop) {
if (numerator % loop == 0 && denominator % loop == 0) {
gcd = loop;
}
}

if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}

int main()
{
Rational c(1, 3);
Rational d(7, 8);

Rational x = c.add(d); // adds object c and d; sets the value to x
cout << c.toRationalString() << " + " << d.toRationalString() << " = " << x.toRationalString() << '\n';
cout << x.toRationalString() << " = " << x.toDouble() << "\n\n";

//Write code to subtract Rational object d from c ( c-d) and display the output similar to the format for adding to Rational //objects above
x = c.subtract(d); // adds object c and d; sets the value to x
cout << c.toRationalString() << " - " << d.toRationalString() << " = " << x.toRationalString() << '\n';
cout << x.toRationalString() << " = " << x.toDouble() << "\n\n";

//Write code to multiply Rational objects c and d, and display the output similar to the format for adding to Rational //objects above
x = c.multiply(d); // adds object c and d; sets the value to x
cout << c.toRationalString() << " * " << d.toRationalString() << " = " << x.toRationalString() << '\n';
cout << x.toRationalString() << " = " << x.toDouble() << "\n\n";

//Write code to divide Rational object c by d (c/d) and display the output similar to the format for adding to Rational //objects above
//Write code to display the result of dividing c by d in the double format as shown in the output sample above
x = c.divide(d); // adds object c and d; sets the value to x
cout << c.toRationalString() << " / " << d.toRationalString() << " = " << x.toRationalString() << '\n';
cout << x.toRationalString() << " = " << x.toDouble() << "\n\n";
}

______________________________________________________________________________

Now you job is to do rest:

Redo the lab 2 (RationalNumber class) to provide the following capabilities:

  1. Create a constructor that prevents a 0 denominator in a fraction, reduce or simplifies fractions that are not in reduced form, and avoids negative denominators.
  2. overload cin (>>) and cout (<<).
  3. Overload the addition, subtraction, multiplication, and division operators for this class.
  4. Overload the relational operators and == for this class

Attached is a partial lab7.cpp program. You need to write code for the following operator overloaded functions:

  • OVERLOAD - operator
  • OVERLOAD * operator
  • OVERLOAD <= operator
  • OVERLOAD = operator
  • OVERLOAD cin (>>) operator
  • FILL IN BLANK C++ STATEMENT FOR THE OVERLOAD cout (<<)

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <iostream>
#include <sstream> // for ostringstream class
using namespace std;
class Rational {
public:
Rational(int = 0, int = 1); // default constructor

Rational operator+(const Rational) const;
Rational operator-(const Rational) const;
Rational operator*(const Rational) const;
Rational operator/(const Rational) const;
/**
* Friend function declarations
* Output Rational number to an output stream, Example 3+4i
*/
friend std::ostream & operator << (std::ostream & dout,const Rational & );
/**
* read Rational number to an input stream
*/
friend std::istream & operator >> (std::istream & din, Rational & );

double toDouble() const; // return rational as double
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduce(); // utility function
};

//Define the two-argument constructors
//Your code
Rational::Rational(int a, int b)
{
this->numerator = a;
this->denominator = b;
}
//I provided the code for the function add

Rational Rational::operator+(const Rational a) const
{
Rational t(a.numerator * denominator + a.denominator * numerator,
a.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
}
//Define the function subtract
//Your code
Rational Rational::operator-(const Rational a) const
{
Rational t(a.numerator * denominator - a.denominator * numerator,
a.denominator * denominator);
t.reduce(); // store the fraction in reduced form
return t;
}

//Define the function multiply
//Your code
Rational Rational::operator*(const Rational a) const
{
Rational t(numerator * a.numerator, denominator * a.denominator);
t.reduce(); // store the fraction in reduced form
return t;
}

//Define the function divide
//Your code
Rational Rational::operator/(const Rational a) const
{
Rational t(numerator * a.denominator, denominator * a.numerator);
t.reduce(); // store the fraction in reduced form
return t;
}

// Function implementation which display Rational numbers by using the operator overloading '<<'
ostream& operator<<(ostream& dout, const Rational& c)
{
dout <<"("<< c.numerator << "/" << c.denominator<<")";
return dout;
}
// Function implementation which read Rational numbers by using the operator overloading '>>'
istream& operator>>(istream& din, Rational& c)
{
char ch;
din >> c.numerator;
din >> ch;
din >> c.denominator;
return din;
}


double Rational::toDouble() const
{
return static_cast<double>(numerator) / denominator;
}
//The function reduce is provided for you. This function reduces the fraction.

void Rational::reduce()
{
int largest = numerator > denominator ? numerator : denominator;

int gcd = 0; // greatest common divisor

for (int loop = 2; loop <= largest; ++loop) {
if (numerator % loop == 0 && denominator % loop == 0) {
gcd = loop;
}
}

if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}

int main()
{
Rational c,d;
cout<<"Rational Number#1:";
cin>>c;
cout<<"Rational Number#2:";
cin>>d;


Rational x = c+d; // adds object c and d; sets the value to x
cout << c<< " + " << d<< " = " << x<< '\n';
cout << x<< " = " << x.toDouble() << "\n\n";

//Write code to subtract Rational object d from c ( c-d) and display the output similar to the format for adding to Rational //objects above
x = c-d; // adds object c and d; sets the value to x
cout << c<< " - " << d<< " = " << x<< '\n';
cout << x<< " = " << x.toDouble() << "\n\n";

//Write code to multiply Rational objects c and d, and display the output similar to the format for adding to Rational //objects above
x = c*d; // adds object c and d; sets the value to x
cout << c<< " * " << d<< " = " << x<< '\n';
cout << x<< " = " << x.toDouble() << "\n\n";

//Write code to divide Rational object c by d (c/d) and display the output similar to the format for adding to Rational //objects above
//Write code to display the result of dividing c by d in the double format as shown in the output sample above
x = c/d; // adds object c and d; sets the value to x
cout << c << " / " << d<< " = " << x<< '\n';
cout << x<< " = " << x.toDouble() << "\n\n";
}

======================================

======================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

Please be clear, concise and up to the point in your answer. Give real world examples...
Please be clear, concise and up to the point in your answer. Give real world examples to support your arguments. West Coast Unlimited is a wholesaler that carries close to 20,000 products. The company has almost 3,000 suppliers and sells its products mostly to business and institutional customers. The company markets its products by relying mainly on sales promotion and advertising. Faced with increasing costs, the company is looking at various ways to reduce expenses. West Coast Unlimited's vice president...
Can you give examples of using functions in real life? By real life I understand not...
Can you give examples of using functions in real life? By real life I understand not only what we do in everyday living, but also science, economy, and similar.
Please use excel and show all work and formulas. (I will give your work a like...
Please use excel and show all work and formulas. (I will give your work a like if you do this) Size (1000s sq. ft) Selling Price ($1000s) 1.26 117.5 3.02 299.9 1.99 139.0 0.91 45.6 1.87 129.9 2.63 274.9 2.60 259.9 2.27 177.0 2.30 175.0 2.08 189.9 1.12 95.0 1.38 82.1 1.80 169.0 1.57 96.5 1.45 114.9 What are the p-values of the t test (for the slope estimate) and F test? What is the coefficient of determination? What is...
The company you work for will deposit $500 at the end of eachmonth into your...
The company you work for will deposit $500 at the end of each month into your retirement fund. Interest is compounded monthly. You plan to retire 12 years from now and estimate that you will need $3,000 per month out of the account for the next 10 years following your retirement. If the account pays 7.25% compounded monthly, how much (in addition to your company deposit)do you need to put into the account on monthly basis in order to meet...
1. When you list your references at the end of your work, you should: a) have...
1. When you list your references at the end of your work, you should: a) have separate lists for journals, books, websites b) have one long list arranged alphabetically by author, and thereafter chronologically, starting with the earliest date c) have one long list for all your references arranged alphabetically by author 2. Is this a correct in-text citation? True or False? Thwaites (2007, p. 151) argued that ‘Psychoanalysis is…..’ . Look at this reference. Is this a complete book...
you are a supervisor of a medical office and your employee showed up for work in...
you are a supervisor of a medical office and your employee showed up for work in an inappropriate outfit how would you handle the situation
You are an audit senior in charge of your first audit and are wrapping up your work....
You are an audit senior in charge of your first audit and are wrapping up your work. The client's controller, a pretty fussy person, knows you're going to ask for a "representation letter" near the end of the audit which he thinks adds an unnecessary burden on him because he's already overworked. He says he won't comply with your request until you explain why an auditor needs a representation letter for every audit. 
The company you work for will deposit $400 at the end of each month into your...
The company you work for will deposit $400 at the end of each month into your retirement find. Interest is compounded monthly. You plan to retire in 15 years from now and estimate that you will need $2,000 per month out of the account for the next 10 years following your retirement. If the account pays 7.25% compounded monthly, how much do you need to put into the account in addition to your company deposit in order to meet your...
PLEASE TYPE all work, and I'll give you a thumbs up! Mean of stock price =...
PLEASE TYPE all work, and I'll give you a thumbs up! Mean of stock price = 1117.64 STDEV (Population) = 67.61 If a person bought 1 share of Google stock within the last year, what is the probability that the stock on that day closed within $50 of the mean for that year (round to two places)? (Hint: this means the probability of being between 50 below and 50 above the mean). If a person bought 1 share of Google...
Imagine you work at an ad agency and your team is charged with coming up with...
Imagine you work at an ad agency and your team is charged with coming up with the name for Beautybee's latest perfume. You have been with the company for 6 months. The branding team has been brainstorminig for the last 2 hours, filling up pages and pages of the flipchart with innovative, imaginative names. Feeling daunted by how loudly, quickly, and assertively branding team members are shouting out suggestions, you decide to sit this one out, even though you have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT