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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

  1. do not reduce fractions (i.e. 15/18 should NOT be reduced to 5/6),
  2. 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:

  1. A default constructor that sets the fraction to 0 (what should be the numerator and denominator for such a fraction?).
  2. A parametrized constructor takes two integer arguments, a numerator and a denominator, and assigns the attributes accordingly.
  3. Arithmetic operations that add, subtract, multiply, and divide Fractions. They should be named addedTosubtractmultipliedBy, 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.
  4. 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.
  5. 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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education