Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************
- * @file Forest.java
- * Answers for GA.1
- * @author hypesystem
- */
- package forest;
- /**
- * This class is a forest. With trees.
- * @author hypesystem
- */
- public class Forest {
- /**
- * @param args the command line arguments
- */
- private Tree tree1, tree2, tree3;
- /**
- * Constructor creates three trees - it's one huge forest!
- */
- public Forest() {
- tree1 = new Tree(10.0);
- tree2 = new Tree(25.0);
- tree3 = new Tree(40.0);
- }
- /**
- * Method to show the current status of the trees.
- */
- public void show() {
- System.out.println("Hvis et træ falder i en skov, og der ingen er til at høre det...");
- tree1.show();
- tree2.show();
- tree3.show();
- System.out.println("");
- }
- /**
- * Method to make all trees grow (yes, time is the same for all the trees).
- */
- public void growOneYear() {
- tree1.growOneYear();
- tree2.growOneYear();
- tree3.growOneYear();
- System.out.println("Et år er passeret...\n");
- }
- /**
- * Main class to instantiate the application.
- * @param args
- */
- public static void main(String[] args) {
- Forest forest = new Forest();
- forest.show();
- forest.growOneYear();
- forest.show();
- }
- }
- /******************************
- * @file Tree.java
- * Answers for GA.1
- * @author hypesystem
- */
- package forest;
- /**
- * This class is a tree, commonly found in forests.
- * @author hypesystem
- */
- public class Tree {
- private int age;
- private double height;
- private double growthPct;
- private boolean alive;
- /**
- * Constructor creates a tree. A tree is one year old when it is "born". For some reason.
- * @param growthPct
- */
- public Tree(double growthPct) {
- age = 1;
- alive = true;
- height = 0.25;
- this.growthPct = growthPct;
- }
- /**
- * Method to make the tree grow.
- * It also handles tree-funerals. Trees die when they are 120 years old. Always.
- */
- public void growOneYear() {
- age++;
- if(height < 20) {
- height = height * (1 + growthPct/100);
- }
- if(height > 20) {
- height = 20;
- }
- if(age >= 120) {
- alive = false;
- }
- }
- /**
- * Method to test whether the tree is still alive.
- * @return alive
- */
- public boolean getAlive() {
- return alive;
- }
- /**
- * Shows the current status of the tree in question.
- * Is it alive? If not, how old is it? How tall is it? And last, but not least,
- * if it falls in a forest and nobody's there to hear it -- will it make a sound?
- */
- public void show() {
- if(alive) {
- System.out.println("Er det " + age + " år gammelt, og " + height + " meter højt.");
- }
- else {
- System.out.println("Er det dødt...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment