Question

In: Computer Science

In C++ Create a class called Rational (separate the files as shown in the chapter) for...

In C++

Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use 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 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 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: add - Adds two Rational numbers. The result should be stored in reduced form. subtract - Subtracts two Rational numbers. Store the result in reduced form. multiply - Multiples two Rational numbers. Store the result in reduced form. divide - Divides two Rational numbers. The result should be stored in reduced form. (toRationalString) - Returns a string representation of a Rational number in the form a/b, where a is the numerator and b is the denominator. display - Display the Rational number a/b. toDouble - Returns the Rational number as a double. (make sure the driver tests out all the functions)

  1. add - Adds two Rational numbers. The result should be stored in reduced form.
  2. subtract - Subtracts two Rational numbers. Store the result in reduced form.
  3. multiply - Multiples two Rational numbers. Store the result in reduced form.
  4. divide - Divides two Rational numbers. The result should be stored in reduced form.
  5. (toRationalString) - Returns a string representation of a Rational number in the form a/b, where a is the numerator and b is the denominator.
  6. display - Display the Rational number a/b.
  7. toDouble - Returns the Rational number as a double.

(make sure the driver tests out all the functions)

Solutions

Expert Solution

Dear Student, i have spent lot of time on it, so if you got something from it, then please give an Upvote. Thank You

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

private:
 int numerator; // integer numerator
 int denominator; // integer denominator
 void reduction(); // utility function
}; // end class Rational
#endif
                                          Rational.cpp
// Member-function definitions for class Rational.
#include <iostream>
using std::cout;
#include "Rational.h" // include definition of class Rational
Rational::Rational( int n, int d )
{
 numerator = n; // sets numerator
 denominator = d; // sets denominator
 reduction(); // store the fraction in reduced form
} // end Rational constructor
Rational Rational::addition( const Rational &a )
{
 Rational t; // creates Rational object
 t.numerator = a.numerator * denominator;
 t.numerator += a.denominator * numerator;
 t.denominator = a.denominator * denominator;
 t.reduction(); // store the fraction in reduced form
 return t;
} // end function addition
Rational Rational::subtraction( const Rational &s )
{
 Rational t; // creates Rational object
 t.numerator = s.denominator * numerator;
 t.numerator -= denominator * s.numerator;
 t.denominator = s.denominator * denominator;
 t.reduction(); // store the fraction in reduced form
 return t;
} // end function subtraction
Rational Rational::multiplication( const Rational &m )
{
 Rational t; // creates Rational object
 t.numerator = m.numerator * numerator;
 t.denominator = m.denominator * denominator;
 t.reduction(); // store the fraction in reduced form
 return t;
} // end function multiplication
Rational Rational::division( const Rational &v )
{
 Rational t; // creates Rational object
 t.numerator = v.denominator * numerator;
 t.denominator = denominator * v.numerator;
 t.reduction(); // store the fraction in reduced form
 return t;
} // end function division
void Rational::printRational ()
{
 if ( denominator == 0 ) // validates denominator
 cout << "\nDIVIDE BY ZERO ERROR!!!" << '\n';
 else if ( numerator == 0 ) // validates numerator
 cout << 0;
 else
 cout << numerator << '/' << denominator;
} // end function printRational
void Rational::printRationalAsDouble()
{
 cout << static_cast< double >( numerator ) / denominator;
} // end function printRationalAsDouble
void Rational::reduction()
{
 int largest;
 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;
 } // end if
} // end function reduction
                                          Solution.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Rational.h" // include definition of class Rational
int main()
{
 Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects
 c.printRational(); // prints rational object c
 cout << " + ";
 d.printRational(); // prints rational object d
 x = c.addition( d ); // adds object c and d; sets the value to x
 cout << " = ";
 x.printRational(); // prints rational object x
 cout << '\n';
 x.printRational(); // prints rational object x
 cout << " = ";
 x.printRationalAsDouble(); // prints rational object x as double
 cout << "\n\n";
 c.printRational(); // prints rational object c
 cout << " - ";
 d.printRational(); // prints rational object d
 x = c.subtraction( d ); // subtracts object c and d

 cout << " = ";
 x.printRational(); // prints rational object x
 cout << '\n';
 x.printRational(); // prints rational object x
 cout << " = ";
 x.printRationalAsDouble(); // prints rational object x as double
 cout << "\n\n";
 c.printRational(); // prints rational object c
 cout << " x ";
 d.printRational(); // prints rational object d
 x = c.multiplication( d ); // multiplies object c and d

 cout << " = ";
 x.printRational(); // prints rational object x
 cout << '\n';
 x.printRational(); // prints rational object x
 cout << " = ";
 x.printRationalAsDouble(); // prints rational object x as double
 cout << "\n\n";
 c.printRational(); // prints rational object c
 cout << " / ";
 d.printRational(); // prints rational object d
 x = c.division( d ); // divides object c and d

 cout << " = ";
 x.printRational(); // prints rational object x
 cout << '\n';
 x.printRational(); // prints rational object x
 cout << " = ";
 x.printRationalAsDouble(); // prints rational object x as double
 cout << endl;
 return 0;
} // end main

Related Solutions

9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use 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 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 form. For...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic 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 when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class representing Polynomials #include <iostream> #include <cmath> using namespace std; class Poly { private: int degree; int *coefficients; public: Poly(); Poly(int coeff, int degree); Poly(int coeff); Poly(const Poly& p); ~Poly(); void resize(int new_degree); void setCoefficient(int exp, int coeff); int getCoefficient(int exp); void display(); }; // default constructor to create a polynomial with constant 0 Poly::Poly() { // set degree to 0 degree = 0; //...
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
C++ Create an ArrayBag template class from scratch. This will require you to create two files:...
C++ Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation. The ArrayBag class must contain the following protected members: static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers...
Task Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return a...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational...
Task CPP Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). The class, along with the required operator overloads, should be written in the files mixed.h and mixed.cpp. Details and Requirements Finish Lab 2 by creating a Mixed class definition with constructor functions and some methods. The Mixed class should have public member functions Evaluate(), ToFraction(), and Simplify(). The Evaluate() function should return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT