hypesystem

GC.1 - Forest.java

Oct 19th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package gc;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * A forest contains many trees. In this forest there are beeches and ashes.
  7.  * @author hypesystem
  8.  */
  9. public class Forest {
  10.     ArrayList<Tree> trees;
  11.    
  12.     public Forest() {
  13.         trees = new ArrayList<Tree>();
  14.     }
  15.    
  16.     /**
  17.      * Initializes a forest with two beeches and two ashes. One beech is 3 years
  18.      * old, one ash is 2 years old. The two remaining trees are 1 year old.
  19.      */
  20.     public void initialize() {
  21.         trees.add(new Beech());
  22.         trees.get(0).growOneYear();
  23.         trees.get(0).growOneYear();
  24.         trees.add(new Beech());
  25.         trees.add(new Ash());
  26.         trees.get(2).growOneYear();
  27.         trees.add(new Ash());
  28.     }
  29.    
  30.     /**
  31.      * Makes the whole forest grow one year.
  32.      */
  33.     public void growOneYear() {
  34.         for(Tree tree : trees) {
  35.             tree.growOneYear();
  36.         }
  37.     }
  38.    
  39.     /**
  40.      * Makes several years pass at once (instead of growing one year at a time)
  41.      * for the whole forest.
  42.      * @param n Amount of years the whole forest should grow
  43.      * ---
  44.      * Note: If growManyYears() is an alternative to calling growOneYear() many
  45.      * times, there is no reason to call show() at each step, as the task
  46.      * assumes. growOneYear() doesn't call show() either. Leaving show() out
  47.      * minimizes output to what you really want to see.
  48.      */
  49.     public void growManyYears(int n) {
  50.         if(n > 0) {
  51.             for(int i = 0; i < n; i++) {
  52.                 for(Tree tree : trees) {
  53.                     tree.growOneYear();
  54.                 }
  55.             }
  56.         }
  57.         else System.out.println("Cannot grow 0 years or less");
  58.     }
  59.    
  60.     /**
  61.      * Shows the entire forest by calling the show() method of each tree.
  62.      */
  63.     public void show() {
  64.         for(Tree tree : trees) {
  65.             tree.show();
  66.         }
  67.     }
  68.    
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment