Question

In: Computer Science

Note: This is a single file C++ project - No documentation is required on this assignment....

Note: This is a single file C++ project - No documentation is required on this assignment.

Write a Fraction class whose objects will represent Fractions.

Note: You should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program.

You must provide the following member functions:

  1. A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly.
  2. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning functions that return a Fraction object. They should be named addedTo, subtract, multipliedBy, and dividedBy. In these functions you will need to declare a local "Fraction" variable, assign to it the result of the mathematical operation, and then return it.
  3. A boolean operation named isEqualTo that compares two Fraction objects for equality. Since you aren't reducing your Fractions, you'll need to do this by cross-multiplying. A little review: if numerator1 * denominator2 equals denominator1 * numerator2, then the Fractions are equal.
  4. An output operation named print that displays the value of a Fraction object on the screen in the form numerator/denominator.

Your class should have exactly two data members, one to represent the numerator of the Fraction being represented, and one to represent the denominator of the Fraction being represented.

Here's a hint for how you will set up your arithmetic operation functions: You need two Fractions. One is the parameter, one is the calling object. The function multiplies the calling object times the parameter and returns the result. In some ways it is similar to the comesBefore() function from the lesson. That function also needs two Fractions, and one is the calling object and one is the parameter.

When adding or subtracting Fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator.

I am providing a client program for you below. You should copy and paste this and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results.

I strongly suggest that you design your class incrementally. For example, you should first implement only the set function and the output function, and then test what you have so far. Once this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program and comment out code that relates to member functions that you have not yet implemented.

As you can see from the sample output given below, you are not required to reduce Fractions or change improper Fractions into mixed numbers for printing. Just print it as an improper Fraction. You are also not required to deal with negative numbers, either in the numerator or the denominator.

Here is the client program.

#include <iostream>
using namespace std;

int main()
{
    Fraction f1;
    Fraction f2;
    Fraction result;

    f1.set(9, 8);
    f2.set(2, 3);
    
    cout<<"\nArithmetic operations with fraction objects stored in the results class object\n"; 
    cout<<"------------------------------------------------------------------------------\n\n";

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.multipliedBy(f2);
    result.print();
    cout << endl;

    cout << "The quotient of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.dividedBy(f2);
    result.print();
    cout << endl;

    cout << "The sum of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.addedTo(f2);
    result.print();
    cout << endl;

    cout << "The difference of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.subtract(f2);
    result.print();
    cout << endl;

    if (f1.isEqualTo(f2)){
        cout << "The two Fractions are equal." << endl;
    } else {
        cout << "The two Fractions are not equal." << endl;
    }
    
    cout<<"\n---------------------------------------------------------\n"; 
    cout<<"\nFraction class implementation test now successfully concluded\n"; 
    // system ("PAUSE"); 
    return 0;
}

This client should produce the output shown here:

C++ CLASS SINGLE-FILE PROJECT Client.cpp - testing a Fraction class implementation 
----------------------------------------------------

Arithmetic operations with Fraction objects stored in the result class object 
------------------------------------------------------------------------------ 
The product of 9/8 and 2/3 is 18/24 
The quotient of 9/8 and 2/3 is 27/16 
The sum of 9/8 and 2/3 is 43/24 
The difference of 9/8 and 2/3 is 11/24 
The two Fractions are not equal. 
--------------------------------------------------------- 
Fraction class implementation test now successfully concluded 

Process returned 0 (0x0) execution time : 10.546 s Press any key to continue.

You may not change the client program in any way. Changing the client program will result in a grade of 0 on the project.

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;

class Fraction
{
private:
   int numerator;
   int denominator;
public:
   void set(int, int);
   Fraction multipliedBy(const Fraction &);
   Fraction dividedBy(const Fraction &);
   Fraction addedTo(const Fraction &);
   Fraction subtract(const Fraction &);
   bool isEqualTo(const Fraction &);
   void print();
};

