Starting from:
$35

$29

Programming Assignment 1 – Implement and Test the Statistician Class Solution

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 (Unix or Windows).
 
How to Turn In: Files to be submitted: source code (*.cpp, *.cxx, *.h)  and the text file (a *.txt file or *.pdf file) showing the running output result of your implementation with the non-interactive test program statsexam.cxx (see below). Please don’t send your executable files. Please submit your files to ds.zhu.ccny@gmail.com, with the subject line “CSc212 Assignment 1“.
Integrity Policy: Please don’t forget to write your name and ID (last four digits) on top of each of your files (see below for files you need to turn in). Then under your name, please write this statement: “The work in this assignment is my own. Any outside sources have been properly cited.” Without writing this statement, you will not be able to get any score.
Files that you must write or generate:

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.
A text file (a *.txt file or *.pdf file) showing the running output result of your implementation with the non-interactive test program statsexam.cxx (see below).
Other files that you may find helpful:    You shall compile one of the following files with your stats.cxx and link them to generate your executable:

stattest.cxx: A simple interactive test program for you to test your implementations.
statexam.cxx: A non-interactive test program that will be used to grade the correctness of your statistician class.
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
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:
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:
An overloaded * operator which allows you to “multiply” a double number times a statistician. Here is the prototype:
Hints and Frequently Asked Questions
The Private Member Variables

Carefully read the class definition in stats.h. Notice how the private member variables are being used to keep track of information about the statistician’s sequence of numbers. The statistician does NOT keep track of all the numbers in the sequence. There is no need to do so, and trying to do so can get you into trouble. Instead, it keeps track of only the information that is relevant to its member functions: How many numbers have been seen? What is the sum of those numbers? If you have seen at least one number, then what are the smallest and largest numbers that you’ve seen so far? These four items should be your only private member variables.Be careful about how you set the private member variable that keeps track of the smallest number. My suggestion is that you do NOT have the constructor initialize this member variables (because when the constructor does its work, there have not yet been any numbers, so there is no smallest number). But part of the work of the “next” function is to correctly maintain the private member variables. This means that the first time that the next function is called, it should set the private member variable that keeps track of smallest values. Later, if next is called again with a smaller number, then the next function will change the member variable that is keeping track of the smallest value. (You’ll have a similar process for the member variable that’s keeping track of the largest value).

Check Boundary Values

Make sure that your + and * operators work correctly when the arguments are statisticians with no numbers.

Check Preconditions

Your implementations should use the assert function to check preconditions of all functions.

Input and Output

Your implementations must NOT produce any output to cout, nor expect any input from cin. All the interaction with the member functions occurs through their parameters.

More products