PLEASE MAKE SURE TO USE CORRECT FUNCTION NAMES SO THE
client.cpp CAN COMPILE WITH OTHER TWO FRACTION FUNCTIONS! MAKE
SURE NOT TO CHANGE client.cpp! THANK YOU!
here is the client.cpp code copied:
Project 1B (50 points]: Designing and implementing a C++ class data type: Fraction Class Note: This is a multifile file C++ project - Documentation is required on this assignment. For this assignment, you will be building on the Fraction class whose objects represent fractions that you began last week. All the requirements from that class are still in force. Your class specification should name the class Fraction (not fraction) and should still have exactly two data members (named appropriately such as int numerator, int denominator), one to represent the numerator of the Fraction object being represented, and one to represent the denominator of the Fraction object being represented. You'll be making major changes to the Fraction class from Project 1A and should provide the following class member functions: · Two constructors, a default constructor which assigns the value to the Fraction object, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction object, and the second parameter will represent the initial denominator of the Fraction object. Since Fractions cannot have denominators of O, the default constructor should assign 0 to the numerator and 1 to the denominator. Also, the parameterized constructor should check to make sure that the second parameter is not a O by using the statement "assert(denomParameter !=0);". To use the assert() function you'll also need to #include <cassert>. (Note, I don't expect the variable to be named "denomParameter," that's just an example.) assert() is not the best way to handle this, but it will have to do until we get to study exception handling time permitting in CS10B or in CS10C. (4 pts for specification and implementation) · Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as four value returning functions that return a fraction object. The member functions should be named AddedTo, Subtract, MultipliedBy, and DividedBy. Note the returned fraction object must be in reduced form. (20 pts for specification and implementation) • A boolean operation with a member function named isGreaterThan that compares two fraction objects to determine whether one fraction object is greater than the other. (4 pts for specification and implementation) · An input operation with a member functions named getFraction that prompts the user for two integer values to be used to define a new fraction object. One value is to be used for the numerator and the other for the denominator of the fraction object. The fraction object will be displayed later on the screen in the form numerator/denominator. The getFraction member function algorithm must include input validation for both numerator and denominator inputs with a check for non-integer and/or zero (O) value input. Provide an appropriate error message and an opportunity to input a valid value for the numerator and denominator. Check the program output below for a sampling of input validation tests on user inputs for two new fraction objects. (5 pts for specification and implementation)
• An output operation with a member function named showFraction that displays the value of a fraction object on the screen in the form numerator/denominator. (3 pts for specification and implementation) • Add a private member function named "simplify()" to your class and call it from the appropriate member functions. (For most of you there will be 5 or 6 places where you need to call it. This, however, depends on how you write the class, so don't assume you are wrong if you don't have exactly 5 or 6.) The best way to do this is to make the function a void function with no parameters that reduce the calling object. Make sure that your class will reduce ANY non-negative Fraction, not just the Fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In other words, you should ensure that all Fraction objects are reduced before the end of any member function. You must create your own algorithm for reducing Fractions. Don't look up an already existing algorithm for reducing Fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don't use Euclid's algorithm. Don't worry about being efficient. It's fine to have your function check every possible factor, even if it would be more efficient to just check prime numbers. Just create something of your own that works correctly on ANY Fraction. (5 pts for specification and implementation) Note: If you are having trouble keeping up with the class, I suggest you skip this part and take the 5 point deduction. • Add the const keyword to your class wherever appropriate (see lectures/lesson notes in ancillary resources). Your class may still work correctly even if you don't do this correctly, so this will require extra care!! Hint: You must also include the constant keywords for all member functions that are "observer type" of functions such as AddedTo, Subtract, MultipliedBy, DividedBy, isGreaterThan and showFraction - see Class setup samples.pdf 2 (3 pts for specification and implementation) Add documentation to your assignment. Be sure to include appropriate documentation Classes : Documentation - Pre/post conditions in class specification and a class invariant in class implementation) and separate your member functions definitions in the implementation file with at least 1 inch of whitespace. Hint: in this assignment, it turns out that none of the functions have preconditions. (6 pts for specification and implementation) Since this project requires multiple files you will need to first create a new project in your IDE/compiler, write the source code for your class specification file (fraction.h), the implementation file (fraction.cpp) and then add the client code below (client.cpp) to the project. To be clear your Fraction class project will be in 3 separate files that are compiled and run in a single project. For an example with explanations of such C++ class-related multifile projects review the Software Engineering Tip: Separating Class, Specification, Implementation, and Client Code in Chapter 13.5 in your Gaddis textbook.
I suggest that you design your Fraction class incrementally. For example, you should first implement only the class constructors and the output function, comment out portions of the client code that is not needed and then test what you have so far. Once this code has been thoroughly debugged, you should then 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 below and comment out portions of code that relate to member functions that you have not yet implemented. Here is the client program (save the contents below or here is a link to the file (client.cpp) /*Client.cpp is set-up as a Fraction class implementation and test driver program - DO NOT CHANGE SOURCE CODE All necessary class objects are declared and defined here to test various class related operations. HINT: see comments for specific class related function calls/ #include <iostream> #include "Fraction.h" using namespace std; int main() { Fraction f1(9,8); //calling a parameterized class constructor Fraction f2(2,3); Fraction result; // calling a default class constructor const Fraction f3 (12, 8); const Fraction 44(202, 303); Fraction f5, f6; cout<<"C++ CLASS MULTI-FILE PROJECT"<<endl; cout<<"Client.cpp - testing a Fraction class implementation\n"; cout<<"- --\n\n"; cout << "The result object starts off at "; result.showFraction(); //calling a void "observer" function cout << endl; cout<<"\nArithmetic operations with Fraction objects stored in the results class object\n"; --\n\n"; cout<<"- cout << "The sum of "; f1.showFraction(); cout << " and "; f2.showFraction(); cout << " is "; result = f1. AddedTo(f2); //a class binary operation - a value-returning "observer" function result.showFraction(); cout << endl; cout << "The difference of "; f1.showFraction(); cout << " and "; f2.showFraction(); cout << " is "; result = f1. Subtract(f2); //a class binary operation - a value-returning "observer" function result.showFraction(); cout << endl;
cout << "The product of "; f1.showFraction(); cout << " and "; f2.showFraction(); cout << " is "; result = f1. MultipliedBy (F2); //a class binary operation - a value-returning "observer" function result.showFraction(); cout << endl; result = f3.DividedBy(74); //a class binary operation - a value-returning "observer" function cout << "The quotient of "; f3.showFraction(); cout << " and "; f4.showFraction(); cout << " is "; result.showFraction(); cout << endl; cout<<"\nInput and Output operations for two new class objects\n"; cout<<"- --\n\n"; f5.getFraction(); //a class input operation - a transformer or setter function cout << "\nNew fraction object: "; f5.showFraction(); //a class output operation - an observer or getter function cout<<endl<<endl; fb.getFraction(); cout << "\nNew fraction object: "; fb.showFraction(); cout<<endl<<endl; cout<<"\nA Boolean operation comparing two class objects\n"; cout<<" --\n\n"; if (f5.isGreaterThan(56)){ //a class relational expression - boolean operation/function f5.showFraction(); cout <<" is greater than "; f6.showFraction(); cout<<endl; } else { f5.showFraction(); cout <<" is less than "; fb.showFraction(); cout<<endl; } cout<<"\n--- --\n"; cout<<"\nFraction class implementation test now successfully concluded\n"; 1/ system ("PAUSE"); return 0; } You may not change the client program in any way. Changing the client program will result in a grade of 0 on the project.
Successfully compiling/running in a project, the client program (client.cpp) provided above along with the fraction class specification (fraction.h) and implementation file (fraction.cpp) you have written should produce the following program output shown below: (items in bold represents user inputs) C++ CLASS MULTI-FILE PROJECT Client.cpp - testing a Fraction class implementation The result object starts off at 0/1 Arithmetic operations with Fraction objects stored in the results class object The sum of 9/8 and 2/3 is 43/24 The difference of 9/8 and 2/3 is 11/24 The product of 9/8 and 2/3 is 3/4 The quotient of 3/2 and 2/3 is 9/4 Input and Output operations for two new class objects Enter values for a fraction object (whole numbers only) Numerator: 12srjc Input error.... Enter values for a fraction object (whole numbers only) Numerator: 12 Denominator: 0 Input error, denominator cannot be a Denominator: 13 New fraction object: 12/13 Enter values for a fraction object (whole numbers only) Numerator: 5.456 Input error..... Enter values for a fraction object (whole numbers only) Numerator: 5 Denominator: 6 New fraction object: 5/6 A Boolean operation comparing two class objects 12/13 is greater than 5/6 Fraction class implementation test now successfully concluded Process returned o (0x0) Press any key to continue. execution time : 68.769 s After successfully completing this multifile C++ class project (ie., your program output should match the above), use the file submission area and upload just two source code files (1) the "Fraction class" specification file (fraction.h), followed by source code for your "Fraction class" implementation file (fraction.cpp). No need to submit the client code or the output produced when you ran your multifile C++ class project using the client program above. DO NOT use global variables and inline class member functions.
PLEASE MAKE SURE TO USE CORRECT FUNCTION NAMES SO THE client.cpp CAN COMPILE WITH OTHER TWO FRACTION FUNCTIONS! MAKE SUR
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am