|
Indicator Methods
A Programming Pattern |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
if (sold >= 1) {
shows = 1;
} else {
shows = 0;
}
/**
* Calculate the number of shows from the number of tickets
* sold for a particular show time.
*
* @param sold The number of tickets sold for a particular show time
* @return 0 if sold is 0; 1 otherwise
*/
public static int shows(int sold) {
if (sold >= 1) {
return 1;
} else {
return 0;
}
}
/**
* Calculate a threshold indicator from a value and threshold.
*
* @param value The value of interest
* @param threshold The threshold value
* @return 0 if the value is less than the threshold; 1 otherwise
*/
public static int indicator(int value, int threshold) {
if (value >= threshold) {
return 1;
} else {
return 0;
}
}
shows = indicator(sold, 1);
baseFine = 10.00;
repeatOffenderPenalty = 35.00;
totalFine = baseFine + (indicator(priors, 1) * repeatOffenderPenalty);
/**
* Return 1 if the person's sex is female and 0 otherwise.
*
* @param sex The person's sex ('F' or 'M')
* @return 1 if the person is female, and 0 otherwise
*/
public static int indicator(char sex) {
if ((sex == 'F') || (sex == 'f')) {
return 1;
} else {
return 0;
}
}
b = 5.00 + 10.00 * m + 6.35 * h - 5.00 * a - 161.00 * indicator(s);
boolean
methods
if (age >= DRINKING_AGE) {
isLegal = true;
} else {
isLegal = false;
}
isLegal = (age >= DRINKING_AGE);