Topic

String Tokenizer
Static Methods
Static Variables
Primitive Arrays

Description

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).

  • A public static method for computing sorted data.
  • A public static method for computing min value.
  • A public static method for computing max value.
  • A public static method for computing mean value.
  • A public static method for computing median value.

In the class Statistics, create the following static variable (in addition to the instance variables already provided).

  • A static variable count that keeps track of the total number of Statistics objects created during the program run.

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:

  • Input all user data values as a single String using a single input dialog box.
  • Use a StringTokenizer object to separate individual values in the input String.
  • Store input values in the elements of the array data.
  • Create a Statistics object and pass it the array data.
  • Call instance methods of the Statistics object to compute required statistics.
  • Call static methods to compute required statistics. (Pass array data to each static method).
  • Display the required statistics two times: once after calling instance methods and a second time after calling static methods. (See the output of a test run later below).
  • Display the total number of Statistics objects created during the program run. (Do this by displaying the value of the static variable count at the end).

Further Details

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:

  • Input all data as a single String.
  • Create a StringTokenizer object and supply it the input String.
  • Use methods of StringTokenizer object to separate data values.

Implementation

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).

  • Create a Statistics object from within the static method.
  • Call the appropriate instance method of the Statistics object to obtain the required statistic.
  • Save the result obtained from the instance method.
  • Return the result to the caller.

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;
}

Testing:

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).
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.