CSCE 2014 - Laboratory Assignment 3

    Most of the classes we have seen so far are designed to store and manipulate a specific data type. For example the "Temp" class stores temperatures in Celsius or Fahrenheit, the "Music" class stores information about songs and performers, and the "Student" and "Course" classes store student records and course rosters.

    The other category of classes we have studied can be viewed as specialized containers with operations that could be used to store and manipulate a variety of data types. For example, the "SafeArray" class provides methods to read/write/access data in a float array, and the "Bag" class has methods to insert, search and remove strings from an array.

    If we decide we need a SafeArray of integers, we can edit the source files and replace "float" with "int" and recompile. As you can imagine, this process is tedious and error prone. In this lab we will explore two better ways to accomplish this task. This lab has the following steps:

  1. Compiling and Running the Numbers Class
  2. In this lab we will be working with the "Numbers" class. Start by downloading the file numbers.cpp from the source directory. If you look inside this file, you will see that it contains the class definition, the implementation of class methods, and a small main program that calls these methods to test their operation.

    Create a NetBeans "C++ Application" project called "Numbers" with an automatically generated "main.cpp" file. Copy the numbers.cpp file into the same directory as main.cpp, and then add numbers.cpp to the project, and remove main.cpp from the project. When you compile and run the project, you should be prompted for an input file.

    Download the file numbers.txt and save this in the same directory as numbers.cpp. If you look inside this file, you will see it has 100 two digit numbers (which are actually the first 200 digits of PI). Now when you run your program, enter "numbers.txt" as the name of the input file.

    Copy and paste your program output into the text box below:

  3. Extending the Numbers Class
  4. In probability theory and statistics, the variance and standard deviation are measures of how "dispersed" or "spread out" a set of numbers are. The standard formula for calculating variance is:

    where the mean is given by

    The standard deviation is simply the square root of the variance.

    If you look at the Numbers class definition you will see that it has methods for calculating the min, max, and mean values of the array of numbers. Your task in this section is to add two new methods called "findVariance" and "findStandardDeviation" that calculate the variance and standard deviation of the array of numbers using the formulas above.

    Start by adding two methods to the class definition, and two empty method implementations. Then add calls to these two methods to the main program and print out the variance and standard deviation. Finally add the body of the two methods and run the program again.

    Copy and paste your findVariance and findStandardDeviation methods below:

    Copy and paste your program output into the text box below:

  5. Generalize the Numbers Class
  6. As you now know, the Numbers class reads and manipulates an array of integers. In this section, we will use "typedef" to generalize the Numbers class so it can work with any numeric data type. Start by making a backup copy of numbers.cpp so you do not lose the original file.

    Add the line "typedef int DataType;" above the Numbers class definition and go through the class definition and replace all of the "int" with "DataType" where the variable or return type is associated with the Data itself. Do NOT change index variables or counters. You should end up with seven occurrences of "DataType" in the class definition.

    Next go through all of the method implementations and the main program and replace all of the "int" to "DataType" using the same approach. Remember, we only want to change variables associated with the Data array. When you compile and run your program it should produce exactly the same results as before.

    Finally, edit the typedef line to be "typedef float DataType;" and compile and run the program. You should see that the mean, variance and standard deviation are all output as float values. Congratulations, you just generalized your first C++ class to support multiple data types.

    Copy and paste your typedef line and class definition below:

    Copy and paste your program output into the text box below:

  7. Create a Template Numbers Class
  8. The typedef approach is great because we only have to change one line to change a class from using "int" data to using "float" data. The problem is that we can not easily have a main program that works with BOTH kinds of classes at the same time. This is why templates were added to C++. By using a template class, we can supply the data type we want to use when we declare the objects in the main program.

    Start by making a backup copy of the typedef version of "numbers.cpp" so you do not lose this work. Next, edit the class definition to remove the typedef line and add the following "template <class DataType>" just above the class definition. Do NOT put an ";" after this line or you will get a million syntax errors.

    Next, copy/paste this line just before each of your method definitions to tell the compiler that these methods are templated. Then change the "Numbers::" to be "Numbers<DataType>" in each of the method headers. When you are finished, your methods should all look like this:

    template <class DataType>
    int Numbers<DataType>::getCount()
       return Count;

    Next, replace the original main program with the following code and compile and run your program. If you look at this code, you will see that we used "Numbers <int> num;" to declare a "num" object that reads and processes the data using "int" data. We used "Numbers <float> num2;" to declare the "num2" object which is used to read and process the data using "float" data.

    int main()
       string filename;
       cout << "Enter filename:";
       cin >> filename;
       // Process int numbers
       Numbers <int> num;
       cout << "min = " << num.findMin() << endl;
       cout << "max = " << num.findMax() << endl;
       cout << "mean = " << num.findMean() << endl;
       // Process float numbers
       Numbers <float> num2;
       cout << "min = " << num2.findMin() << endl;
       cout << "max = " << num2.findMax() << endl;
       cout << "mean = " << num2.findMean() << endl;

    Finally, edit your program and see what happens when you use the following data types (short, long, double, char, string) when creating Numbers objects. Which work fine? Which have problems?

    Describe your experiments with your Numbers class below:

    Copy and paste your final "numbers.cpp" file below:

  9. Submit Work
  10. This lab assignment will be submitted electronically to the TAs once you fill in the fields below and click on the "submit" button. You do NOT need to print a copy of this document to hand in.

    Your UAID number:
    Your website PASSWORD: