Question

In: Computer Science

Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will...

Hello, I need this is C++. Thank you! (1B)

Write a Fraction class whose objects will represent Fractions.

Note: this is the first part of a multi-part assignment. For this week 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.

Solutions

Expert Solution

DO GIVE THUMPS UP...

HERE IS YOUR CODE FOR ABOVE PROBLEM.

I HAVE WRITTEN THIS CODE IN DEV C++ IDE.I HAVE ALSO ATTACHED THE SCREENSHOT OF CODE AND OUTPUT.

CODE :

#include <iostream>
#include<string.h>

using namespace std;

class Fraction{
   //numerator and dinomerator are the members of the fraction class
   public:
       int numerator;
       int dinomerator;
   public:
       //Set the numerator and dinomerator for the fraction
       void set(int num, int dino){
           numerator = num;
           dinomerator = dino;
       }
       //This will print the faction
       void print(){
           cout<<numerator<<"/"<<dinomerator<<endl;
       }
       //This is used to calculate the multiplication of two fractions.
       Fraction multipliedBy(Fraction obj){
           Fraction temp;
          
           temp.numerator = numerator*obj.numerator;
           temp.dinomerator = dinomerator*obj.dinomerator;
          
           return temp;
       }
       //This is used to calculate the division of two fractions.
       Fraction dividedBy(Fraction obj){
           Fraction temp;
          
           temp.numerator = numerator*obj.dinomerator;
           temp.dinomerator = dinomerator*obj.numerator;
          
           return temp;
       }
       //This is used to calculate the addition of two fractions.      
       Fraction addedTo(Fraction obj){
           Fraction temp;
          
           temp.numerator = numerator*obj.dinomerator + dinomerator*obj.numerator;
           temp.dinomerator = dinomerator*obj.dinomerator;
          
           return temp;
       }      
       //This is used to calculate the subtraction of two fractions.
       Fraction subtract(Fraction obj){
           Fraction temp;
          
           temp.numerator = numerator*obj.dinomerator - dinomerator*obj.numerator;
           temp.dinomerator = dinomerator*obj.dinomerator;
          
           return temp;
       }
       //This will check whether two fraction are equal or not.
       bool isEqualTo(Fraction obj){
          
           if(numerator == obj.numerator && dinomerator == obj.dinomerator){
               return true;
           }
           else
               return false;
       }
};

int main(int argc, char** argv) {
  
   //f1, f2 and result are instances of the Fraction class
   Fraction f1;
  
   Fraction f2;
  
   Fraction result;
   //set two fraction 9/8 and 2/3 for further operation
   f1.set(9, 8);
  
   f2.set(2, 3);
  
   cout<<"-----------------------------------OUTPUT-----------------------------------\n\n";
  
   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); // multiplied of two fraction
  
   result.print();
  
   cout << endl;
  
   cout << "The quotient of ";
  
   f1.print();
  
   cout << " and ";
  
   f2.print();
  
   cout << " is ";
  
   result = f1.dividedBy(f2); //division of two fraction
  
   result.print();
  
   cout << endl;
  
   cout << "The sum of ";
  
   f1.print();
  
   cout << " and ";
  
   f2.print();
  
   cout << " is ";
  
   result = f1.addedTo(f2); //addition of the two fractions
  
   result.print();
  
   cout << endl;
  
   cout << "The difference of ";
  
   f1.print();
  
   cout << " and ";
  
   f2.print();
  
   cout << " is ";
  
   result = f1.subtract(f2); //Subtraction of the two fraction
  
   result.print();
  
   cout << endl;
  
   if (f1.isEqualTo(f2)){ //Cheched the two fractions are equal or not.
  
   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 :

-----------------------------------OUTPUT-----------------------------------


Arithmetic operations with fraction objects stored in the results 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

SCREENSHOT :

IF YOU HAVE ANY QUESTIONS REGARDING CODE, DO COMMENT IN COMMNET SECTION.GIVE A THUMPS UP...


Related Solutions

I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Hello, Kindly I need short description of " Fixed Income Analysis?" . Thank you!!
Hello, Kindly I need short description of " Fixed Income Analysis?" . Thank you!!
Hello I need this written in C# with a few comments thanks Modify the Patient class...
Hello I need this written in C# with a few comments thanks Modify the Patient class to reference a demographic object that contains the patient phone number, email and next of kin. Create a demographic class to store this information. Make sure to create a constructor in the class for this information. Modify the Patient constructor to pass in this additional information at the time of instantiation. Modify the display method to display all the data HERE IS THE CODE...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE,...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE Thank in Advance. Must do; The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor You must overload the assignment operator You must declare the overloaded output operator as a friend rather than part of...
(YOU don't need to write the client class I already have it, just need this) You...
(YOU don't need to write the client class I already have it, just need this) You are provided with Main.java that is a client for this program. You will create THREE files-- GameCharacter.java, ShieldMaiden.java, and Dragon.java First: You must Create an interface called GameCharacter WITH JUST PROTOTYPES, NO IMPLEMENTATIONS A GameCharacter has a few functions associated with it 1) takeHit: decreases the character's health. It should return an int representing damage taken (hit points) 2) heal: increases the character's health....
You have leased a Class C network whose network ID is 204.188.89.0. You need to divide...
You have leased a Class C network whose network ID is 204.188.89.0. You need to divide it into three subnets to correspond to three different departments in your organization. For each of the four subnets enter the usable Host Address range: NOTE: enter the host address range using the following format 0.0.0.0-0.0.0.0 Notice the single dash between each IP address. Host addresses for the first subnet: Host addresses for the second subnet: Host addresses for the third subnet: Host addresses...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT