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

Untitled

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