Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gc;
- import java.util.ArrayList;
- /**
- * A forest contains many trees. In this forest there are beeches and ashes.
- * @author hypesystem
- */
- public class Forest {
- ArrayList<Tree> trees;
- public Forest() {
- trees = new ArrayList<Tree>();
- }
- /**
- * Initializes a forest with two beeches and two ashes. One beech is 3 years
- * old, one ash is 2 years old. The two remaining trees are 1 year old.
- */
- public void initialize() {
- trees.add(new Beech());
- trees.get(0).growOneYear();
- trees.get(0).growOneYear();
- trees.add(new Beech());
- trees.add(new Ash());
- trees.get(2).growOneYear();
- trees.add(new Ash());
- }
- /**
- * Makes the whole forest grow one year.
- */
- public void growOneYear() {
- for(Tree tree : trees) {
- tree.growOneYear();
- }
- }
- /**
- * Makes several years pass at once (instead of growing one year at a time)
- * for the whole forest.
- * @param n Amount of years the whole forest should grow
- * ---
- * Note: If growManyYears() is an alternative to calling growOneYear() many
- * times, there is no reason to call show() at each step, as the task
- * assumes. growOneYear() doesn't call show() either. Leaving show() out
- * minimizes output to what you really want to see.
- */
- public void growManyYears(int n) {
- if(n > 0) {
- for(int i = 0; i < n; i++) {
- for(Tree tree : trees) {
- tree.growOneYear();
- }
- }
- }
- else System.out.println("Cannot grow 0 years or less");
- }
- /**
- * Shows the entire forest by calling the show() method of each tree.
- */
- public void show() {
- for(Tree tree : trees) {
- tree.show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment