SHARE
TWEET

Untitled

a guest Jun 24th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import java.io.*;
  4. public class Main {
  5.  
  6.     private static Scanner scan = new Scanner(System.in);
  7.     public static void main(String[] args) {
  8.  
  9. //initialize variables for Forest object, name, height and other inputs
  10. Forest thisForest = null;
  11.         char userInput = ' ';
  12.         String forestName;
  13.         int height = 0;
  14.         String loadName = null;
  15.  
  16.         while((userInput != 'x') && (userInput != 'X' )){
  17.  
  18.             switch (userInput) {
  19.  
  20. //sets the first prompt      
  21.                 case ' ':
  22.                     System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  23.                     userInput = scan.next().charAt(0);
  24.  
  25.                     break;
  26.  
  27. //Displays the output but only if the forest exists      
  28.                 case 'D':
  29.                 case 'd':
  30.  
  31.                     if(thisForest != null){
  32.                         thisForest.display();
  33.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  34.                         userInput = scan.next().charAt(0);
  35.                     }else{
  36.                         System.out.println("No Forrest.n");
  37.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  38.                         userInput = scan.next().charAt(0);
  39.                     }
  40.                     break;
  41.  
  42.                 case 'N':
  43.                 case 'n':
  44.  
  45.                     System.out.print("What is the name of the forest: ");
  46.                     forestName = scan.next();
  47.                     thisForest = new Forest(forestName);
  48.  
  49.                     System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  50.                     userInput = scan.next().charAt(0);
  51.  
  52.                     break;
  53.  
  54.                 case 'Y':
  55.                 case 'y':
  56. //grows the equivalent of a "year".  
  57.                     if(thisForest != null){
  58.                         thisForest.yearGrowth();
  59.  
  60.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  61.                         userInput = scan.next().charAt(0);
  62.                     }else{
  63.                         System.out.print("No forest exists.n");
  64.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  65.                         userInput = scan.next().charAt(0);
  66.                     }
  67.                     break;
  68.  
  69.                 case 'R':
  70.                 case 'r':
  71.  
  72.                     //reaps, but only if the forest exists. Catches possible exceptions.
  73.                     if(thisForest != null){
  74.                         try{
  75.                             System.out.print("What height to reap at :");
  76.                             height = scan.nextInt();
  77.                         }catch(NumberFormatException e){
  78.                             System.out.println("ERROR: Invalid heightn");
  79.  
  80.                             System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  81.                             scan.nextLine();
  82.                             userInput = scan.next().charAt(0);
  83.                             break;
  84.                         } catch(InputMismatchException e){
  85.                             System.out.println("ERROR: Invalid heightn");
  86.  
  87.                             System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  88.                             scan.nextLine();
  89.                             userInput = scan.next().charAt(0);
  90.                             break;
  91.                         }
  92. //end of try-catch      
  93.                         thisForest.reap(height);
  94.  
  95.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  96.                         scan.nextLine();
  97.                         userInput = scan.next().charAt(0);
  98.                         break;
  99. //output if there is no forrest      
  100.                     }else{
  101.                         System.out.print("There is no forest to reap.n");
  102.  
  103.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  104.                         scan.nextLine();
  105.                         userInput = scan.next().charAt(0);
  106.                     }
  107.                     break;
  108.  
  109.  
  110.  
  111.                 case 'S':
  112.                 case 's':
  113. //saves the program      
  114.                     if(thisForest != null){
  115.                         try{
  116.                             Forest.saveForest(thisForest);
  117.                         }catch(IOException e){
  118.                             System.out.println("Cannot save.");
  119.                         }
  120.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  121.                         userInput = scan.next().charAt(0);
  122.                     }else{
  123.                         System.out.println("No Forrest exists to save.");
  124.                         System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  125.                         userInput = scan.next().charAt(0);
  126.  
  127.                     }
  128.                     break;
  129.  
  130.                 case 'L':
  131.                 case 'l':
  132. // loads the program      
  133.                     try{
  134.                         System.out.print("What is the name of the Forest: ");
  135.                         loadName = scan.next();
  136.                         thisForest = Forest.loadForest(loadName);
  137.  
  138.                     }catch(IOException e){
  139.                         System.out.println("Cannot load.");
  140.                     }
  141.                     System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  142.                     userInput = scan.next().charAt(0);
  143.                     break;
  144.  
  145.                 default:
  146.                     System.out.println("ERROR: Invalid Option.");
  147.                     System.out.print("(D)isplay, (N)ew, (Y)ear, (R)eap, (S)ave, (L)oad, e(X)it :");
  148.                     userInput = scan.next().charAt(0);
  149.                     break;
  150.             }
  151.         }
  152. //after x is received and the while loop breaks.
  153.         System.out.println("Goodbye");
  154.  
  155.     }
  156. }
  157.      
  158. import java.io.*;
  159. import java.util.*;
  160. public class Forest implements Serializable{
  161.  
  162. //creates variables and constants
  163.     private final int MAX_NUM_TREES = 10;
  164.     private String name;
  165.     private Tree[] arrayOfTrees;
  166.     int index;
  167.     public Forest(String forestName){
  168.  
  169.         index = 0;
  170.         name = forestName;
  171.         arrayOfTrees = new Tree[MAX_NUM_TREES];
  172.  
  173.         for(index = 0; index < arrayOfTrees.length; index++){
  174.  
  175.             arrayOfTrees[index] = new Tree();
  176.  
  177.         }
  178.     }
  179.  
  180.     public void display(){
  181. // displays the array of trees and the index
  182.         index = 0;
  183.  
  184.         if(name != null){
  185.  
  186.             System.out.println(name);
  187.             for(index = 0; index < arrayOfTrees.length; index ++){
  188.                 System.out.printf("%2d   :   %sn", (index + 1), arrayOfTrees[index]);
  189.             }
  190.         }else{
  191.             System.out.println("No forest.");
  192.         }
  193.  
  194.     }
  195.    public void yearGrowth(){
  196. //grows each tree in the array
  197.         index = 0;
  198.  
  199.         for(index = 0; index < arrayOfTrees.length ; index ++){
  200.  
  201.             arrayOfTrees[index].grow();
  202.         }
  203.  
  204.     }
  205.    public void reap(int reapHeight){
  206.         //reaps the trees and prints out the old and new information
  207.         index = 0;
  208.  
  209.  
  210.         for(index = 0; index < arrayOfTrees.length; index++){
  211.  
  212.             if(arrayOfTrees[index].getHeight() >= reapHeight){
  213.  
  214.                 System.out.println("Cut " + (index+1) + " : " + arrayOfTrees[index] );
  215.                 arrayOfTrees[index] = new Tree();
  216.                 System.out.println("New " + (index+1) + " : " + arrayOfTrees[index] );
  217.  
  218.             }
  219.         }
  220.  
  221.     }
  222. public static void saveForest(Forest forest) throws IOException {
  223. //saves the forest
  224.         String name = forest.getName();
  225.         ObjectOutputStream toStream;
  226.  
  227.         toStream = new ObjectOutputStream(new FileOutputStream(name));
  228.         toStream.writeObject(forest);
  229.         toStream.close();
  230.     }
  231.  
  232.    public static Forest loadForest(String fileName) throws IOException {
  233. //loads the forest
  234.         ObjectInputStream fromStream = null;
  235.         Forest local;
  236.  
  237.         fromStream = new ObjectInputStream(new FileInputStream(fileName));
  238.         try {
  239.             local = (Forest)fromStream.readObject();
  240.         }catch (ClassNotFoundException e) {
  241.             System.out.println(e.getMessage());
  242.             return(null);
  243.         }finally{
  244.             try {
  245.                 if (fromStream != null) {
  246.                     fromStream.close();
  247.                 }
  248.             } catch (IOException e) {
  249.                 System.out.println(e.getMessage());
  250.                 return(null);
  251.             }
  252.         }
  253.         return(local);
  254.     }
  255.     public String getName(){
  256.  
  257.         return (name);
  258.     }
  259. }
  260.      
  261. import java.util.Random;
  262. import java.util.*;
  263. import java.io.*;
  264.  
  265. public class Tree implements Serializable{
  266.  
  267. //creates the variables as the
  268.     private double height;
  269.     private double growthRate;
  270.     private static Random rand = new Random();
  271.     final double MIN_HEIGHT = 1;
  272.     final double MIN_GROWTH_RATE = 0.5;
  273.     final double MAX_HEIGHT = 5;
  274.     final double MAX_GROWTH_RATE = 1.0;
  275.  
  276.     public Tree() {
  277. //creates tree with a height and a growth rate
  278.         Random rand = new Random();
  279.  
  280.         height = (MIN_HEIGHT + ((Math.random() * (MAX_HEIGHT - MIN_HEIGHT))));
  281.         growthRate = (MIN_GROWTH_RATE + (Math.random() * (MAX_GROWTH_RATE - MIN_GROWTH_RATE)));
  282.  
  283.  
  284.     }
  285.  
  286.     public double grow(){
  287. //tree grows and returns height
  288.  
  289.         height = height * (1 + growthRate);
  290.         return height;
  291.  
  292.  
  293.     }
  294.  
  295.     public double getHeight(){
  296.  
  297.         return (height);
  298.  
  299.     }
  300.  
  301.     public double getGrowthRate(){
  302.  
  303.         return (growthRate);
  304.  
  305.     }
  306.  
  307.     public String toString(){
  308. //toString formats the output with height and growthrate
  309.  
  310.         return (String.format("%7.2f (%2d%% pa)", height, ((int)(growthRate * 100))));
  311.  
  312.     }
  313. }
  314.  
  315. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top