What is a Tree?

Chapter: What is a Tree?

I think that I shall never see / A poem lovely as a tree (Joyce Kilmer 1888-1918)

In the context of this simulation, a tree is an object. Objects have state and functionality.

The state of a tree might involve:

  1. its type (oak or pine)
  2. its burn time
  3. is it unburnt, burning, or burnt?
  4. if burning, how long has it been burning?
  5. what does it look like? (so we can draw it)
  6. who are its neighbors?

These MIGHT be mapped to instance variables:

  1. String myType
  2. int myBurnTime
  3. boolean unburnt, burning, burnt
  4. int alightFor
  5. Image myPic ?
  6. Collection< Tree > neighbors ?

The state of an object can be encapsulated in instance variables. But when a lot of objects are likely to share the same state, it helps to put them all in the same class. This motivates a big idea:

We have two kinds of tree. Why not have a PineTree class and an OakTree class. Both of these classes are descended from a Tree class. This way, species-dependent data (like burn time) can be assigned in the constructor rather than specifically assigned each time we need one tree or another. And we can eliminate the need for an instance variable indicating whether a Tree is a PineTree or an OakTree because that information is inherent in the Tree's class.

The functionality of a tree might include:

  1. get my type
  2. where am I?
  3. add a neighbor?
  4. get my neighbors?
  5. get my unburnt/burning/burnt information
  6. get my icon
  7. ignite me
  8. draw me?
  9. update me

These MIGHT be mapped into methods:

  1. public String getType()
  2. public Dimension getLoc()
  3. public void addNeighbor(Tree t)
  4. public Collection< Tree > getNeighbors()
  5. public boolean isUnburnt() ... etc
  6. public ImageIcon getIcon()
  7. public void ignite()
  8. public void draw()

rhyspj@gwu.edu