Question: The Assignment: You will implement and test a small class called statistician, which is similar to some of the small classes in Chapter 2 of
The Assignment:
You will implement and test a small class called statistician, which is similar to some of the small classes in Chapter 2 of the text.
Files that you must write:
stats.h: The header file for the new statistician class. Actually, you don't have to write much of this file. Just start with our version and add your name and other information at the top. If some of your member functions are implemented as inline functions, then you may put those implementations in this file too.
stats.cxx: The implementation file for the new statistician class. You will write all of this file, which will have the implementations of all the statistician's member functions.
The Statistician Class Discussion of the Assignment
As indicated above, you will implement a new class called statistician, using a header file (most of which is written for you) and an implementation file (which you will write by yourself). The statistician is a class that is designed to keep track of simple statistics about a sequence of real numbers. There are two member functions that you should understand at an informal level before you proceed any further. The prototypes for these two functions are shown here as part of the statistician class declaration:
class statistician { public: ... void next(double r); double mean( ) const; ... }; The member function "next" is used to give a sequence of numbers to the statistician one at a time. The member function "mean" is a constant member function that returns the arithmetic mean (i.e., the average) of all the numbers that have been given to the statistician.
Example: Suppose that you want a statistician to compute the mean of the sequence 1.1, 2.8, -0.9. Then you could write these statements:
// Declares a statistician object called s statistician s; // Give the three numbers 1.1, 2.8 and -0.9 to the statistician s.next(1.1); s.next(2.8); s.next(-0.9); // Call the mean function, and print the result followed by a carriage return cout << s.mean( ) << endl;
The output statement will print 1.0, since 1.0 is the mean of the three numbers 1.1, 2.8 and -0.9.
Once you understand the workings of the next and mean member functions, you can look at the complete specification of the statistician class, which is in the file stats.h. Notice that the statistician class in this file is part of a namespace called main_savitch_2C. You should use this namespace for your statistician. In this file you will find a precondition/postcondition contract for all the statistician's member functions, including:
A default constructor, which merely does any initialization needed for the statistician to start its work
The next and mean functions, described above
A constant member function called length, which returns the count of how many numbers have been given to the statistician
A constant member function called recent, which returns the most recent number that was given the the statistician via the next function.
Two constant member functions called minimum and maximum, which return the smallest and largest numbers that have been given to the statistician.
Two more constant member functions called abs_minimum and abs_maximum, which return the numbers with the smallest and largest absolute values. (By the way, these two functions, the minimum and maximum functions, the recent function and the mean function all have a precondition that requires length( ) > 0. You cannot use these three member functions unless the statistician has been given at least one number!)
A constant member function called sum, which returns the sum of all the numbers that have been given to the statistician. This function does NOT have a precondition. It may be called even if the statistician has NO numbers (in which case it should return 0).
An overloaded + operator which has two statisticians as arguments, and returns a third statistician, as shown in this prototype:
statistician operator +(const statistician& s, const statistician& t);
An overloaded * operator which allows you to "multiply" a double number times a statistician. Here is the prototype:
statistician operator *(double scale, const statistician& s);
This is not a member function. The result of a multiplication such as 2*s is a new statistician that looks as if it had been given all the numbers of s, multiplied by the constant 2. Examples: Suppose that s is a statistician that has been given 1, 2, 3, and u is another statistician. Then the assignment statement u=2*s will result in u behaving as if it had been given the numbers 2, 4, 6. As another example, the assignment statement u=-3*s will result in u behaving as if it had been given the numbers -3, -6, -9. Notice that neither + nor == are member functions. (See Section 2.5 in the class notes). The result of s+t is a new statistician that looks as if it had been given all the numbers of the sequence for s, followed by all the numbers of the sequence for t. For example: Suppose that we have three statisticians s, t, and u. The statistician s has been given the numbers 1, 2, 3; the statistician t has been given the numbers 4, 5. Then the assignment statement u=s+t will result in u behaving as if it had been given the five numbers 1, 2, 3, 4, 5.
"stats.h File includes this:"
// FILE: stats.h // CLASS PROVIDED: statistician // (a class to keep track of statistics on a sequence of real numbers) // This class is part of the namespace main_savitch_2C. // // CONSTRUCTOR for the statistician class: // statistician( ); // Postcondition: The object has been initialized, and is ready to accept // a sequence of numbers. Various statistics will be calculated about the // sequence. // // PUBLIC MODIFICATION member functions for the statistician class: // void next(double r) // The number r has been given to the statistician as the next number in // its sequence of numbers. // void reset( ); // Postcondition: The statistician has been cleared, as if no numbers had // yet been given to it. // // PUBLIC CONSTANT member functions for the statistician class: // int length( ) const // Postcondition: The return value is the length of the sequence that has // been given to the statistician (i.e., the number of times that the // next(r) function has been activated). // double sum( ) const // Postcondition: The return value is the sum of all the numbers in the // statistician's sequence. // double mean( ) const // Precondition: length( ) > 0 // Postcondition: The return value is the arithmetic mean (i.e., the // average of all the numbers in the statistician's sequence). // double minimum( ) const // Precondition: length( ) > 0 // Postcondition: The return value is the tinyest number in the // statistician's sequence. // double maximum( ) const // Precondition: length( ) > 0 // Postcondition: The return value is the largest number in the // statistician's sequence. // // NON-MEMBER functions for the statistician class: // statistician operator +(const statistician& s1, const statistician& s2) // Postcondition: The statistician that is returned contains all the // numbers of the sequences of s1 and s2. // statistician operator *(double scale, const statistician& s) // Postcondition: The statistician that is returned contains the same // numbers that s does, but each number has been multiplied by the // scale number. // bool operator ==(const statistician& s1, const statistician& s2) // Postcondition: The return value is true if s1 and s2 have the zero // length. Also, if the length is greater than zero, then s1 and s2 must // have the same length, the same mean, the same minimum, // the same maximum, and the same sum. // // VALUE SEMANTICS for the statistician class: // Assignments and the copy constructor may be used with statistician objects. #ifndef STATS_H // Prevent duplicate definition #define STATS_H #include namespace main_savitch_2C { class statistician { public: // CONSTRUCTOR statistician( ); // MODIFICATION MEMBER FUNCTIONS void next(double r); void reset( ); // CONSTANT MEMBER FUNCTIONS int length( ) const; double sum( ) const; double mean( ) const; double minimum( ) const; double maximum( ) const; // FRIEND FUNCTIONS friend statistician operator + (const statistician& s1, const statistician& s2); friend statistician operator * (double scale, const statistician& s); private: int count; // How many numbers in the sequence double total; // The sum of all the numbers in the sequence double tinyest; // The smallest number in the sequence double largest; // The largest number in the sequence }; // NON-MEMBER functions for the statistician class bool operator ==(const statistician& s1, const statistician& s2); } #endif Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
