hypesystem

GA.1

Sep 8th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. /******************************
  2.  * @file Forest.java
  3.  * Answers for GA.1
  4.  * @author hypesystem
  5.  */
  6.  
  7. package forest;
  8.  
  9. /**
  10.  * This class is a forest. With trees.
  11.  * @author hypesystem
  12.  */
  13. public class Forest {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.    
  19.     private Tree tree1, tree2, tree3;
  20.    
  21.     /**
  22.      * Constructor creates three trees - it's one huge forest!
  23.      */
  24.     public Forest() {
  25.         tree1 = new Tree(10.0);
  26.         tree2 = new Tree(25.0);
  27.         tree3 = new Tree(40.0);
  28.     }
  29.    
  30.     /**
  31.      * Method to show the current status of the trees.
  32.      */
  33.     public void show() {
  34.         System.out.println("Hvis et træ falder i en skov, og der ingen er til at høre det...");
  35.         tree1.show();
  36.         tree2.show();
  37.         tree3.show();
  38.         System.out.println("");
  39.     }
  40.    
  41.     /**
  42.      * Method to make all trees grow (yes, time is the same for all the trees).
  43.      */
  44.     public void growOneYear() {
  45.         tree1.growOneYear();
  46.         tree2.growOneYear();
  47.         tree3.growOneYear();
  48.         System.out.println("Et år er passeret...\n");
  49.     }
  50.    
  51.     /**
  52.      * Main class to instantiate the application.
  53.      * @param args
  54.      */
  55.     public static void main(String[] args) {
  56.         Forest forest = new Forest();
  57.         forest.show();
  58.         forest.growOneYear();
  59.         forest.show();
  60.     }
  61. }
  62.  
  63. /******************************
  64.  * @file Tree.java
  65.  * Answers for GA.1
  66.  * @author hypesystem
  67.  */
  68.  
  69. package forest;
  70.  
  71. /**
  72.  * This class is a tree, commonly found in forests.
  73.  * @author hypesystem
  74.  */
  75. public class Tree {
  76.    
  77.     private int age;
  78.     private double height;
  79.     private double growthPct;
  80.     private boolean alive;
  81.    
  82.     /**
  83.      * Constructor creates a tree. A tree is one year old when it is "born". For some reason.
  84.      * @param growthPct
  85.      */
  86.     public Tree(double growthPct) {
  87.         age = 1;
  88.         alive = true;
  89.         height = 0.25;
  90.         this.growthPct = growthPct;
  91.     }
  92.    
  93.     /**
  94.      * Method to make the tree grow.
  95.      * It also handles tree-funerals. Trees die when they are 120 years old. Always.
  96.      */
  97.     public void growOneYear() {
  98.         age++;
  99.         if(height < 20) {
  100.             height = height * (1 + growthPct/100);
  101.         }
  102.         if(height > 20) {
  103.             height = 20;
  104.         }
  105.         if(age >= 120) {
  106.             alive = false;
  107.         }
  108.     }
  109.    
  110.     /**
  111.      * Method to test whether the tree is still alive.
  112.      * @return alive
  113.      */
  114.     public boolean getAlive() {
  115.         return alive;
  116.     }
  117.    
  118.     /**
  119.      * Shows the current status of the tree in question.
  120.      * Is it alive? If not, how old is it? How tall is it? And last, but not least,
  121.      * if it falls in a forest and nobody's there to hear it -- will it make a sound?
  122.      */
  123.     public void show() {
  124.         if(alive) {
  125.             System.out.println("Er det " + age + " år gammelt, og " + height + " meter højt.");
  126.         }
  127.         else {
  128.             System.out.println("Er det dødt...");
  129.         }
  130.     }
  131.    
  132. }
Advertisement
Add Comment
Please, Sign In to add comment