void Fraction::set(int n, int d)
{
   numerator = n;
   denominator = d;
}
Fraction Fraction::multipliedBy(const Fraction & other)
{
   int n, d;
   n = numerator * other.numerator;
   d = denominator * other.denominator;
   Fraction r;
   r.set(n, d);
   return r;
}
Fraction Fraction::dividedBy(const Fraction &other)
{
   int n, d;
   n = numerator * other.denominator;
   d = denominator * other.numerator;
   Fraction r;
   r.set(n, d);
   return r;
}
Fraction Fraction::addedTo(const Fraction &other)
{
   int n, d;
   n = numerator * other.denominator+denominator*other.numerator;
   d = denominator * other.denominator;
   Fraction r;
   r.set(n, d);
   return r;
}
Fraction Fraction::subtract(const Fraction &other)
{
   int n, d;
   n = numerator * other.denominator - denominator * other.numerator;
   d = denominator * other.denominator;
   Fraction r;
   r.set(n, d);
   return r;
}
bool Fraction::isEqualTo(const Fraction &other)
{
   return numerator * other.denominator == other.numerator*denominator;
}
void Fraction::print()
{
   cout << numerator << "/" << denominator;
}

int main()
{
   Fraction f1;
   Fraction f2;
   Fraction result;

   f1.set(9, 8);
   f2.set(2, 3);

   cout << "\nArithmetic operations with fraction objects stored in the results class object\n";
   cout << "------------------------------------------------------------------------------\n\n";

   cout << "The product of ";
   f1.print();
   cout << " and ";
   f2.print();
   cout << " is ";
   result = f1.multipliedBy(f2);
   result.print();
   cout << endl;

   cout << "The quotient of ";
   f1.print();
   cout << " and ";
   f2.print();
   cout << " is ";
   result = f1.dividedBy(f2);
   result.print();
   cout << endl;

   cout << "The sum of ";
   f1.print();
   cout << " and ";
   f2.print();
   cout << " is ";
   result = f1.addedTo(f2);
   result.print();
   cout << endl;

   cout << "The difference of ";
   f1.print();
   cout << " and ";
   f2.print();
   cout << " is ";
   result = f1.subtract(f2);
   result.print();
   cout << endl;

   if (f1.isEqualTo(f2)) {
       cout << "The two Fractions are equal." << endl;
   }
   else {
       cout << "The two Fractions are not equal." << endl;
   }

   cout << "\n---------------------------------------------------------\n";
   cout << "\nFraction class implementation test now successfully concluded\n";
   system ("PAUSE");
   return 0;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce...
IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce you to basic operations on a linked list. Specifics Design a program that generates a linked list of randomly generated Integer objects. Present a menu at program start that gives the user the following options (most of these options will have corresponding methods in a Linked List class): 1. Create a new list. The size will be specified by the user, make sure a...
C++ good documentation as well as explanations would be great. In this HW assignment we will...
C++ good documentation as well as explanations would be great. In this HW assignment we will simulate a car wash and calculate the arrival time, departure time and wait time for each car that came in for a car wash. We will use the following assumptions about our car wash: Each car takes 3 minutes from start of the car wash to the end. Only one car can be in the car wash at a time. This mean that if...
NOTE: Projects will not be graded if: --------------------------- Your complete project is not in a SINGLE...
NOTE: Projects will not be graded if: --------------------------- Your complete project is not in a SINGLE xxxxxp2.java file, where xxxxx is at most the 1st the first 5 characters of your last name and p2 is the project number. Using package or have compile error. It does not read input from any data file. It’s not uploaded in canvas. Those who work jointly, only one should submit and group names must be written at the bottom of the output. ---------------------------...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
You are required to come up with a single header file (IntList.h) that declares and implements...
You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement more than 1...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
how to write Man Page documentation ? i have a group project written in C and...
how to write Man Page documentation ? i have a group project written in C and my job is to write man page documentstion. can anyone please explain it and how to write and please provide example. thank you
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT