Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

undefined 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

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedundefined

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. Purposes:Ensure that you can write a small class that meets a precise specification. Make sure you understand how to write a class that is separated into a header file and an implementation file. Give you experience in using a test program to track down bugs in a class's implementation. Before Starting:Read all of Chapter 2.Know how to compile and run C++ programs on your system. Files that you must write: 1. stats.h: The header file for the new statistician class. Actually, you don't have to write much of this file. Just start with the version provide 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. 2. 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 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 statisticians 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. 1 // Provided by: (your name here) Go Back Email Address: (your email address here) 3 // FILE: stats.h 4 // CLASS PROVIDED: statistician 5 (a class to keep track of statistics on a sequence of real numbers) 6 This class is part of the namespace main_savitch_2c. 7 // 8 // CONSTRUCTOR for the statistician class: statistician(); 10 // Postcondition: The object has been initialized, and is ready to accept 11 // a sequence of numbers. Various statistics will be calculated about the 12 // sequence. 13 // 14 // PUBLIC MODIFICATION member functions for the statistician class: 15 // void next(double r) 16 // The number r has been given to the statistician as the next number in 17 its sequence of numbers. 18 // void reset(); 19 Postcondition: The statistician has been cleared, as if no numbers had 20 // yet been given to it. 21 // 22 // PUBLIC CONSTANT member functions for the statistician class: 23 // int length() const 24 // Postcondition: The return value is the length of the sequence that has 25 // been given to the statistician (i.e., the number of times that the 26 // next(r) function has been activated). 27 // double sum( ) const 28 // Postcondition: The return value is the sum of all the numbers in the 29 // statistician's sequence. 30 double mean() const 31 Precondition: length() > 32 Postcondition: The return value is the arithmetic mean (i.e., the 33 // average of all the numbers in the statistician's sequence). 34 // double minimum( ) const 35 // Precondition: length() > 0 36 // Postcondition: The return value is the tinyest number in the 37 // statistician's sequence. 38 // double maximum( ) const 39 // Precondition: length() > 0 40 // Postcondition: The return value is the largest number in the 41 // statistician's sequence. 42 // 43 // NON-MEMBER functions for the statistician class: 44 statistician operator +(const statistician& si, const statistician& s2) 45 // Postcondition: The statistician that is returned contains all the 46 // numbers of the sequences of s1 and s2. 47 statistician operator *(double scale, const statistician& s) 48 // Postcondition: The statistician that is returned contains the same 49 // numbers that s does, but each number has been multiplied by the 50 // scale number. 51 bool operator ==(const statistician& si, const statistician& s2) 52 // Postcondition: The return value is true if s1 and s2 have the zero 53 // length. Also, if the length is greater than zero, then si and s2 must 54 // have the same length, the same mean, the same minimum, 55 // the same maximum, and the same sum. 56 // 57 // VALUE SEMANTICS for the statistician class: 58 // Assignments and the copy constructor may be used with statistician objects. 59 69 70 75 76 60 #ifndef STATS_H // Prevent duplicate definition 61 #define STATS_H 62 #include 63 -64 namespace main_savitch_2C 65 { class statistician 67 { 68 public: // CONSTRUCTOR statistician(); 71 // MODIFICATION MEMBER FUNCTIONS 72 void next(double r); 73 void reset(); 74 // CONSTANT MEMBER FUNCTIONS int length() const; double sum() const; 77 double mean() const; 78 double minimum const; double maximum( ) const; // FRIEND FUNCTIONS friend statistician operator + 82 (const statistician& si, const statistician& s2); friend statistician operator * (double scale, const statistician& s); private: int count; // How many numbers in the sequence 87 double total; // The sum of all the numbers in the sequence 88 double tinyest; // The smallest number in the sequence double largest; // The largest number in the sequence }; 91 92 // NON-MEMBER functions for the statistician class 93 bool operator ==(const statistician& si, const statistician& s2); 94 } 95 96 #endif 89 90 97

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions