User Tools

Site Tools


solution_of_12._mortality

The Capsis training online

Back to the table of contents

Solution: 12. Mortality

Try to work with the helping elements before looking at the solution…

Helping elements

  • make a new method in TraModel: private boolean treeSurvival()
  • given a tree, the method will return true if it survives, or false if it dies (the tree does not need to be passed to the method)
  • possible code for this method:
    double survivalProba = 0.98;
    double proba = random.nextDouble (); // [0,1[
    return proba <= survivalProba; // returns true in 98% of the cases
  • in processEvolution (), in the trees growing loop, for each tree, check the result of the treeSurvival () method
  • if it returns false, skip the tree growth and addition in newScene: it is dead, it will not be present in the newScene
  • the 'continue' statement jumps directly to the next iteration, to be called if the tree is dead, before its addition in newScene
  • compile, run a new simulation to check if trees disappear with this mortality feature

A possible solution

A possible survival method Calling the method in the tree growth loop After 10 years, new trees appear, after 5 more years, mortality is visible

Back to the table of contents

solution_of_12._mortality.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1