C++ Code: Goal: Write a Class to represent Fractions Write a Fraction class whose objects will represent fractions. For example, a fraction of 3/7 will be represented by a Fraction object whose numerator will be set to 3 and denominator will be set to 7. Implementation Note: For this checkpoint do not reduce fractions (i.e. 15/18 should NOT be reduced to 5/6), do not use "const" (since we haven't discussed in class yet) Attributes: Your class should have exactly two private data members, one to represent the numerator of the Fraction being represented, and one to represent the denominator of the Fraction being represented. (What should be the data type of these member variables?) Member functions: A default constructor that sets the fraction to 0 (what should be the numerator and denominator for such a fraction?). A parametrized constructor takes two integer arguments, a numerator and a denominator, and assigns the attributes accordingly. Arithmetic operations that add, subtract, multiply, and divide Fractions. They should be named addedTo, subtract, multipliedBy, and dividedBy. These return a Fraction object. 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. 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. An output operation named print that displays the value of a Fraction object on the screen in the form numerator/denominator. Hints When adding or subtracting Fractions, remember that you must first find the common denominator. The easy way to do this (and that you must follow for this checkpoint since we are not simplifying fractions) is to multiply the denominators together and use that product as the common denominator. 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 current object. All operations will occur on the numerator/denominator of the current object and the numerator/denominator of the parameter. The signature of the functions implementing arithmetic operations will be something like: Fraction arithmeticOperation(Fraction& otherFraction); Design your class incrementally. For example, you should first implement only the constructors and the print 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; OR use the provided client program (see next) and comment out code that relates to member functions that you have not yet implemented. I am providing a template Download templatefor you that you should download. It includes the client program (main.cpp) which currently is completely commented out. To open this template code in cLion, simply use the "New Project" option, select the folder templateA, and when a prompt shows up saying "Directory is Not Empty", choose the option "Create from Existing Sources". The output that should be produced when the provided client program is run with your correctly implemented class is given below so that you can check your results. Subsequently, you will test your code by submitting it on zyBooks which will have a different client code. Sample Output 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 (f1 and f2) are not equal. The two Fractions (f3 and f4) are equal. As you can see from the sample output given above, 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.
C++ Code:
Goal: Write a Class to represent Fractions
Write a Fraction class whose objects will represent fractions. For example, a fraction of 3/7 will be represented by a Fraction object whose numerator will be set to 3 and denominator will be set to 7.
Implementation
Note: For this checkpoint
- do not reduce fractions (i.e. 15/18 should NOT be reduced to 5/6),
- do not use "const" (since we haven't discussed in class yet)
Attributes:
- Your class should have exactly two private data members, one to represent the numerator of the Fraction being represented, and one to represent the denominator of the Fraction being represented. (What should be the data type of these member variables?)
Member functions:
- A default constructor that sets the fraction to 0 (what should be the numerator and denominator for such a fraction?).
- A parametrized constructor takes two integer arguments, a numerator and a denominator, and assigns the attributes accordingly.
- Arithmetic operations that add, subtract, multiply, and divide Fractions. They should be named addedTo, subtract, multipliedBy, and dividedBy. These return a Fraction object. 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.
- 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.
- An output operation named print that displays the value of a Fraction object on the screen in the form numerator/denominator.
Hints
- When adding or subtracting Fractions, remember that you must first find the common denominator. The easy way to do this (and that you must follow for this checkpoint since we are not simplifying fractions) is to multiply the denominators together and use that product as the common denominator.
- 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 current object. All operations will occur on the numerator/denominator of the current object and the numerator/denominator of the parameter. The signature of the functions implementing arithmetic operations will be something like: Fraction arithmeticOperation(Fraction& otherFraction);
- Design your class incrementally. For example, you should first implement only the constructors and the print 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; OR use the provided client program (see next) and comment out code that relates to member functions that you have not yet implemented.
- I am providing a template Download templatefor you that you should download. It includes the client program (main.cpp) which currently is completely commented out. To open this template code in cLion, simply use the "New Project" option, select the folder templateA, and when a prompt shows up saying "Directory is Not Empty", choose the option "Create from Existing Sources".
- The output that should be produced when the provided client program is run with your correctly implemented class is given below so that you can check your results. Subsequently, you will test your code by submitting it on zyBooks which will have a different client code.
Sample Output
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 (f1 and f2) are not equal.
The two Fractions (f3 and f4) are equal.
As you can see from the sample output given above, 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.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images