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 count variable from within the Statistics constructor.
Display the value of the static count variable at the end of the program.
Class TestStatistics
In the main method of the class TestStatistics, do the following:
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)
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:
Testing:
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
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).
Submit
Submit the following:
Discussion
Static versus Instance variables and methods
A java class can contain any combination of instance variables, instance methods, static variables, and static methods.
Defining static and instance variables/methods
A static variable is declared in the same way that an instance variable is except that the word static is added in the declaration.
Similarly, a static method is written the same way that an instance method is written except that the word static is added in its header.
Using static and instance variables/methods
Instance variables and methods make up the blue print (template) of the class. Static variables and methods are not considered a part of the blue print.
Instance variables and methods come to existence when an object of the class is created. During object creation, only the blue print (template) part of the class is used. Therefore, an object contains only the instance variables and methods. Each object contains its own copy of the instance variables and associated methods. When a method of an object is called, it operates on its own copy of the instance variables. You can access an instance variable or an instance method by pre-pending its name with the object name and a dot as shown below:
objectName.instanceVariable
objectName.instanceMethod ( )
One can assume that static variables and methods exist from the start of the program and remain in existence for the life of the program. There is only one copy of a static variable or a static method for the class. You can access a static variable or a static method by pre-pending its name with the class name and a dot as shown below:
className.staticVariable
className.staticMethod ( )
You can access static variables and methods directly from within an instance method.
However, from within a static method you can access instance variables and methods only after the corresponding object is created. You can either create the object from within the static method or pass the static method the reference of the created object.
Implementation
Accesing A Static Variable
Since static variable count is declared public, its value can be accessed directly 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
//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;
}
WhatsApp us