CS
205 - Programming for the Sciences
Spring 2008 - In-class
Exercise for 04/03/08
Today's exercise is to modify the Statistics program with actions to load a data file of numbers into an ArrayList object, list the data in the ArrayList, and compute the median of the numbers in the ArrayList. For this exercise, you are given a C# project with a Statistics program that has all of the previous functionality given: computing maximum, average, and standard deviation.
Use a Web browser to go to the course webpage http://csserver.evansville.edu/~hwang/s08-courses/cs205.html. Under today's date, save the file StatisticsFilesInClass.zip. Extract the solution folder. Double-click into the folder, then double-click on StatisticsFiles.sln (the Visual Studio solution file). This will launch Visual Studio with the solution loaded.
Changes from the previous Statistics program:
We do not know the number of values in a data file ahead of time, so we cannot use an array to hold the values, since arrays have fixed size. Instead we will use an ArrayList object (Chapter 10, pp. 176-178). An ArrayList is like an array with the following differences:
ArrayList is defined in the System.Collections library, which must be added to the using list..
The property that contains the number of elements in an ArrayList is named Count (rather than Length).
The elements of an ArrayList are generic Objects, so an element needs to be converted to its actual type before being used.
An item may be added to the end of an ArrayList using the Add method.. Elements may also be removed. In particular, all of the elements may be removed using the Clear method.
Since the elements ArrayList may be empty, a method CheckForData is called by all handlers to test for this. If there is no data, this method informs the user with a message box and returns false. Otherwise it returns true.
Since computing standard deviation requires computing the average, a method ComputeAverage computes and returns the average of the values in the elements ArrayList. It assumes that the elements ArrayList is not empty. This method is called by the Average button handler and the Std. Dev. button handler.
Assignment
(10 points) We will do the following in class. Details will be given during class.
Add a handler for the Load Data button. This handler should use an OpenFileDialog object to get a file name from the user, then create a StreamReader object with the file name and clear the elements ArrayList. A data file will consist of an unknown number of values each on its own line. The handler should read each value until it reaches the end of the file, and add each value to end of the elements ArrayList.
Add a handler for the List Data button that displays the values in the elements ArrayList using the foreach statement (examples on p.186, 315).
Add a handler for the Median button that computes and displays the median of the elements ArrayList. This handler should sort the ArrayList into ascending order. If there are an odd number of elements, the median is the middle element. If there are an even number of elements, the median is the average of the two middle elements.
04/02/08