- Forward


Indicators
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Situation:
    • Calculations must be performed that vary based on one or more binary conditions
  • Terminology:
    • Indicator Variables
    • Dummy Variables
  • Common Notation:
    • \(\delta\) (often with a subscript identifying the indicator)
An Example
Back SMYC Forward
  • A Model of Birth Weight:
    • \(w = -2200 + 148.2 g - 238.6 \delta_s\)
  • Notation:
    • \(w\) denotes the weight of the baby in grams
    • \(g\) denotes the length of the gestation period in weeks
    • \(\delta_s\) is 1 if the mother smoked during the gestation period and 0 otherwise
The Pattern
Back SMYC Forward
  • Getting Started:
    • Define as many 0-1 variables (each of which can be an int or a double) as needed
  • The Process with One Indicator:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: pattern)
       
Examples
Back SMYC Forward
  • The Setting:
    • The birth weight example
  • The Code:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: birthweight)
       
Examples (cont.)
Back SMYC Forward
  • The Setting:
    • The fine associated with a first parking ticket is smaller than the fine associated with subsequent parking tickets (specifically, $10.00 for the first ticket and $45.00 for subsequent tickets)
  • The Code:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: parking)
       
Examples (cont.)
Back SMYC Forward
  • The Setting:
    • A rental car company that charges a base rate $19.95 per day
    • There is a surcharge of $5.00 per day if multiple people drive the car
    • There is a surcharge of $10.00 per day if any driver is under 25 years of age
  • The Code:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: rental)
       
Some Warnings
Back SMYC Forward
  • What About if Statements?
    • They tend to be much more verbose
    • They treat the discrete and continuous variables differently for no apparent reason
  • One Example:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: ifbirthweight)
       
  • Another Example:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: ifrental)
       
Some Warnings (cont.)
Back SMYC Forward
  • What About ternary operators?
    • Some people think they are more concise then if statements but they are still more verbose than indicators
  • An Example:
    • javaexamples/programmingpatterns/Indicators.java (Fragment: ternaryrental2)
       
There's Always More to Learn
Back -