Question
You will create, implement and test a small class called statistician, which is similar to some of the small classes in Chapter 2 of the
You will create, implement and test a small class called statistician, which is similar to some of the small classes in Chapter 2 of the text.
.
The new class is called statistician, using a header file, stats.h (attached),(most of which is written for you) and an implementation file, stats.cpp (which you need to create and implement.). 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 CISP430_A1. 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
Two constant member functions called minimum and maximum, which return the smallest and largest numbers that have been given to the statistician. (By the way, these two functions 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 tests to see whether two statisticians are "equal". The prototype is:
bool operator ==(const statistician& s, const statistician& t);
In order for two statisticians to be equal, they must have the same length (i.e., they have been given the same number of numbers). Also, if their length is greater than zero, they must also have the same mean, the same minimum, the same maximum, and the same sum. For example: Suppose that a statistician s has been given four numbers 1, 2, 3, 4. A second statistician t has been given four numbers 1, 1.5, 3.5, 4. Then the test (s==t) must return true since both s and t have equal values for all the member functions, as shown here:
s.length( ) and t.length( ) are both 4
s.mean( ) and t.mean( ) are both 2.5
s.sum( ) and t.sum( ) are both 10.0
s.minimum( ) and t.minimum are both 1
s.maximum( ) and t.maximum are both 4
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.
// FILE: statexam.cpp // This program calls five test functions to test the statisitician class. // Maximum number of points from this program is 200. #include#include #include #include // Provides memcpy function #include "stats.h" using namespace CISP430_A1; using namespace std; const int SCORE0=0,SCORE1=100, SCORE2=25; bool close(double a, double b) { const double EPSILON = 1e-5; return (fabs(a-b) < EPSILON); } int test1( ) { // Test program for basic statistician functions. // Returns 90 if everything goes okay; otherwise returns 0. statistician s, t; int i; double r = 0; if (s.length( ) || t.length( )) return 0; if (s.sum( ) || t.sum( )) return 0; for (i = 1; i <= 10000; i++) { s.next(i); r += i; }; if (t.length( ) || t.sum( )) return 0; if (s.length( ) != 10000) return 0; if (!close(s.sum( ), r)) return 0; if (!close(s.mean( ), r/10000)) return 0; // Reset and then retest everything s.reset( ); t.reset( ); r = 0; if (s.length( ) || t.length( )) return 0; if (s.sum( ) || t.sum( )) return 0; for (i = 1; i <= 10000; i++) { s.next(i); r += i; }; if (t.length( ) || t.sum( )) return 0; if (s.length( ) != 10000) return 0; if (!close(s.sum( ), r)) return 0; if (!close(s.mean( ), r/10000)) return 0; return SCORE1; } int test2( ) { // Test program for minimum/maximum statistician functions. // Returns 15 if everything goes okay; otherwise returns 0. statistician s, t, u; double r = 1000; char n[15] = "10000000000000"; if (s.length( ) || t.length( )) return 0; if (s.sum( ) || t.sum( )) return 0; memcpy(&r, n, sizeof(double)); r = 1/r; s.next(r); if ((s.minimum( ) != r) || (s.maximum( ) != r)) return 0; r *= -1; t.next(r); if ((t.minimum( ) != r) || (t.maximum( ) != r)) return 0; u.next(100); u.next(-1); u.next(101); u.next(3); if ((u.minimum( ) != -1) || (u.maximum( ) != 101)) return 0; return SCORE2; } int test3( ) { // Test program for + operator of the statistician // Returns 15 if everything goes okay; otherwise returns 0. statistician s, t, u, v; if (s.length( ) || t.length( )) return 0; if (s.sum( ) || t.sum( )) return 0; t.next(5); u.next(0); u.next(10); u.next(10); u.next(20); v = s + s; if (v.length( ) || v.sum( )) return 0; v = s + u; if (!(u == v)) return 0; v = t + s; if (!(t == v)) return 0; v = t + u; if (v.length( ) != 5) return 0; if (!close(v.sum( ), 45)) return 0; if (v.minimum( ) != 0) return 0; if (v.maximum( ) != 20) return 0; if (!close(v.mean( ), 45.0/5)) return 0; v = v + t; if (v.length( ) != 6) return 0; if (!close(v.sum( ), 50)) return 0; if (v.minimum( ) != 0) return 0; if (v.maximum( ) != 20) return 0; if (!close(v.mean( ), 50.0/6)) return 0; return SCORE2; } int test4( ) { // Test program for * operator of the statistician // Returns 15 if everything goes okay; otherwise returns 0. statistician s, t, u; if (s.length( ) || t.length( )) return 0; if (s.sum( ) || t.sum( )) return 0; u.next(0); u.next(10); u.next(10); u.next(20); s = 2*u; if (s.length( ) != 4) return 0; if (!close(s.sum( ), 80)) return 0; if (s.minimum( ) != 0) return 0; if (s.maximum( ) != 40) return 0; if (!close(s.mean( ), 80.0/4)) return 0; s = -2*u; if (s.length( ) != 4) return 0; if (!close(s.sum( ), -80)) return 0; if (s.minimum( ) != -40) return 0; if (s.maximum( ) != 0) return 0; if (!close(s.mean( ), -80.0/4)) return 0; s = 0*u; if (s.length( ) != 4) return 0; if (!close(s.sum( ), 0)) return 0; if (s.minimum( ) != 0) return 0; if (s.maximum( ) != 0) return 0; if (!close(s.mean( ), 0)) return 0; s = 10 * t; if (s.length( ) != 0) return 0; if (s.sum( ) != 0) return 0; return SCORE2; } int test5( ) { // Test program for == operator of the statistician. // Returns 15 if everything goes okay; otherwise returns 0. statistician s, t, u, v, w, x; if (s.length( ) || t.length( )) return 0; if (s.sum( ) || t.sum( )) return 0; t.next(10); u.next(0); u.next(10); u.next(10); u.next(20); v.next(5); v.next(0); v.next(20); v.next(15); w.next(0); x.next(0); x.next(0); if (!(s == s)) return 0; if (s == t) return 0; if (t == s) return 0; if (u == t) return 0; if (!(u == v)) return 0; if (w == x) return 0; return SCORE2; } int main( ) { int value = 0; int result; cerr << "Running statistician tests:" << endl; cerr << "TEST 1:" << endl; cerr << "Testing next, reset, length, sum, and mean (100 points). "; result = test1( ); value += result; if (result > 0) cerr << "Test 1 passed." << endl << endl; else cerr << "Test 1 failed." << endl << endl; cerr << " TEST 2:" << endl; cerr << "Testing minimum and maximum member functions (25 points). "; result = test2( ); value += result; if (result > 0) cerr << "Test 2 passed." << endl << endl; else cerr << "Test 2 failed." << endl << endl; cerr << " TEST 3:" << endl; cerr << "Testing the + operator (25 points). "; result = test3( ); value += result; if (result > 0) cerr << "Test 3 passed." << endl << endl; else cerr << "Test 3 failed." << endl << endl; cerr << " TEST 4:" << endl; cerr << "Testing the * operator (25 points). "; result = test4( ); value += result; if (result > 0) cerr << "Test 4 passed." << endl << endl; else cerr << "Test 4 failed." << endl << endl; cerr << " TEST 5:" << endl; cerr << "Testing the == operator (25 points). "; result = test5( ); value += result; if (result > 0) cerr << "Test 5 passed." << endl << endl; else cerr << "Test 5 failed." << endl << endl; cerr << "If you submit the statistician to me now, this part of the "; cerr << "grade will be " << value << " points out of 200. "; system("PAUSE"); return EXIT_SUCCESS; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started