basic-java-programming

Topic

String Tokenizer

Static Methods

Save your time - order a paper!

Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines

Order Paper Now

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 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:

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

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.

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

Submit

Submit the following:

  • Source code
  • The output of the test run. Do not submit the input of the test run.

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

Accessing Static Variables/Methods From Within Instance Methods

You can access static variables and methods directly from within an instance method.

Accessing Instance Variable/Methods From Within Static Methods

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

  • 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

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

}