
Purpose:
A Categorizer object determines which of a collection
of intervals a
particular value belongs to. In other words, a Categorizer
object can be used
to determine the "category" of a value.
Categorizer objects are immutable.
Categorizer objects can be used in a variety of
different applications. For example, when used as part of a system
for calculating the Air Quality Index (AQI) for carbon monoxide
(CO), a Categorizer object could be used to
determine that a value of 8.4
is "Moderate" with an AQI of 89.
A Categorizer object uses an array of
Category objects to do the actual work (one
Category object for each interval).
Continuing with the AQI example, a Categorizer for CO
would use an array of six Category objects for the
intervals \([0.0,4.5), [4.5,9.5), [9.5,12.5), [12.5,15.5), [15.5, 30.5),
[30.5,\infty)\) and the descriptions "Good", "Moderate",
"Unhealthy for Sensitive Groups", "Unhealthy", "Very Unhealthy",
and "Hazardous". Given a value of 8.4 for CO, it would identify
(using the includesValue() method in the
Category class)
the Category that contains this value (i.e., the
Category for the interval \([4.5,9.5)\)),
and use that Category object to
determine that the AQI is 89 (using its indexOf() method)
and the description is "Moderate" (using its
getDescription() method).
name - A String containing
the name of this Categorizer (e.g., "CO")categories - A Category[] containing
the Category objects that actually do the work.
It may contain other private attributes as well.
/**
* Explicit Value Constructor.
*
* @param name The name of this Categorizer (e.g., "CO")
* @param descriptions The descriptions of the categories
* @param indexes The indexes that define the intervals
* @param values The values that define the corresponding intervals
*/
public Categorizer(String name,
String[] descriptions,
int[] indexes,
double[] values)
{
}
At a minimum, this method must construct the array of
Category objects that the Categorizer
uses.
/**
* Calculate the index for a particular value.
*
* In other words, determine which interval contains the value and
* calculate the corresponding index (using linear
* interpolation). If the value is not found, this method returns
* the largest index
*
* @param value The value of interest
* @return The corresponding index
*/
public int indexOf(double value)
{
}
/**
* Get the description associated with a particular index (or "N/A"
* if the index is not in any of the intervals).
*
* @param index The index of interest
* @return The description of the Category that contains the index
*/
public String getDescription(int index)
{
}
public String getName()
Copyright 2013