For this example, our problem statement is to code an Association.

What is an association? Let us discuss it.

Introduction
An "Association" associates a key to a value.
Example
Here we see an association that associates the key "K20299820" (an id) with a value that is a "Student" object.
Several Examples
Here we show a set of associations, each associating a certain key with a specific value.

  • For example, the key "PI" (math symbol) is associated with the number 3.1416.
  • And the key "WA" (state abbrev) is associated with the full name "Washington"

Note association values are flexible. We could associate "WA" with a string (like we do here), but we could also associate it with a full-fledged "State" object.
Definition
Let us try to define an Association so we can code it:

  • We have a key
  • We have a value

By having these two properties we "tie together" (or associate) the key with the value.
Object Structure
Class: Association

Instance Variables:
  • key
  • value

e.g.:
  • key = "WA"
  • value = "Washington"
Real World
Associations are used as part of composite objects.

A "Map" (or dictionary) is a common example.

We may have a small map (e.g. like a map of states keyed by state abbrev as we show here), or a larger one like a map of all employees of a corporation keyed by employee id.


Hooray time to code!