Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab01;
- /**
- * Forest class
- * Models a forest of trees
- * @author Chad Humphries
- * @version lab01
- * Date Created: Sept 14, 2019
- */
- public class Forest
- {
- //Variables
- private Tree[] trees;
- /**
- * Default constructor
- */
- public Forest()
- {
- trees = new Tree[10];
- }
- /**
- * Plants a tree
- * @param tree Tree to be planted
- */
- public void plant(Tree tree)
- {
- for(int i = 0; i < trees.length; i++)
- {
- if(trees[i] == null)
- {
- trees[i] = tree;
- break;
- }
- }
- }
- /**
- * Determines the amount of trees in the forest
- * @return Amount of trees in the forest
- */
- public int numberOfTrees()
- {
- int amount = 0;
- for(Tree t : trees)
- {
- if(t != null)
- {
- amount++;
- }
- }
- return amount;
- }
- /**
- * Prints details on all trees in the forest
- */
- public void showTrees()
- {
- System.out.println("Trees in forest:");
- System.out.println("**********************");
- for(Tree t : trees)
- {
- if(t != null)
- {
- t.showDetails();
- }
- }
- System.out.println("**********************\n");
- }
- /*
- * Prints details on all trees planted in that year
- */
- void showTreesByYear(int year)
- {
- System.out.println("Trees planted in: " + year);
- System.out.println("**********************");
- for(Tree t : trees)
- {
- if(t != null && t.getYear() == year)
- {
- t.showDetails();
- }
- }
- System.out.println("**********************\n");
- }
- /**
- * Removes all trees planted in that year
- * @param year Year of trees to be removed
- */
- void removeTreesByYear(int year)
- {
- for(int i = 0; i < trees.length; i++)
- {
- if(trees[i] != null && trees[i].getYear() == year)
- {
- trees[i] = null;
- }
- }
- System.out.println("Removed trees planted in: " + year);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement