Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 7.03 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.Scanner;
  2. import java.io.*;
  3. //=============================================================================
  4. public class UseForest {
  5. //-----------------------------------------------------------------------------
  6.   private static Scanner keyboard = new Scanner (System.in);
  7.   private static final int ZERO = 0;
  8.  
  9. //-----------------------------------------------------------------------------
  10.   public static void main (String[] args) {
  11. //---variables-----------------------------------------------------------------
  12.  
  13.     String name;
  14.     int index;
  15.     char choice;
  16.     Forest currentForest;
  17.     Forest newForest;
  18.     int reapHeight;
  19.  
  20.  
  21.  
  22. //-------user input------------------------------------------------------------
  23.  
  24.     System.out.println();
  25.  
  26.     do {
  27.       System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave," +
  28.        " (L)oad, e(X)it : ");
  29.       choice = keyboard.next().charAt(0);
  30.       choice = Character.toUpperCase(choice);
  31.  
  32. //------Choices---------------------------------------------------------------
  33.  
  34.         switch (menu) {
  35.  
  36.           case 'D' :
  37.             if (currentForest == null) {
  38.               System.out.println("No forest");
  39.               break;
  40.             } else {
  41.               currentForest.display();
  42.             }
  43.             break;
  44.  
  45.           case 'N' :
  46.             System.out.print("What is the forest name : ");
  47.             name = keyboard.nextLine();
  48.             newForest = new Forest(name);
  49.             break;
  50.  
  51.           case 'Y' :
  52.             if (currentForest == null) {
  53.               System.out.println("No forest");
  54.             } else {
  55.               currentForest.year();
  56.             }
  57.             break;
  58.  
  59.           case 'R' :
  60.             if (currentForest == null) {
  61.               System.out.println("No forest");
  62.             } else {
  63.               System.out.print("What height to reap at : ");
  64.               reaper = keyboard.nextDouble();
  65.               currentForest.reap(reaper);
  66.             }
  67.             keyboard.nextLine();
  68.             break;
  69.  
  70.           case 'S' :
  71.             if (currentForest == null) {
  72.               System.out.println("No forest");
  73.             } else {
  74.               //newForest.save(newForest);
  75.                 currentForest.save(name);
  76.             }
  77.             break;
  78.  
  79.           case 'L' :
  80.             System.out.print("What is the forest name : ");
  81.             forestName = keyboard.nextLine();
  82.             //newForest.load(forestName);
  83.                         currentForest=load(forestName);
  84.             break;
  85.  
  86.           case 'X' :
  87.             System.out.println("Goodbye");
  88.             System.out.println();
  89.             break;
  90.  
  91.           default :
  92.             System.out.println("ERROR: Invalid option");
  93.             break;
  94.  
  95.            }
  96.         }
  97.         while (menu != 'X');
  98.  }
  99. //-----------------------------------------------------------------------------
  100.     public Forest load(String forestName) throws IOException {
  101.  
  102.       try {
  103.         ObjectInputStream fromStream;
  104.         Forest local;
  105.  
  106.         fromStream = new ObjectInputStream(new FileInputStream(forestName));
  107.         local = (Forest)fromStream.readObject();
  108.         fromStream.close();
  109.         return (local);
  110.       } catch (Exception e) {
  111.         System.out.println("Error");
  112.       }
  113. //-----------------------------------------------------------------------------
  114.     }
  115. }
  116. //==================================================================
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. import java.io.*;
  124.  
  125. //=============================================================================
  126. public class Forest implements Serializable {
  127.  
  128. //-----------------------------------------------------------------------------
  129.  
  130.  private Tree[] trees;
  131.  private static final int TREES = 10;
  132.  
  133.   String name;
  134.   int index;
  135.  
  136. //-----------------------------------------------------------------------------
  137.  
  138.  public Forest(String treeName) {
  139.  
  140.    trees = new Tree[TREES];
  141.  
  142.    name = treeName;
  143.  
  144.    for(index = 0; index < trees.length; index++){
  145.       trees[index] = new Tree();
  146.     }
  147.   }
  148. //-----------------------------------------------------------------------------
  149.  
  150.  public void display() {
  151.  
  152.     for(index = 0; index < trees.length; index++) {
  153.       System.out.printf("%2d", (index+1));
  154.       System.out.print(trees[index]);
  155.    
  156.  
  157.    }
  158.  }
  159. //-----------------------------------------------------------------------------
  160.  public void reapForest(int reapHeight) {
  161.  
  162.     for(index = 0; index < trees.length; index++) {
  163.       if(trees[index].getHeight <= reapHeight) {
  164.          System.out.print("Cut ");
  165.          System.out.printf("%2d ",(index+1));
  166.          System.out.print(trees[index]);
  167.          trees[index] = new Tree();
  168.          System.out.print("New ");
  169.          System.out.printf("%2d ",(index+1));
  170.          System.out.print(trees[index]);
  171.         }
  172.      }
  173.  
  174.   }
  175. //-----------------------------------------------------------------------------
  176.  public String getName() {
  177.  
  178.     return(name);
  179.  
  180.   }
  181. //-----------------------------------------------------------------------------
  182.  public void year() {
  183.  
  184.   for(index = 0; index < trees.length; index++) {
  185.       trees[index].year();
  186.  
  187.       }
  188.  
  189.    }
  190.  
  191. //-----------------------------------------------------------------------------
  192.        public void save(Forest newForest) throws IOException {
  193.  
  194.         ObjectOutputStream toStream;
  195.         toStream = new ObjectOutputStream(new FileOutputStream
  196. (newForest.getName()));
  197.         toStream.writeObject(newForest);
  198.         toStream.close();
  199.     }
  200. //-----------------------------------------------------------------------------
  201. }
  202. //=============================================================================
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. import java.util.*;
  211.  
  212. import java.io.*;
  213.  
  214. //=============================================================================
  215.  
  216. public class Tree implements Serializable{
  217.  
  218. //-----------------------------------------------------------------------------
  219.  
  220.   private double height;
  221.  
  222.   private int growthRate;
  223.  
  224. //-----------------------------------------------------------------------------
  225.  
  226.   private static final double MAX_HEIGHT = 5.0;
  227.  
  228.   private static final int RATE = 50;
  229.  
  230. //-----------------------------------------------------------------------------
  231.  
  232.   public Tree() {
  233.  
  234.  
  235.  
  236.     height = MAX_HEIGHT * Math.random();
  237.  
  238.     growthRate = RATE + (int)(Math.random()*RATE);
  239.  
  240.  
  241.  
  242.   }
  243.  
  244. //-----------------------------------------------------------------------------
  245.  
  246.   public double heightGenerator() {
  247.  
  248.  
  249.  
  250.     return(height);
  251.  
  252.  
  253.  
  254.   }
  255.  
  256. //-----------------------------------------------------------------------------
  257.  
  258.   public String toString() {
  259.  
  260.  
  261.  
  262.     return(" : Math.Round(height) (" + growthrate + ")%");
  263.  
  264.  
  265.  
  266.   }
  267.  
  268. //-----------------------------------------------------------------------------
  269.  
  270.   public void growth() {
  271.  
  272.  
  273.  
  274.     height = height * ((100.0 + growthRate)/100.0);
  275.  
  276.  
  277.  
  278.   }
  279.  
  280.  
  281.  
  282. //-----------------------------------------------------------------------------
  283.  
  284. }
  285.  
  286. //=============================================================================