String Tokenizer
Static Methods
Static Variables
Primitive Arrays
Enhance the last assignment by providing the following additional features:
Class Statistics
In the class Statistics, create the following static methods (in addition to the instance methods already provided).
In the class Statistics, create the following static variable (in addition to the instance variables already provided).
Increment the static variable count from within the Statistics constructor.
Display the value of the static variable count in the main method towards the end.
Class TestStatistics
In the main method of the class TestStatistics, do the following:
Static Method Headers
Static methods cannot access instance variables unless they create an object. Therefore, in the header of each static method, provide array data as an input parameter. Header definitions for static methods are listed below.
public static double [ ] computeSortedData (double [] data)
public static double computeMin (double [] data)
public static double computeMax (double [] data)
public static double computeMean (double [] data)
public static double computeMedian (double [] data)
Static Variable
Create a public static variable count as below. Use this variable for keeping track of the total number of Statistics objects created during program execution.
public static int count = 0;
Input All Data as A Single String
Input all data values as a single String. Use a StringTokenizer object to tokenize the data. For this purpose, follow the steps listed below:
Accesing A Static Variable
Since static variable count is declared public, its value can be accessed directly from main method or anywhere else as below.
int count = Statistics.count;
Coding Static Methods
In coding a static method of class Statistics follow the steps below: (See sample code below).
Sample Code for a Static Method
//sample code for static method computeMin
public static double computeMin (double [ ] data)
{
//Create a Statistics object. Pass it the array data during construction.
Statistics st = new Statistics ( data );
//Ask Statistics object to find min of the array passed during creation.
double min = st.findMin ( );
//return min to the caller
return min;
}
Input Test Run
Enter All Data < separated by commas/spaces>:
7.2, 7.6, 5.1, 4.2, 2.8, 0.9, 0.8, 0.0, 0.4, 1.6, 3.2, 6.4
Enter the Number of Decimal Places that the Result is Required:
3
Output Test Run
Original Data:
7.2 7.6 5.1 4.2 2.8 0.9 0.8 0.0 0.4 1.6 3.2 6.4
Results Using Instance Methods:
Sorted Data:
0.0 0.4 0.8 0.9 1.6 2.8 3.2 4.2 5.1 6.4 7.2 7.6
Computed Values:
Min Value: 0.000
Max Value: 7.600
Mean: 3.350
Median: 3.000
Results Using Static Methods:
Sorted Data:
0.0 0.4 0.8 0.9 1.6 2.8 3.2 4.2 5.1 6.4 7.2 7.6
Computed Values:
Min Value: 0.000
Max Value: 7.600
Mean: 3.350
Median: 3.000
The Total Number of Statistics objects created during execution:
(display the value here).